Skip to content

Multiple Assignment Workaround #19

@shinzlet

Description

@shinzlet

In previous versions of Crystal, Phase could do:

narr = NArray[[1, 2, 3], [4, 5, 6]]
a, b = narr # this becomes a = narr[0], b = narr[1], which are implicitly equivalent to a = narr[0, ..], b = narr[1, ..]
a # => NArray[1, 2, 3]
b # => NArray[4, 5, 6]

Which was pretty handy!

In crystal PR #11545, multiple assignment was restricted to only allow Indexable on the RHS. I don't believe that MultiIndexable could possibly be turned into an instance of Indexable, as one-dimensionality is kind of baked into the interface.

I think that restricting the RHS to Indexable was a slight misnomer, for what it's worth - Indexable has a lot more baggage than just saying "this is a container". After exploring this some more, I might open an issue or PR on the crystal stdlib to work around this.

(Possibly a new module, e.g. MultiAssignable is needed that only requires unsafe_fetch and size to be defined, without forcing you to include dozens of 1d container methods?)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions