Unexpected (to me) Array behavior

I don’t think the situation is quite as extreme as you are making out. Lots of basic syntax returns new types in all kinds of languages. So let’s restrict ourselves to slicing. Go and Rust both pervasively return new types on slicing. Python and most interpreted languages allow it too, though it is uncommon. Broadly speaking, languages that fall into the camp of both “strongly typed” and “not focused on immutability” tend to have this behaviour.

3 Likes

I'm pretty sure slicing syntax is a (relatively) recent invention. I don't think you can get anywhere near as concise with C, C# (Ok, maybe 8.0), or Ruby. Ones in my recent memory would be Go, Kotlin, and Python. Even Python refuses to call whatever they're slicing array.

Perl allows a programmer to define the starting index for all arrays. Pascal allows the programmer to define the valid range of indexes on a per-array basis (fixed size), and the range need not start with zero.

And those are arrays, not types that look like arrays.

1 Like

Perl has learned its lesson

As of Perl v5.30.0, or under "use v5.16", or "no feature
"array_base"", $[ no longer has any effect, and always contains
0. Assigning 0 to it is permitted, but any other value will
produce an error.

This is not the first, nor the last thread, let me link this here [Rant] Indexing into ArraySlice

1 Like

Before diving in. In there are

  • The offset camp that thinks every collection should index like array,
    • startIndex will just bloat the code base and easy to forget,
  • The index camp the think it’s better to truly reflect how collection works inside,
    • Offset-based would have hidden perf impact, and that you should actually think with index, not offset,
  • Someone propose offset design, and the discussion evolves into into discussion about that design.

Lemme see how right I am. This’s been rehashed enough that I’m pretty sure this is accurate. Oh, and the op is vehemently in the offset camp to boot

There is an obvious design deficiency in Swift that breaks people's expectations, and leads them to this forum in frustration.

There is a "camp" that learnt model for array and array slice from other languages. Swift on the first glance adheres to that model. And continues mimicking that model, while underlying concepts are different. Then Swift takes little to no effort to transition user from established model: the first wtf moment is a runtime error. And then user is left with no support until the next runtime error.

Shouldn't Slice provide two insecies properties and two related subscripts to remove the confusion? It could be like

var baseInheritedIndicies: Base.Indicies
var selfIndicies: Indicies
subscript(baseIndex:)
subscript(selfIndex:)

While I understand slices (and know about indices and offsets), I'm still surprised by this example.
I believe my surprise comes from the fact that I assumed that a single slice can only be a subset of a single Array, but in this case a single slice has references to two distinct arrays?

2 Likes

The returned slice (combo2) doesn't slice any array, it must be newly allocated.

It seems to me that ideally this + operator shouldn't have been defined to allow returning ArraySlice. However, since it had to work with both Array and ContiguousArray, it was defined as returning Self, where Self is the first argument. Weirdness ensues.

4 Likes
Terms of Service

Privacy Policy

Cookie Policy