-
Notifications
You must be signed in to change notification settings - Fork 344
Consider renaming Span<T> to Array<T> #633
Comments
cc: @terrajobst, @cartermp |
Wow! That's a change! So there will be abstract |
I guess this is the implication. For the context, I filed the issue because:
|
👍 |
Combined with full language and JIT support slices could (almost) completely replace usage of arrays. At least in the new code. int[:] indexes = new int[10]; So the name looks appropriate. |
Indeed, if such support is added and if new language features ( |
Since I doubt arrays are ever going away, I don't think To contrast, similar situation worked out well for generic collections: it's pretty clear that "queue" generally refers to It may be too long, but what about |
I think the end goal here is that you shouldn't really have to be asking that question in the first place. In Go or Rust, for example, you create arrays at the top level, but the vast majority of meaningful work you do is on a slice of that array (even if the slice happens to be the whole thing). The way they are reasoned about isn't as this other thing that is backed by an array, but the chunk of the array I care about. Rust ex: fn print_size(slice: &[i32]) {
println!("slice has {} items", slice.len());
}
fn main() {
let xs: [i32; 5] = [1, 2, 3, 4, 5]; // fixed-size array of 5 elements
print_size(&xs); // prints 5
print_size(&xs[1 .. 4]); // prints 3
} When I write this kind of code I never ask which type is the array of ints. Although the semantics here are a bit different than typical C# code, the underlying reasoning I do doesn't involve distinguishing between types.
This is the current way that Swift handles things. Although I haven't written much Swift code, the impression I get is that the friction between |
I think it would be confusing since Array is a well established data structure going beyond .Net and does not only implies indexing but also always represents a complete view on the stored data. I think the data structure should be called In most cases we will not utter the actual type (like we rarely mention int[] data = new int[100];
// slice a range of 3 to 9 elements inclusively and pass that to Foo
Foo(data[3:9]);
// same as
int[:] range = data[3:9];
Foo(range);
// same as
Range<int> range = data.Slice(3, 9);
Foo(range); |
@KrzysztofCwalina side note: should the title be |
@cartermp, fixed. |
Is the restriction that a method cannot have the same name as it's enclosing type just a C# limitation or is that a CLR restriction? class ToString{}
delegate int Invoke();
Enum A{ A} I think slice is the best name. Can we just remove the restriction for this class? |
What about |
@jamesqo To me, |
I agree with @jamesqo. To me |
|
While that is likely true, I don't think we should rely on language features when naming APIs. As you know, there is a high chance that the language feature either doesn't happen or happens much later. I think as a best practice we should design our APIs without having to rely on language features to hide any quirkiness (not just naming). Of course, we should leverage the compiler and the languages to do things that are hard (such as creating state machines or doing reachability analysis) but not for putting lip stick on a pig. |
I would prefer If we're going to make the differentiation between slices and "sliceable" collections be conceptually distinct, then keeping "slice" in the name makes it easiest to understand without having to consult an API reference. |
@cartermp Perhaps we should name the normal version |
Also, maybe |
Range is not a bad idea. I would like a single word (and short) name for the type. Especially that we will also have to have ReadOnlyXxx version of it. |
My preference would be In general, it should not be assumed that the result of a slice operation is always a slice/
In my view, the ideal API would use indexer properties that take as an argument a lightweight structure (
|
I buy that which is why I believe having a single term is useful because we can combine it with other concepts or in other places where
Not sure what map the slicing syntax to the Slice type would mean if there is no syntax. In general, I like the idea of mapping the indexer syntax That being said, I wonder whether there shouldn't also be a syntax for the slice type. For instance, it would be nice to be able to declare a type using similar syntax, such as: int[] numbers = {1, 2, 3, 4, 5}
int[:] threeFourFive = array[2:3];
string text = "Hello";
string[:] el = text[1:2]; I see two options there:
In general, I think syntax matters a lot when it comes to how features are being perceived. |
I think that once you make the result of a slicing operation of arbitrary type, then a special syntax for the slice type is very close to useless and certainly not worth the trouble. Your first option is too restrictive, and in your second option it looks like
I mean that the expression
maps to
where you've defined:
The biggest problem when you don't use such an index type is that it doesn't work for multi-dimensional arrays. How do you differentiate between You can also use the SliceIndex syntax in loops if you want:
|
Just to add my 0.02, Slice as it stands is nothing more than ArraySegment for the most part. IList Adapter can already do this as well as signal changes to the underlying collection. Generic.IList misses that but you can easily add it. What happens when an array is resized? What if the sliced data no longer exists? |
Just as a FYI, I penned something out to deal with the Resizing issue among other things. The result is @ In my projects source It's not 100% complete yet as it doesn't deal with strings correctly. I also have an idea about not even copying memory by forging an CLR array header and placing it right before the offset of the first desired element which makes the array think it has X elements which start directly after the header... |
Lots of good discussion on this, but I don't think we have found the perfect name, so not worth renaming. |
Any thoughts on this from the community?
The text was updated successfully, but these errors were encountered: