-
Notifications
You must be signed in to change notification settings - Fork 20
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
Allowable FSA subtype parameters and similar() #116
Comments
That seems pretty reasonable! :) We should rename it to |
Althoooough, this seems to work on 0.5: Base.@pure similar_type(::Type{Coordinates}, ::Type{Float64}, sz::Tuple{Int}) = sz[1] == 3 ? Coordinates : Vec{sz[1],Float64}
function test{N}(a, b::Vec{N, Int})
map(identity, similar_type(Coordinates, Float64, (N,)))
end
@code_typed(test(a, Vec{3,Int}(2)))[1].rettype == Coordinates
@code_typed(test(a, Vec{4,Int}(2)))[1].rettype == FixedSizeArrays.Vec{4,Float64}
# this doesn't work though (but Val doesn't seem to help either)
function test(a, b)
similar_type(Vec, length(b), Int)(a[1], b[2], a[3])
end The last case could possibly work, but doesn't right now because something goes wrong somewhere else?! Not sure, needs investigation! |
Heh, I did think the pun on Regarding downsides of Oh, one other thing. I did think that |
Indeed, +1 for I also agree sorting out these problems targeting Julia 0.5 is probably the smart move. Also, note that: Base.similar{T,SZ}(::Type{Coordinates}, ::Type{T}, ::Type{Val{SZ}}) = Vec{SZ[1],T} # Hmm, how do we restrict SZ in the function signature? Ugh! is not type-inferred. Inference bails whenever you try and do anything to a type parameter (and I really mean ANYTHING, like getting the first index of a tuple). You have to insert the value as a literal in a @generated function Base.similar{T,SZ}(::Type{Coordinates}, ::Type{T}, ::Type{Val{SZ}})
:(Vec{$(SZ[1]),T})
end This also opens up the possibility of capturing different dimensionalities of Though, the real fix would be to investigate Julia's base/inference.jl and see if we can get The other thing that would be helpful is if Julia let you constrain the types of parametric values not just parametric types in method signatures. |
Thanks for the thoughts guys. Considering I've also spent far too much time worrying about what the default
At this stage I'm going to try for 2. and we can see how it looks. |
I think #118 sorts this out to the extent it can be for now. |
With #105 merged,
similar()
now plays a central role in deciding the FSA type which is returned from mapping operations. This means that some FSA subtypes which kind of worked before now really don't work without overloadingsimilar()
. For example, theCoordinates
type in #104.Looking at the implementations of
similar()
the problem is fairly obvious - it's assumed that the type parameters of FixedVector subtypes will always look likeMyFSA{N,T, ...}
. Personally I'm feeling like this is too constraining, and I think it's reasonable for users to want toN
forFixedVector
(to 3 for three dimensions, say).N
forFixedVectorNoTuple
, (ok, this is more or less inherently fixed :-) )T
to something specific for their problem domain.Supporting all these needs automatically with the default implementation of
similar()
seems impossible, so I'd like to makesimilar()
something users must overload in general, and discuss how we can have the most sensible default implementation.If users are allowed to fix
N
andT
for their subtype, they must have a way to produce an alternative container for the output ofmap
whenmap
produces incompatibleN
orT
. Luckily overloadingsimilar()
seems a nice way to do this. Consider:With the above definitions, we can get a sensible container of
Bool
out of.<
even thoughCoordinates
is fixed toFloat64
:Unfortunately the definitions of
similar()
above are a bit nasty with the branch on the size tuple meaning they are type unstable. To avoid this, I think we could do something with Val...Thoughts?
The text was updated successfully, but these errors were encountered: