-
Notifications
You must be signed in to change notification settings - Fork 149
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Named access with .{x,y,z,w} to elements of SVector and MVector #980
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I can see how this could be convenient for using these as points, and as you say there isn’t any real downside.
src/MVector.jl
Outdated
|
||
# Named field access for the first three elements with the conventional | ||
# field names | ||
function Base.getproperty(v::Union{MVector}, name::Symbol) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unnecessary Union
src/SVector.jl
Outdated
|
||
# Named field access for the first three elements with the conventional | ||
# field names | ||
function Base.getproperty(v::Union{SVector}, name::Symbol) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unnecessary Union
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Heh oops, thanks!
The reason this was there was that I had this defined as Union{SVector,MVector}
because the implementations are the same.
But then I didn't know which file to put these in so I duplicated them instead as they're a concrete interface. Kind of a stupid reason to have duplicate code though I guess...
Perhaps I should also add |
@@ -36,10 +36,6 @@ const SVector{S, T} = SArray{Tuple{S}, T, 1, S} | |||
## SVector methods ## | |||
##################### | |||
|
|||
@propagate_inbounds function getindex(v::SVector, i::Int) | |||
v.data[i] | |||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Haha, this redundant method has been in here for years. I removed it in favour of the linear getindex from SArray
Absent objections, I'll probably merge this some time tomorrow or so. It's a neat little feature and should be harmless to anyone who hasn't been involved in type piracy :-) |
Perhaps this should also include |
It is also used by the Eigen C++ library. |
I agree I initially decided against |
Would it make sense to limit this to length-3 vectors ( |
Maybe throw @inbounds in there? I think that made a significant speed-up in my toy raytracing implementation, and otherwise people might not think to add it up, i.e. might not realize that .x/.y translates into array look-up... |
I don't know! The question is, could it lead to unexpected bugs in other cases? You know what I think I agree: let's be strict here and relax the rules later if we think it makes sense. It's much easier to add API than to remove it!
Adding |
Ok thanks for the feedback everyone. I've now updated this as follows:
|
Side note... gosh our docs are pretty awful 😅 Not something for this PR though. |
Having access with v.x etc makes the *concrete* SVector interface much more usable for geometry; indeed, it will "just work like you expect" without the need to resort to a custom FieldVector type. This makes the concrete SVector/MVector interface fatter, but I don't think it can do any harm to people who don't want to use it.
4ece173
to
8ef637f
Compare
Having access with v.x etc makes the concrete SVector interface much
more usable for geometry; indeed, it will "just work like you expect"
without the need to resort to a custom FieldVector type.
This makes the concrete SVector/MVector interface fatter, but OTOH I
don't think it can do any harm to people who don't want to use it.
If we do this, it should be made to work at least for SVector{1}
SVector{2} and SVector{3}, but I saw no reason to disallow the use of
these in higher dimensions and overcomplicate the implementation.
Inspired by discussion at
https://discourse.julialang.org/t/ray-tracing-in-a-week-end-julia-vs-simd-optimized-c/72958
I'll add tests and some docs if we decide this is a good idea!