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
Type inference breaks inheritance #1119
Comments
Currently, inheritance in the ARTIQ compiler is only supported for code reuse, not for polymorphism. This means that you cannot make an array of objects of different types, even if one inherits from another. |
The primary reason here is that inserting automatic upcast coercions results in surpising behavior when combined with global type inference. This is the same reason you have to explicitly cast e.g. a float argument to int when passing it to a function that accepts an int. There is no way to fix this other than mandatory type annotations on every function; it's a fundamental limitation of type inference. You'll notice that languages that feature both subtyping and type inference either make all subtyping explicit (OCaml) or require type annotations on all toplevel functions (Rust). |
Thanks for the explanation. So is there a way to tell the compiler explicitly that upcasting is ok for, e.g. the items in a list? |
Currently not. The reason this isn't implemented is because as of 3.5, Python didn't have a typecast operator. In Python 3.6 there is the |
Python 3.6 is also not supported by the packaging (#652). |
Using artiq 3.6 py_42+git3e7cdaa5 on Windows 7 64bit
Mixing instances of a class and its descendant upsets the compiler's type inference. My naive notion is that it's always safe to assume that a descendant provides all the capabilities of its parent (of course, the opposite isn't true, and I wouldn't be surprised if things were more complicated altogether here).
Is this behavior intentional? And if so, can you recommend an elegant way around it?
For example, this
produces
In case the background matters: I'm using a class derived from
SPIMaster
that should be interchangeable with its parent (i.e. experiment code is agnostic to the type of SPI port a device is connected to). This works well as long as all devices of the same type use either one or the other. However, as soon as I have devices of the same type assigning different ones to their.bus
member variable, I can't put them in lists to iterate over, since the compiler doesn't recognize them as the same type anymore.The text was updated successfully, but these errors were encountered: