Polymorphic functions in Teal code? #898
-
I know Teal supports polymorphic functions, which will determine the correct output type based on the types of the arguments. But the only examples I've seen are in .d.tl files for Lua code. Is there any way to declare polymorphic functions in Teal code itself? Unions aren't quite as safe, since a |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
No, it is not currently possible. Support for polymorphic functions exists for better represent Lua functions that simulate function polymorphism via manual dynamic dispatch (i.e. good ol' The best you could do nowadays would be to declare a record field to be a polymporphic function, but then assign to it a function with a wider type and then do the |
Beta Was this translation helpful? Give feedback.
No, it is not currently possible. Support for polymorphic functions exists for better represent Lua functions that simulate function polymorphism via manual dynamic dispatch (i.e. good ol'
if
statements). There is no syntax in Teal for describing true function polymorphism.The best you could do nowadays would be to declare a record field to be a polymporphic function, but then assign to it a function with a wider type and then do the
if
matching yourself like you would in Lua; the compiler won't verify that your runtime checks actually match the declared polymorphic types, but the result for the callers would be that the field would look like a polymorphic function.