-
Notifications
You must be signed in to change notification settings - Fork 148
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
WIP/RFC: Implement SA[...] syntax for SArray literals #633
Conversation
So well... for something I expected to be a contentious issue there's been a deafening silence here so far :-) Any thoughts @andyferris on the specifics of this? It's not very featureful compared to |
Thanks Chris. I'm definitely very interested in this. I've been weighing up the difference between The main distinction that I can think of is that we can support generators/comprehensions through the syntax In the macro implementation, I suspect the existing |
I played with an |
For the record, I think the macros supporting calls to |
Yeah, the lowering of typed comprehension seems kind of awful. On the other hand, it might be possible or even easy to change that by adjusting lowering. There's a "TODO this is a hack..." comment in the associated lowering code. |
How about |
Well, we can support |
It looks like you can make it work if it's just julia> f() = _f(x^2 for x in 1:3)
f (generic function with 1 method)
julia> _f(xs) = ntuple(x -> xs.f(x + xs.iter.start - 1), xs.iter.stop)
_f (generic function with 1 method)
julia> @code_warntype optimize=true f()
Variables
#self#::Core.Compiler.Const(f, false)
#3::getfield(Main, Symbol("##3#4"))
Body::Tuple{Int64,Int64,Int64}
1 ─ return (1, 4, 9) Maybe you mean it doesn't work with generic literals (e.g., |
@tkf the case I am thinking of is where the closure (in your case
and try look at e.g. |
Ah, I see. I wasn't paying attention to the non-const'ness of the struct field in your first reply. But luckily for us, it looks like it actually works in 1.2 (and master)?
Though it doesn't work in 1.1 as you said. |
OK, well that is pretty exciting, if there are partially-const struct lattice elements in inference now, then that opens up tons of possibilities all over the place. |
I have direct confirmation from @Keno - it seems partially-const propagation now works anywhere that normal const propagation would (including for non- |
Yeah that's pretty crazy. So it looks like we could have this version. Possibly with a tweak to the way typed comprehensions are lowered by Base we could also have the typed comprehension syntax. That extension would be in julia-1.3 at earliest but I think we can easily wait for that as long as there's some kind of clear path forward to consistency. |
I've implemented the more general I'm wondering whether we actually need to support generator syntax with SArray(i for i=1:3) seems fairly reasonable and doesn't fall afoul of #518. |
Modifying lowering to make typed comprehension turn into something we can hook seems like it was just a matter of deleting a (hopefully obsolete) workaround: JuliaLang/julia#32709. |
cc @EvertSchippers @MeganDawson |
@andyferris this has been slowly bit-rotting but coming back to look at it again I think we should just merge it and see how it feels. As noted above, I think any worries about supporting generator syntax are orthogonal and should just be left to constructors + constant propagation. (Somewhat OT, but FWIW I still think JuliaLang/julia#32709 is sound in principle. We just can't do it yet without further hobbling generated functions.) |
The discussion at #655 has stimulated what I feel is finally a reasonable solution to constructing small static arrays with precise eltype without too much typing. Behold: SA_F64 = SA{Float64}
SA_F32 = SA{Float32}
v = SA_F64[1,2,3] # A Float64 vector
v = SA_F32[1 2; 3 4] # A 2x2 Float32 SMatrix I don't love the underscore there, but I have implemented that and added it here with some tests. With these I'm feeling quite good about this solution now, so I plan to merge it shortly. |
Upside: This gives extremely attractive syntax for short static array literals. Downside: This is a syntax pun: the result of the `SA[...]` depends on whether the name `SA` is bound to `StaticArrays.SA` or is local variable of some other type. However, this is no worse than Int[1,2,3], which is also a pun on the array indexing syntax. Cases where this syntax could refer to something other than StaticArrays.SA are detectable by the compiler and in principle also accessible to a linter.
d6663be
to
2fead27
Compare
Thanks Chris for all your efforts! I like the ease with which a very common vector can be created like this! |
2fead27
to
9f0489e
Compare
I'll put the aliases in a separate PR so people can bikeshed the names if desired. |
Here's a bare bones implementation of a very old idea, originally brought up by @andyferris in #24 (comment). Also related to the #518 discussion at #518 (comment).
The idea is to give a more attractive syntax for short
SArray
literals to make removing the multi-argument constructors (as suggested by @timholy in #518 (comment)) less of a bitter pill to swallow.Some examples:
I've spent some time worrying about the fact that this is technically this is a syntax pun: the result of the
SA[...]
depends on whether the nameSA
is bound toStaticArrays.SA
or is local variable of some other type. However I've come to the conclusion that this is no worse than Int[1,2,3], which is also a pun on the array indexing syntax. Cases where this syntax could refer to something other thanStaticArrays.SA
are detectable by the compiler and should be accessible to a linter as well.Possible extensions / todo:
SA
parametric soSA{Float64}[1,2]
works?SA(x)
return a zero dimensionalSArray
?@SA
(Inconsistencies with multi-argument constructors #518 (comment)) might be better. This could do more, though we might be better to just keep@SArray
for those extra functionalities.