-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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 annotations in global scope #964
Comments
It is expected insofar as that we are aware of the issue. Currently using global variables kills performance specifically because they are of unknown type. Type annotations on global variables are a planned feature but IIRC, there was some technical limitation (maybe @JeffBezanson can go into detail on that). Hope that helps. |
Planned feature. |
Further plan: make globals type-const by default. That is, writing glb = x is equivalent to this: glb::typeof(x)
glb = x unless there was some prior declaration of the type of glb = 1 # not const but has to be Int
glb::Any # can hold anything
glb::String # can hold any kind of string This approach will alleviate the horrific performance trap that global variables currently constitute. Most globals are actually constant and they hardly ever change type. |
The current workaround is to declare globals as I keep running into this problem in modules, in which the module needs to do some run-time initialization and cache the results in a global, but the only way to do this without screwing the compiler seems to be to annotate every place that the global is used, which is annoying. Please don't let this issue fall off the queue. |
I'm not sure if this is entirely kosher but I have found that I can declare an array as |
That is completely kosher – the only thing that's |
A possibly related problem: you can't usefully declare type annotations with the
is accepted by the parser, but the type annotation on the |
Hearty bump. I've recently been working on porting type-stability-sensitive C++ code where I use the pattern of: function foo(args...)
init::Uint64 = 1 # need to ensure init::Uint64 throughout function
... But then when trying to debug, I can't run the lines one by one because just running In [99]: init::Uint64 = 1
Out [99]: ERROR: init not defined
while loading In[99], in expression starting on line 1 It just makes type annotating a hassle because it's the right way to ensure the type stability of a variable, yet I can't switch between using it in function vs. global scope. |
I've came to discover yet another workaround: using 0-dimensional arrays to store single boxed immutables:
About the "globals type-const by default" I think that not only it does not solve all problems but it will also break a little the REPL user experience... |
Yes, that's a bit of a problem. The REPL experience would not be good with type-const by default. |
also deprecates `global x::T` meaning a typeassert also fix a bug where a type-assert was considered to be effect-free, causing it to be converted into a local variable declaration ref #964
also deprecates `global x::T` meaning a typeassert also fix a bug where a type-assert was considered to be effect-free, causing it to be quasi-converted into a local variable declaration ref #964
also deprecates `global x::T` meaning a typeassert also fix a bug where a type-assert was considered to be effect-free, causing it to be quasi-converted into a local variable declaration ref #964
deprecate `x::T` as type declaration syntax, ref #16071 deprecate `x::T = 0` where x is global and `global x::T` meaning typeassert, ref #964 also fix a bug where a typeassert was considered to be effect-free, causing it to be quasi-converted into a local variable declaration fix a bug in env.jl where a typeassert was intended
deprecate `x::T` as type declaration syntax, ref JuliaLang#16071 deprecate `x::T = 0` where x is global and `global x::T` meaning typeassert, ref JuliaLang#964 also fix a bug where a typeassert was considered to be effect-free, causing it to be quasi-converted into a local variable declaration fix a bug in env.jl where a typeassert was intended
I'm not sure why this is considered breaking. Just because we ignore it now? If so, can we at least make this syntax an error on globals so that it is no longer breaking and can be added in 1.1 instead of having to wait until 2.0? Alternatively, this just doesn't seem all that hard to implement. |
This is no longer breaking after #17445. |
Sometimes I just copy code from other people' s module to see the output. Can we make this a warning instead of error? |
Or better yet: add proper support for it. |
If someone is willing to give me some guidance, I'll happily make the PR to fix this. |
This is an initial proposal for supporting typed globals based on #964. The syntax for this is either `global x::T` or just doing `x::T = 1` in global scope. It is even supported to add these annotations to globals from inside functions. The type declaration will then be applied when the method is defined and inside the function body conversion to the specified type will happen automatically, similar to type annotations for local variables. This conversion will not be applied if the assignment is not done inside the same scope of the type annotation however. This could potentially be supported as well, but the problem is that this would mean any assignment to a global variable - typed or not - would need to go through a call to `convert` first, since lowering can't know if a given binding already has a type annotation. This probably wouldn't be a good thing for latency and sysimage size and would add an invalidation risk. It is allowed to refine a type annotation, but there will be a warning. Widening the type or changing it to any other type that is not a subtype of the previous one is an error, since that could cause cached compiled code to become invalid.
This is an initial proposal for supporting typed globals based on #43455. The syntax for this is either `global x::T` or just doing `x::T = 1` in global scope. It is even supported to add these annotations to globals from inside functions. The type declaration will then be applied when the method is defined and inside the function body conversion to the specified type will happen automatically, similar to type annotations for local variables. This conversion will not be applied if the assignment is not done inside the same scope of the type annotation however. This could potentially be supported as well, but the problem is that this would mean any assignment to a global variable - typed or not - would need to go through a call to `convert` first, since lowering can't know if a given binding already has a type annotation. This probably wouldn't be a good thing for latency and sysimage size and would add an invalidation risk. It is allowed to refine a type annotation, but there will be a warning. Widening the type or changing it to any other type that is not a subtype of the previous one is an error, since that could cause cached compiled code to become invalid. replaces #43455 closes #964
This is an initial proposal for supporting typed globals based on #43455. The syntax for this is either `global x::T` or just doing `x::T = 1` in global scope. It is even supported to add these annotations to globals from inside functions. The type declaration will then be applied when the method is defined and inside the function body conversion to the specified type will happen automatically, similar to type annotations for local variables. This conversion will not be applied if the assignment is not done inside the same scope of the type annotation however. This could potentially be supported as well, but the problem is that this would mean any assignment to a global variable - typed or not - would need to go through a call to `convert` first, since lowering can't know if a given binding already has a type annotation. This probably wouldn't be a good thing for latency and sysimage size and would add an invalidation risk. It is allowed to refine a type annotation, but there will be a warning. Widening the type or changing it to any other type that is not a subtype of the previous one is an error, since that could cause cached compiled code to become invalid. replaces #43455 closes #964
add type declarations to global bindings replaces #43455 closes #964 Co-authored-by: Miguel Raz Guzmán Macedo <[email protected]>
add type declarations to global bindings replaces JuliaLang#43455 closes JuliaLang#964 Co-authored-by: Miguel Raz Guzmán Macedo <[email protected]>
add type declarations to global bindings replaces JuliaLang#43455 closes JuliaLang#964 Co-authored-by: Miguel Raz Guzmán Macedo <[email protected]>
add type declarations to global bindings replaces JuliaLang#43455 closes JuliaLang#964 Co-authored-by: Miguel Raz Guzmán Macedo <[email protected]>
Is it expected behaviour that I can't type annotate variable declarations in global scope? Example:
The text was updated successfully, but these errors were encountered: