Make the use of new
be optional when it can be inferred that a new object is being created
#7708
Replies: 4 comments 23 replies
-
this sounds like a good case for SGs. Add such an implicit operator to the type if non-existent :) |
Beta Was this translation helpful? Give feedback.
-
This is something I've thought about when designing my imaginary language. The main downside is that it is really easy to obscure the code. It looks nice in these small very concrete examples but reading something like In the end my personal preference is And with shapes I think you might be able to define your own |
Beta Was this translation helpful? Give feedback.
-
Just, why? IMO, |
Beta Was this translation helpful? Give feedback.
-
I think we would be unlikely to do anything here as the feature is already syntactically and semantically legal c# in the presence of implicit operators. Imo, if you want tuples to legally convert to your types, then opt into that with such an operator. And, if you want that for all your types, write a generator that adds such an operator for you. |
Beta Was this translation helpful? Give feedback.
-
In most use cases, it's clear when a new object is being created. Traditionally, creating new instances of a type has been like:
The addition of
var
made this a bit more simple:And then the ability to omit the type itself as part of the construction call if the type could be inferred:
Inspired a bit by the new collection initializer syntax, I'd like to see this taken a step further, and make something like this possible:
The idea is that, you don't really need to use
new
in this case. It's clear from use thata
is being a assigned to a new instance of MyType. Similar to how justnew()
will throw a compiler error if the type isn't declared, this would not be a valid type instance creation and instead would "fall back" to assuming a is a Tuple<T1,T2,T2>:The tuple question is probably the biggest here, especially with the ability to achieve similar behavior through
static implicit operator
to add in the ability to treat use a tuple in place of object creation. I do this below:In a case where something like this is defined, the compiler would use the user defined
implicit operator
. However, if this isn't defined, it would attempt to use a constructor that matches the signature. If no valid ctor signature is found, there would be a compiler error (as it is trying to assign a tuple to an object):If an implicit ctor is defined, I could see there being some warning as well that it's using the operator instead of the ctor.
I think the argument against this is maybe mostly a philosophical one — being able to use
new
is a more obvious signal of an allocation, but it's also not hard to understand that creating a new object is an implicit allocation already, with or without anew
. I think if the idea is received well enough but maybe too radical, I could also see hiding it behind some compiler flag like .For backwards compatibility, I can't think of any cases where this would break existing code. Given the above, is a user defined
implicit operator
exists, the compiler would fall back on that and use the same behavior.As a tease, it's also just really nice to see code like this possible (leveraging latest c# features and my proposal):
Would love to know what people think, and thanks for reading!
EDIT: Just so it's clear, as I mentioned it in the title but not in the actual body of the proposal, the idea is not to get rid of
new
. Only to make it optional when its use can be inferred.Beta Was this translation helpful? Give feedback.
All reactions