andremm / typedlua

An Optional Type System for Lua
563 stars 53 forks source link

allow circular require #120

Open daurnimator opened 5 years ago

daurnimator commented 5 years ago

You can have interfaces that include each other:

interface foo
    bar: bar
end
interface bar
    foo: foo
end

However if I want to split that into two files (say, foo.tld and bar.tld), then tlc fails with type error, circular require

mascarenhas commented 5 years ago

The problem is that a Typed Lua require maps to a Lua require, and Lua does not allow circular module references. A possible solution would be for the Typed Lua compiler to notice when a module is only being required for its types (its members are only referenced in type declarations), and not emit a require in the generated Lua file for that case. The compiler would also need to defer the circular require error until the module doing the offending require has been analyzed and it knows whether this is a runtime require or not.

daurnimator commented 5 years ago

I feel like require here needs to act more like include

mascarenhas commented 5 years ago

Do you mean source file inclusion? Lua (and by extension Typed Lua) cannot really have circular requires with the way its modules work, so the exception would really have to be for modules that are required just for their types.

daurnimator commented 5 years ago

This was in reference to .tld files.

mascarenhas commented 5 years ago

Sorry, saw that part only now!