-
Notifications
You must be signed in to change notification settings - Fork 111
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Typing a table as an interface makes it abstract #825
Comments
Note: The reason I can't use local mod1: test1 is because in my setup I need to call a function to get a class (using middleclass) and the return is the base Class type so at some point I'd have to typecast anyway |
The bit that was similar to #829 is indeed solvable with When I envisioned modules returning interfaces, I assumed those files would contain the returned interface only (e.g. what we did in luarocks/luarocks#1705 with the Returning an interface and yet having side-effecting operations in the module that would warrant a concrete I am trying to understand your use-case better to see if there's a way to support this, or if organizing the code some other way would lead to a possible workaround. But at first glance I think a separate record would be needed, so that the module returns something concrete that can be required by the other module. Given your example in #821 (reply in thread), wouldn't you already have to declare both an interface and a record to get the middleclass behavior? |
The issue is by declaring the instance as record, it would make it impossible to subtype later for inheritence without yet another interface which would get messy (having 3 types is already excessive for 1 class I feel) The main issue is typing something |
I currently use interface tables to do some runtime logic as well, so would be sad if interface tables didn't actually exist in the generated lua anymore. I'm using latest version now and I see that they do still exist in generate lua, but can't be used anymore due to the change in #830. To elaborate more on my specific use case, I'm doing a kind of dependency injection in lua and using interface tables as the 'id', kind of like this:
With the change in #830, this is no longer allowed. Is there any way to do a cast or something to suppress this error and get access to an "any" reference to interface tables? BTW - I noticed today that enums that are obtained via an include now require the "local type" + "require" syntax as well, like this:
It makes sense but didn't see this explicitly called out anywhere so mentioning in case this wasn't intentional |
First quickly with the enums that makes sense (typescript also doesn't allow abstract types to be required via standard import) |
Speaking of typescript interfaces they do not have this issue, aka an object can be cast to an interface without suddenly being abstract |
One fix could be #724 as then you can pass the type without it actually existing |
@svermeulen That's a bug, they should be fully abstract.
That is not a lot more typesafe than using a string/enum and doing Your particular use-case looks tame enough, but exposing interfaces as concrete objects can have a bunch of other side-effects. I'd rather not go there now; interactions of purely abstract interfaces in the presence of polymorphism is already complex enough as is.
Yes, that was intentional. All abstract types should require |
@Hedwig7s In general a record can be cast to an interface in Teal without becoming abstract. I think I'm finally understanding the issue here now. In your original example, the compiler is treating |
Makes sense. I'll just use a separate concept of ID when I upgrade like you suggest |
@Hedwig7s @svermeulen please give this PR a try (some of the new testcases include examples of the expected behavior, you can take a look). The original example from this issue now works. @Hedwig7s, I'm not sure if this will allow you to "use an interface both for subtyping and typing" as you originally intended; if this doesn't address your use-case, I would suggest declaring the interface in a separate file that can be required separately from the interface instance. |
Minimal test case:
In this example there's no reason to use an interface but in a situation where you need to be able to use an interface both for subtyping and typing (e.g. define a class's type and also use it for subtypes in inheritance, but also getting classes from a function call (replacing mod1 the table)) you'd have to have a seperate record which would be messy and bloated
Unless there's another way to do this (like function generics or something) could this be looked into? Thanks
The text was updated successfully, but these errors were encountered: