-
-
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
RFC: Make Tuple types with defined lengths iterable #11547
Conversation
More abstract tuple types like `Tuple` or `NTuple` (without parameters) and vararg tuples remain without any iteration methods defined. I only discovered one minor confusion stemming from a definition for `eltype` for NTuples of all the same type. Since eltype works in the type domain, there is no way to tell whether `eltype{N,T}(::Type{NTuple{N,T}})` should be `T` or `Type{T}`. I have removed these definitions.
Yes please! It's crazy to have to use |
This seems sensible to me as well – @JeffBezanson, do you have a reason not to do this? |
Would be great to test, but even with #11242 |
Ah, and there will have to be special-casing for iterating over julia> collect(Tuple{Vararg{Int,3}})
3-element Array{Any,1}:
Int64
Int64
Int64
julia> collect(Tuple{String,Vararg{Int,3}})
4-element Array{Any,1}: # the length is correct
AbstractString
Vararg{Int64,3}
#undef
#undef |
There shouldn't be any need to remove |
The trouble is that we define |
Well, that definition only applies to non-Types. Anyway I have much more serious objections to this. This depends on This kind of approach will increasingly be the wrong way to operate on types. This is basically the same situation that we have in arithmetic and linear algebra, where it was proposed to add definitions like
It should be clear how that does not scale. Put differently, it's not such a big win to provide only iteration. Many use cases want indexing, or concatenation, etc. |
Alright, see my take 2. All computations are done in the type domain now, and I've eliminated the funky dispatch on I disagree that iteration isn't a big win. Right now it's very difficult for users to dig into the contents of What are your thoughts on merging an implementation like this? |
This is better than the last version, though the staged functions don't seem to be necessary. I just still find it problematic to have types implement value-level interfaces. This also has the problem described in issue #11164. |
FWIW, I also dislike that Enum types are iterable. I'd rather have something like |
Alright. So, what's the path forward here? Refactoring the interface to |
Well, |
The "good" thing about .parameters is that at least you have this feeling that you have to be careful because you're touching internal representation, which is the case. Manipulating tuple types correctly is hard for anything but fixed length leaf ones. |
That's precisely what I want to fix! It seems very valuable to have some sort of blessed method that only works in well-defined scenarios (and has sensible error messages otherwise)… especially as the internal representations become more complicated with The behavior of Tuple types is well-defined, and its type parameters are part of that well-defined external interface. "Go digging around in .parameters and sort it out yourself" is a very unsatisfactory resolution here. |
FWIW, I agree totally with @mbauman. Having people play around with internal representation all over the place goes against everything I ever learned about good software engineering practices. What's worse, I believe that with Julia, it doesn't even gain you any performance benefits. |
I have no problem with wrapping this in an API, e.g. |
👍 to @JeffBezanson last comment, (IMO there are a lot of places in Julia where wrapping in well-designed API would be quite beneficial, and not cost any performance because of the great way Julia works) |
Ah, good. That's what I was after. Closing this in favor of a different API, then. |
For those following along at home, I've created a test-bed repository to hash out the interface at TupleTypes.jl. This doesn't need to be defined in Base to work, but I think it'd be good to eventually move it back in since I think it should be an official API. |
More abstract tuple types like
Tuple
orNTuple
(without parameters) and vararg tuples remain without any iteration methods defined.This is something I'd really like to see, but I understand that it may be contentious. For the most part this is fairly straightforward, but I did discover one minor confusion stemming from a definition for
eltype
for NTuples of all the same type. Since eltype works in the type domain, there is no way to tell whethereltype{N,T}(::Type{NTuple{N,T}})
should beT
orType{T}
. I have removed these definitions. This broke one unrelated test, sincecollect((1,2,3))
is nowAny[1,2,3]
, and indexing with anAny
array isn't implemented.