Feedback on "Property pattern" discussion #1522
-
In https://github.com/dotnet/csharplang/blob/master/meetings/2018/LDM-2018-04-30.md property patterns are discussed. I personally prefer the I think pattern matching should look like named destructuring (hope that this will be in scope):
The matching also has some destructuring inside it, as shown in the Future of C# talk on Build 2018. |
Beta Was this translation helpful? Give feedback.
Replies: 9 comments
-
What should happen when In ES we can provide a default value, I don't know if that'd be useful in C#. |
Beta Was this translation helpful? Give feedback.
-
IMHO deconstruction like this only really belongs in the context of pattern matching. I'd think that the ratio of scenarios in which the compiler could guarantee that the deconstruction would succeed would be relatively low. |
Beta Was this translation helpful? Give feedback.
-
Thanks for you feedback, I knew it would be a bad idea 😉 |
Beta Was this translation helpful? Give feedback.
-
@alrz Doesn't the problem of |
Beta Was this translation helpful? Give feedback.
-
No, because those are value types and cannot be null. see you can't deconstruct a |
Beta Was this translation helpful? Give feedback.
-
Note that if you define I suggest you keep this open because imo it might come in handy actually if you don't have a Deconstruct. -- PS: I think this could be a fine alternative to separate assignments sometimes, var [a, b] = array;
var a = array[0];
var b = array[1]; var [.., last] = array;
var last = array[^1]; var { Name: name, Age: age } = GetPerson();
var p = GetPerson();
var name = p.Name;
var age = p.Age; var {[key1]: value1, [key2]: value2 } = GetDictionary();
var d = GetDictionary();
var value1 = d[key1];
var value2 = d[key2]; Note: the lhs is not a pattern perse, it's just an extended deconstruction syntax i.e we're not "matching" we're deconstructing. so no patterns (like constants) should be allowed instead of names (just like ES). See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment |
Beta Was this translation helpful? Give feedback.
-
Quite old issue but since it's in "Discussion" I'll add my two cents.
I'm mostly familiar with pattern matching in F# and there the syntax shows much cleared symmetry between construction and deconstruction: type Bar = {
PropBar: int
}
type Foo = {
Prop1: string;
Bar: Bar
}
let foo1 = { Prop1 = "test1"; Bar = { PropBar = 1} }
let matchTest1 =
match bar with
| { Prop1 = "test1" } -> "test"
| { Prop1 = "test2"; Bar = { PropBar = b} } -> "test_" + (string b)
| _ -> "default" just notice that construction of let { Bar = { PropBar = extractedPropBar }} = foo1
printfn "%d" extractedPropBar However when we code in C# this doesn't look that similar: public class Bar
{
public int PropBar { get; set; }
public Point Position { get; set; }
}
public class Foo
{
public string Prop1 { get; set; }
public Bar Bar { get; set; }
}
public class Point
{
public int X { get; }
public int Y { get; }
public Point(int x, int y) => (X, Y) = (x, y);
public void Deconstruct(out int x, out int y) => (x, y) = (X, Y);
}
...
var foo1 = new Foo { Prop1 = "test1", Bar = new Bar { PropBar = 1, Position = new Point(2, 4) } };
var foo2 = new Foo { Prop1 = "test1", Bar = new Bar { PropBar = 2 } };
object foo2obj = new Foo { Prop1 = "test5", Bar = new Bar { PropBar = 5 } };
var testMatch = foo1 switch
{
{ Prop1: "test1", Bar: { PropBar: 1, Position: (2, 3) } } => $"test_{1}_p_",
Foo { Prop1: "test1", Bar: Bar { PropBar: 1, Position: Point(2, 4) } } => $"test_{1}_p_",
{ Prop1: "test1", Bar: { PropBar: 1, Position: (_, var y) } } => $"test_{1}_py_{y}",
{ Prop1: "test1", Bar: { PropBar: var b } } => $"test_{b}",
_ => "default"
}; I really like the current syntax for pattern matching and I think it should stay like that. But it would be great if in future we could get new syntax for constructing object that would use Foo foo1a = { Prop1: "test1", Bar: { PropBar: 1, Position: (2, 4) } };
var foo1b = Foo { Prop1: "test1", Bar: Bar { PropBar: 1, Position: Point(2, 4) } }; The
|
Beta Was this translation helpful? Give feedback.
-
Are there any discussions going on about this proposal? Or any update? The original proposal for:
in the case where "Employer" is null can return the default value for the type of "ID" (it can be null if it is The idea for arrays is also helpful:
This might be trickier when the array has only 1 element - using a[1] throws an exception, so probably this should throw as well. |
Beta Was this translation helpful? Give feedback.
-
Recursive property patterns are shipping in C# 8.0 which is feature complete. The pattern you specified for |
Beta Was this translation helpful? Give feedback.
@Stamo-Gochev
Recursive property patterns are shipping in C# 8.0 which is feature complete. The pattern you specified for
Employee
will only match if that property is notnull
, butID
can benull
, depending on the data type.