teal-language / tl

The compiler for Teal, a typed dialect of Lua
MIT License
2.13k stars 107 forks source link

"Type-require" and improvements for circular requires #609

Closed hishamhm closed 1 year ago

hishamhm commented 1 year ago

Adds new syntax for requiring a module's exported type without generating a require() call in the output Lua:

local type MyType = require("mytype")

To go along with this, support for circular requires was improved. Instead of forward-declaring global types, you can now declare types that are circularly-required.

This is still a simple single-pass implementation though: the catch is that you can only refer to those types in type definitions, but not dereference the contents of the type in any way (ie, accessing circular subtypes or using the record table as a concrete object).

If you have circular type dependencies, you can define the types in circularly-required type definition modules, and then type-require these in concrete implementation modules which are not circular.

The test cases added to spec/stdlib/require_spec.lua show some examples of workable patterns as well as some of the pitfalls.

github-actions[bot] commented 1 year ago

Teal Playground URL: https://609--teal-playground-preview.netlify.app