Line types in hacklang: statically invoking the order of function calls

So, Hacklang does not have a new, bizarre type system, where before you can use it, you need to check a variable with a zero value. I wonder if you can achieve something like linear types by statically invoking the order of function calls, and the usual example is to open a file before reading it? In pseudo code:

$file_handler = get_file_handler("myfile");
$file_handler->open();
$string = $file_handler->read();

Now $file_handler->read()without, open()instead of throwing an exception at runtime, it just doesn't compile:

$file_handler = get_file_handler("myfile");
$string = $file_handler->read(); /* Won't compile, must run open() first */

doable?

(OK, maybe a bad example for PHP / Hacklang, since this is not this low level, but you get the idea.)

+4
source share
1 answer

Hack . , , -: (, , , , )

<?hh

newtype closedfile = resource;
newtype openfile = resource;

function get_file_handler(string $filename): closedfile {
  return some_wrapped_function($filename);
}

function open_file_handler(closedfile $file): openfile {
  $file->open();
  return $file;
}

function read(openfile $file): string {
  return $file->read();
}

, , .

, API, - , , , , - . ( , -, API, ! , , , API , , API.)

+6

Source: https://habr.com/ru/post/1540058/


All Articles