Replies: 15 comments
-
This would truly make collections first class citizens, dare I say it, just like Fortran ;-) |
Beta Was this translation helpful? Give feedback.
-
That's the sort of behaviour I'd expect shapes and extensions to be able to implement. public extension AddableEnumerable of IEnumerable<T> where T : Addable
{
public T[] operator +(T left, IEnumerable<T> right) => right.Select(x => x + left).ToArray();
} etc etc |
Beta Was this translation helpful? Give feedback.
-
Have a read of the range feature, which might see the light of day with v7.3. |
Beta Was this translation helpful? Give feedback.
-
@DarthVella The point is that the programmer wouldn't have to write any extra code to operate with collections. If the a type has an operator, then the collection of the type should have it also.
and later in code
|
Beta Was this translation helpful? Give feedback.
-
@DavidArno The range feature was the inspiration for this issue. I can see how auto implemented operators work to enhance ranges. It is also how |
Beta Was this translation helpful? Give feedback.
-
I disagree. The container is not it's elements and the nature of the behavior of the operator on the container would be expected to be different from on its elements. For example, why would For your original example of getting a range in an array, C# is introducing slices (or spans) and they're looking to implement a range operator which can be used in indexers. That will solve that need nicely. |
Beta Was this translation helpful? Give feedback.
-
Essentially this is just syntactic sugar, replacing the operator with the appropriate LINQ statement |
Beta Was this translation helpful? Give feedback.
-
I argue that it's unnecessary. For as infrequent as something like this would come up you already have a relatively quick and easy way to accomplish it via LINQ which puts much more control in the hands on the programmer. |
Beta Was this translation helpful? Give feedback.
-
@HaloFour in the area of scientific computing this comes up all the time. Possibly in the area of Graphics also (like operating on collections of PS. Thank you for looking into this. |
Beta Was this translation helpful? Give feedback.
-
You can always write your own collection class which does implement the overloaded operators and use that. Once and done. Could even be in a NuGet package. There's no reason to do that down at the language level. |
Beta Was this translation helpful? Give feedback.
-
You would not want operators working this way in graphics. Your example: |
Beta Was this translation helpful? Give feedback.
-
I know I can write my own (which I have for every custom type I design). The point being that it is automatic and I would never have to write yet another silly wrapper code that takes a collection and operates on each element. Lets free the user from boilerplate code. Make collections a first class citizen and use the existing design of classes to reduce unnecessary code. |
Beta Was this translation helpful? Give feedback.
-
I've done this sort of computing. In those specific cases, we simply did this: struct MySpecialVector {
private readonly double[] values;
public double this[int i] => values[i];
public static MySpecialVector operator ...
} Now we can define all the semantics we want precisely, without having inefficient implementations supplied by having these operators translate out to Linq calls. Linq would be absolutely a non-started in the scientific and graphics spaces due to the allocations and overhead. |
Beta Was this translation helpful? Give feedback.
-
This is really hard to do due to the different domain requirements that people have. Especially the people that want to operate over collections in a numeric manner. First, this is a non-starter for any sort of operation that would cause an allocation. No one is going to apply an operator over their million element collection if it allocates another million elements. :) |
Beta Was this translation helpful? Give feedback.
-
I want to second Cyrus. Wrapping primitive concepts like ints and arrays in a struct to endow special semantics of validation or behavior is something that has paid off for me. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
If an operator is defined for a type to automatically define it for an
IEnumerable
of that type. Now you can only fly manipulate collections in predefined ways (like negating all numbers) without having to explicitly write out.Select()
or.Zip()
every time.To negate the first 10 items of an array you now need
vs
Which one is clearer to the reader?
Auto implemented operators should work as follows:
.Select( x=> -x )
).Zip( (x,y)=> x-y )
).Select (x=> x-a)
)So off the bat, users can start manipulating collections of integers, string, & floats without any additional coding effort. To manipulate collections of custom types, obviously the appropriate operator should be declared in the type.
Beta Was this translation helpful? Give feedback.
All reactions