Proposal: Lambda expression set-only properties #1615
-
I know we almost never use set-only properties, and, because they have to be side effect driven, some people say that they should never be used and a method should be used instead. Regardless, here is an example of when I'd find it useful: public class Cube
{
public int X { get; private set; }
public int Y { get; private set; }
public int Z { get; private set; }
public int Volume => X * Y * Z;
// Current
public int Scale { set => X = Y = Z = value; }
// Proposal
public int Scale <= X = Y = Z = value;
// Or
public int Scale <- X = Y = Z = value;
} Ideally it would be |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments
-
Hmm, like you said, That being said, I find both of your proposed symbols
|
Beta Was this translation helpful? Give feedback.
-
@stakx I see where your coming from. I see At the same time, it isn't as necessary still as Foo { get { return foo = value; } } // only setting foo here to make an equal comparision
Foo => foo = value; // nearly half the size
Foo { set { foo = value; } }
Foo => foo = value; // not as big of an improvement Also, IIRC, I came across an instance the other day when I could've used a set-only, but then I found a better way to do it. Regardless, it's a feature I wouldn't mind. |
Beta Was this translation helpful? Give feedback.
-
Does the existing syntax ( |
Beta Was this translation helpful? Give feedback.
-
@jnm2 Not really, which is why I'm 😕over this myself. Maybe if we could remove the brackets to be cleaner |
Beta Was this translation helpful? Give feedback.
Does the existing syntax (
public int Scale { set => X = Y = Z = value; }
) seem burdensome for such a rare scenario as this? I would have said it was clearer than the proposed syntax.