Design thread for from end slicing #472
Replies: 3 comments 1 reply
-
@TIHan, @cartermp and @dsyme had a design discussion today about from end slicing in F# (currently in preview). Design notes are below. Context: There is a dissonance between the C# 9.0 design for slicing and indexing and the existing F# support. F# support for multi-dimensional indexing and slicing looks like this: // F# pattern
type Tensor with
// 1D
member t.Item(i:int) =
member t.GetSlice(i0min:int option, i0max:int option) =
// 2D
// xs.[a, b]
member t.Item(i:int, j:int) =
// xs.[a..b, c..d]
member t.GetSlice(i0min:int option, i0max:int option, i1min:int option, i1max:int option) =
// xs.[a..b, c]
member t.GetSlice(i0min:int option, i0max:int option, i1:int) =
// xs.[a, b..c]
member t.GetSlice(i0:int, i1min:int option, i1max:int option) = The F# support has problems:
Here we address 4, and coincidentally 3. The F# proposal is to adjust FS-1076 as follows:
Note that in the above Note this only solve problem 3 and 4. Note these equivalences:
In future F# pattern becomes same as C# pattern: type Tensor with
// 1D
member t.Item(i:int) =
member t.Item(i:Index) =
member t.Item(i:Range) =
// 2D
member t.Item(i:int, j:int) =
member t.Item(i:Index, j:Index) = ...
member t.Item(i:Index, j:Range) = ...
member t.Item(i:Range, j:Index) = ...
member t.Item(i:Range, j:Range) = ...
etc. Note it is possible to add from-end-indexing-and-slicing to 1D F# types like type List<T> with
member t.Item(i:int) =
member t.Item(i:Index) = // new
member t.GetSlice(i0s:Range) = // new
member t.GetSlice(startIndex:int option, endIndex:int option) -> 'T list |
Beta Was this translation helpful? Give feedback.
-
There are issues with the above design change which I noticed while writing up the notes It requires overloading the
you will need a type annotation of Also, the F# programmer might reasonably want to keep the slicing methods separately named to the That is there is question about whether we support the following for F# coding:
|
Beta Was this translation helpful? Give feedback.
-
Can't we use something like trait-calls defaults to make indexing operations default to int in absence of type annotations and evidence from usage ? |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
All reactions