Replies: 4 comments 2 replies
This comment has been hidden.
This comment has been hidden.
-
It's not just you, but I'd suggest that simply creating named constructors is a fine solution (named static methods on the type). A source generator to do this would be trivial. |
Beta Was this translation helpful? Give feedback.
0 replies
-
If |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Not entirely sure what to call this idea, but essentially I want to expose a syntax that allows for method references to the ctor of a type. Consider the following example:
There is no corresponding syntax for identifying a constructor for the
Bar
type, leaving consumers to usefoos.Select(it => new Bar(it));
. This feels like a use case where it can be shortened, similiar to howit => Bar.FromFoo(it)
can be shortened. However,foos.Select(new Bar)
doesn't really make much sense syntactically and ctors are handled differently from method calls in this case anyways.Syntax ideas:
Bar:new
orBar::new
as inSelect(Bar:new)
orSelect(Bar::new)
. This involves adding a new symbol and meaning and carries with it all of the complexities of adding new symbols.Bar.Bar
as inSelect(Bar.Bar)
. This could get cumbersome for longer type names.Bar.New()
method, possibly created via code generation. Used asSelect(Bar.New)
. Body would be weird to figure out for this one but one possible solution could be that it needs to find an equivalent ctor signature. In other words,New(string)
would need a correspondingBar(string)
constructor.Concerns:
nameof
or reflection.FromX()
methods for types just so I can have that visual congruity. But maybe that's just me.Beta Was this translation helpful? Give feedback.
All reactions