Support Construct methods. opposite of Deconstruct. #941
Replies: 8 comments
-
We already have constructors and I dislike hiding the |
Beta Was this translation helpful? Give feedback.
-
I prefer this approach: #100 |
Beta Was this translation helpful? Give feedback.
-
Thats calling Parameter less constructor. Its not equivalent. In this approach you construct a type from tuple. You could also have additional logics inside construct method. Why new statement is necessary. This adds syntactic sugar, i liked it btw @jnm2 |
Beta Was this translation helpful? Give feedback.
-
@MkazemAkhgary I believe #100 proposal covers parameters, as in There's no real reason to require a tuple instead of making the parameters part of the argument list, like there would be for returning multiple values. Nor do deconstructors need them; out parameters are good enough. I'd say anything a "Construct" method can do, a constructor can do more idiomatically. You're clear about the fact that you're creating new object instances. |
Beta Was this translation helpful? Give feedback.
-
So concrete examples: IMO While tuple enumeration isn't terribly common yet, I think showing the foreach (var t in source)
{
var x = new MyType(t.first, t.second);
// ...
} Perhaps you'd be interested in tuple splatting so that you don't have to write |
Beta Was this translation helpful? Give feedback.
-
Extension everything + implicit operators would probably cover this. |
Beta Was this translation helpful? Give feedback.
-
I used a tuple enumerator in my collection since its not simple list. Yes my initial thought was its usage in foreach but then i expanded it into tuple construction thing. I see, new() with parameters is definitely acceptable. @jnm2 |
Beta Was this translation helpful? Give feedback.
-
That's never a good argument for a new feature |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I found deconstruct often useful in foreach loops.
The opposite could be also useful sometimes. that is going from tuple to single type. why not?
Tuple argument in this method could be generic, how ever out argument needs to be more specific, obviously when you have to initialize result and possibly by passing argument to constructor, however this is just something to keep in mind.
Another more useful example
A construct can also be a
instancemethod which could be used instead of calling constructor directly. you would set tuple like this.Beta Was this translation helpful? Give feedback.
All reactions