From 2efe13529154490fbd565957d55cacf2071a48f1 Mon Sep 17 00:00:00 2001 From: Olle Date: Fri, 19 Jun 2020 00:37:49 +0000 Subject: [PATCH] Add some notes --- typestatetest.php | 95 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 typestatetest.php diff --git a/typestatetest.php b/typestatetest.php new file mode 100644 index 000000000..5265ed15f --- /dev/null +++ b/typestatetest.php @@ -0,0 +1,95 @@ +open(); +if ($file instanceof OpenFile) { + $content = $file->read(); + $file->close(); +} elseif ($file instanceof CannotOpen) { + // Mooo. +} + +for ($i = 1; $i < 10; ++$i) { + // TODO: What here? + $file->open(); +} + +// ClosedFile +$file = new MyFile("somefile"); +if ($file instanceof OpenFile) { + // TODO: Impossible +} +if (hardcomputation()) { + $file->open(); +} +// $file is OpenFile|ClosedFile|CannotOpen +// TODO: Have to check state? + +/** + * @param OpenFile $file + */ +function readall(OpenFile $file) { + $file->read(); + $file->close(); // TODO: Not allowed to change state of aliased variable. + return $file; // TODO: Not allowed to transfer ownership +} + +/** + * @param string $name + * @return ClosedFile + */ +function makefile($name) { + // ClosedFile + $file = new File($name); + dosomething($file); // Can't change state, still closed + return $file; // Owner can change +} + +/** + * @param ClosedFile $file + * @param-out OpenFile $file + */ +function open(ClosedFile &$file) +{ +}