Replies: 19 comments
-
I find this confusing, because it would allow every constructor to be used in this way. I prefer the current way to use an object factory by an explicit static (op-. extension) method named |
Beta Was this translation helpful? Give feedback.
-
Isn't that the entire purpose of this proposal/request? |
Beta Was this translation helpful? Give feedback.
-
Constructors cannot be bound to a delegate. edit: to clarify, there is a CLR restriction in place. |
Beta Was this translation helpful? Give feedback.
-
This could be accomplished via syntax candy, though: Func<MyClass> func = MyClass::new; // using Java method reference syntax
// compiled into
Func<MyClass> func = () => new MyClass(); |
Beta Was this translation helpful? Give feedback.
-
That sucks, but I recall @CyrusNajmabadi saying in another thread that he would like to see this (even suggested And really, even if it was just syntax sugar as HaloFour suggests, it'd be pretty great. |
Beta Was this translation helpful? Give feedback.
-
I would be fine with this if there is a clear thing this translates to in the CLR. And, specifically, we'd need a solution that could ensure the following would work properly: x.SomeEvent += SomeType.new;
...
x.SomeEvent -= SomeType.new; |
Beta Was this translation helpful? Give feedback.
-
Where should the result of 'SomeType.new', namely the newly created object, go? |
Beta Was this translation helpful? Give feedback.
-
@lachbaer The result is returned from the invocation of the delegate which is backing the event. With multicast delegates, that's the value of either the last or the first invocation (I forget). |
Beta Was this translation helpful? Give feedback.
-
I'll also propose this syntax, for the sake of familiarity: Func<MyClass> f = new MyClass; |
Beta Was this translation helpful? Give feedback.
-
@HaloFour how would you avoid the cross assembly issues? |
Beta Was this translation helpful? Give feedback.
-
@GeirGrusom What cross-assembly issues? |
Beta Was this translation helpful? Give feedback.
-
new Func<string, string>(String.Copy) == new Func<string, string>(String.Copy) This evaluates to true today. Imagine if the right hand was done in a different assembly (and it was a constructor instead). It would suddenly evaluate to |
Beta Was this translation helpful? Give feedback.
-
True, the wrapping with a lambda would introduce that problem. But that's the only way that this feature can be introduced with only language changes. Constructors are instance While the event subscription situation does exist I'd wager that it would be exceptionally rare. Constructors don't fit in the normal convention for defining events, and events that return values, while technically legal in C# (not in VB.NET), are non-deterministic in multiple subscription scenarios. |
Beta Was this translation helpful? Give feedback.
-
Or CLR support. |
Beta Was this translation helpful? Give feedback.
-
I like @bondsbw's syntax more, if possible. |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
I was about to open an issue to suggest this feature. I would also favour @bondsbw's syntax, it is in keeping with both current constructor syntax and method group syntax. I was surprised to find constructors not supported as method groups, something like the following feels like it should be supported C#: public class StringWrapper
{
public string Value { get; }
public StringWrapper(string value) => Value = value;
}
var strings = new string[] { "foo", "bar" };
var wrappers = strings.Select(new StringWrapper); |
Beta Was this translation helpful? Give feedback.
-
I would welcome this feature in C#, because
From what I have seen so far, I would prefer @bondsw's syntax. |
Beta Was this translation helpful? Give feedback.
-
I think I would prefer I think the only outstanding question would be overload resultion in case of multiple constructors, but that should be something that can be handled just like
Edit: @333fred points out that |
Beta Was this translation helpful? Give feedback.
-
Add the ability to use constructors as method groups when creating delegates.
This:
Func<object> f = Object.new;
would be similar to this:
Func<object> f = Object.StaticMethod;
as this:
Func<object> f = () => new object();
is similar to this:
Func<object> f = () => Object.StaticMethod();
A constructor used as a method group when creating a delegate will be act like using a static method group that returns the object that is being constructed when compiling.
Beta Was this translation helpful? Give feedback.
All reactions