-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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
Function arguments are inferred to be of type Any despite annotation (v0.3.0-prerelease) #6813
Comments
The types in the function signature only serve as a dispatch spec; they don't "declare" the variables to be of those types in a case like this where the arguments are assigned to. It has to be this way, to make dispatch and declaration orthogonal. I was able to fix it by adding these declarations at the beginning of the function:
One could argue that this orthogonality is not so important, and you just have to expect arguments with types to have fixed types. But the current behavior is intentional and we'll probably keep it that way for now. |
Understood – thanks for the diagnosis. I had thought that type annotations in the signature had the effect as those in the two lines you added. For my understanding, could you give an example of a case where the current behavior is beneficial? |
So @JeffBezanson is then a failure in some sense of the type inference, and is thus fixable? |
Sometimes people write code like
and expect the type of |
But that doesn't seem to be the case here? |
In the new version of the code, the abstract |
Oh, yes, indeed. 👍 |
For what it's worth, I think that the code was prettier and more expressive when empty and nonempty nodes had different types. The change in the new version was made for performance reasons alone (and does improve the speed of treap operations significantly). I wonder if there's any way to use the type system to encode the distinction while maintaining performance? |
It would be nice to have a way to "seal" an abstract type so that its set of subtypes is fixed. Then you would get more of the benefits of tagged unions as in many functional languages. |
Isn't a sealed abstract type just a type union? The only difference is that an abstract type is declared before its constituent types, whereas a type union must be defined after. However, if we allowed referring to not-yet-declared types that are defined later in the same closed module (i.e. not Main), then we would get both for free and solve the mutually recursive types problem. |
I'm not sure they're equivalent when parameters are involved. For example
|
Can't you get around that with a typealias? Close abstract types seem kind of featurey. |
I agree it feels featurey, but I don't see how to express my example with typealias. |
typealias Sub{T} Union(Sub1{T},Sub2) |
In my example |
What is the abstract type that you want to close here? |
Yes, that's the only abstract type in the example. |
There are an unbounded number of abstract types in the example: |
With the typealias, |
In the definition of
getindex()
below, which is taken from a Treap implementation discussed in this julia-users thread, inspecting variable types usingcode_typed
reveals that botht
andindex
are inferred to be of typeAny
despite being annotated with specific types in the function declaration.Here's a minimal piece of code that will reproduce the issue (load the Treap code first):
For convenience, here's the definition of
getindex()
that is being invoked above:The text was updated successfully, but these errors were encountered: