Skip to content

Remove eltype parameter from AbstractArray and use IteratorEltype instead #39626

@bramtayl

Description

@bramtayl

Background:

There seems to be a preponderance of theoretical AbstractArrays (roughly, a bunch of things arranged in a hypercube) without eltypes:

The reason these things don't have an eltype is fundamentally because Julia is a dynamic language and return types aren't really supposed to exist. That is, you can't really know the type of the result of applying a function (like the mapping function, or view for array slices) until you actual do it.

The problem is, none of the the theoretical AbstractArrays can inherit any of the methods defined for AbstractArrays, which means they either have to duplicate a whole bunch of machinery (like the broadcasted objects) or just not have the same super powers (like the Iterator.maps). I've been thinking about this for a while; see #34196 and #35095 for context.

I really like @andyferris 's idea in #32310 (comment) as a simple solution. The idea if I understand correctly is to remove the eltype type parameter from AbstractArray, and instead, unless you already know it exists, access the eltype only after having checked the IteratorEltype trait first.

The advantages I see are:

  • Doesn't require really dramatic changes to the language; after all, we already have and use IteratorEltype, this would just make it more consistent.
  • Will either remove the need for duplicate code or add new powers to existing things
  • Could facilitate laziness (like, instead of allocating a broadcast, return a fully featured lazy broadcast instead)

The disadvantages I see are:

  • Will require some new types. We would need at least a MappedArray object, a SlicedArray object, and maybe a BroadcastedArray object. In comparison, a more thoroughly trait-based solution might not need as many new types.
  • Maybe a bit more inconvenient; to write generic methods for AbstractArrays, you might have to check IteratorEltype first.

I had a crack at messing around with the code in src to make a PR and realized pretty quickly I was out of my depth because I don't know much C (and obtuse abbreviated names don't help much). If someone figures that out though, I could do the Julia parts (updating Julia code and tests). I suppose this is likely too big of a change for 1.0?

Sorry if I wrote too much...I figure too much is better than too little though.

Metadata

Metadata

Assignees

No one assigned

    Labels

    arrays[a, r, r, a, y, s]breakingThis change will break codedesignDesign of APIs or of the language itselfneeds decisionA decision on this change is neededspeculativeWhether the change will be implemented is speculativetraitsTraits, sometimes called Tim Holy Traits

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions