{"action":"created","issue":{"url":"https://api.github.com/repos/dotnet/roslyn/issues/10429","repository_url":"https://api.github.com/repos/dotnet/roslyn","labels_url":"https://api.github.com/repos/dotnet/roslyn/issues/10429/labels{/name}","comments_url":"https://api.github.com/repos/dotnet/roslyn/issues/10429/comments","events_url":"https://api.github.com/repos/dotnet/roslyn/issues/10429/events","html_url":"https://github.com/dotnet/roslyn/issues/10429","id":146994547,"number":10429,"title":"C# Design Notes for Apr 6, 2016","user":{"login":"MadsTorgersen","id":6677272,"avatar_url":"https://avatars.githubusercontent.com/u/6677272?v=3","gravatar_id":"","url":"https://api.github.com/users/MadsTorgersen","html_url":"https://github.com/MadsTorgersen","followers_url":"https://api.github.com/users/MadsTorgersen/followers","following_url":"https://api.github.com/users/MadsTorgersen/following{/other_user}","gists_url":"https://api.github.com/users/MadsTorgersen/gists{/gist_id}","starred_url":"https://api.github.com/users/MadsTorgersen/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/MadsTorgersen/subscriptions","organizations_url":"https://api.github.com/users/MadsTorgersen/orgs","repos_url":"https://api.github.com/users/MadsTorgersen/repos","events_url":"https://api.github.com/users/MadsTorgersen/events{/privacy}","received_events_url":"https://api.github.com/users/MadsTorgersen/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/dotnet/roslyn/labels/Area-Language%20Design","name":"Area-Language Design","color":"5319e7"},{"url":"https://api.github.com/repos/dotnet/roslyn/labels/Design%20Notes","name":"Design Notes","color":"fc2929"},{"url":"https://api.github.com/repos/dotnet/roslyn/labels/Language-C%23","name":"Language-C#","color":"207de5"},{"url":"https://api.github.com/repos/dotnet/roslyn/labels/New%20Language%20Feature%20-%20Pattern%20Matching","name":"New Language Feature - Pattern Matching","color":"bfd4f2"},{"url":"https://api.github.com/repos/dotnet/roslyn/labels/New%20Language%20Feature%20-%20Tuples","name":"New Language Feature - Tuples","color":"bfd4f2"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":86,"created_at":"2016-04-08T17:52:20Z","updated_at":"2016-04-17T20:38:57Z","closed_at":null,"body":"# C# Design Notes for Apr 6, 2016\r\n\r\nWe settled several open design questions concerning tuples and pattern matching.\r\n\r\n# Tuples\r\n\r\n## Identity conversion\r\n\r\nElement names are immaterial to tuple conversions. Tuples with the same types in the same order are identity convertible to each other, regardless of the names.\r\n\r\nThat said, if you have an element name at *one* position on one side of a conversion, and the same name at *another* position on the other side, you almost certainly have bug in your code:\r\n\r\n``` c#\r\n(string first, string last) GetNames() { ... }\r\n(string last, string first) names = GetNames(); // Oops!\r\n```\r\n\r\nTo catch this glaring case, we'll have a warning. In the unlikely case that you meant to do this, you can easily silence it e.g. by assigning through a tuple without names at all.\r\n\r\n## Boxing conversion\r\n\r\nAs structs, tuples naturally have a boxing conversion. Importantly, the names aren't part of the runtime representation of tuples, but are tracked only by the compiler. Thus, once you've \"cast away\" the names, you cannot recover them. In alignment with the identity conversions, a boxed tuple will unbox to any tuple type that has the same element types in the same order.\r\n\r\n## Target typing\r\n\r\nA tuple literal is \"target typed\" whenever possible. What that means is that the tuple literal has a \"conversion from expression\" to any tuple type, as long as the element expressions of the tuple literal have an implicit conversion to the element types of the tuple type.\r\n\r\n``` c#\r\n(string name, byte age) t = (null, 5); // Ok: the expressions null and 5 convert to string and byte\r\n```\r\n\r\nIn cases where the tuple literal is not part of a conversion, it acquires its \"natural type\", which means a tuple type where the element types are the types of the constituent expressions. Since not all expressions have types, not all tuple literals have a natural type either:\r\n\r\n``` c#\r\nvar t = (\"John\", 5); // Ok: the type of t is (string, int)\r\nvar t = (null, 5); //   Error: null doesn't have a type\r\n```\r\n\r\nA tuple literal may include names, in which case they become part of the natural type:\r\n\r\n``` c#\r\nvar t = (name: \"John\", age: 5); // The type of t is (string name, int age)\r\n```\r\n\r\n## Conversion propagation\r\n\r\nA harder question is whether tuple types should be convertible to each other based on conversions between their element types. Intuitively it seems that implicit and explicit conversions should just \"bleed through\" and compose to the tuples. This leads to a lot of complexity and hard questions, though. What kind of conversion is the tuple conversion? Different places in the language place different restrictions on which conversions can apply - those would have to be \"pushed down\" as well.\r\n\r\n``` c#\r\nvar t1 = (\"John\", 5);   // (string, int)\r\n(object, long) t2 = t1; // What kind of conversion is this? Where is it allowed\r\n```\r\n\r\nOn the whole we think that, while intuitive, the need for such conversions isn't actually that common. It's hard to construct an example that isn't contrived, involving for instance tuple-typed method parameters and the like. When you really need it, you can deconstruct the tuple and reconstruct it with a tuple literal, making use of target typing.\r\n\r\nWe'll keep an eye on it, but for now the decision is *not* to propagate element conversions through tuple types. We do recognize that this is a decision we don't get to change our minds on once we've shipped: adding conversions in a later version would be a significant breaking change.\r\n\r\n## Projection initializers\r\n\r\nTuple literals are a bit like anonymous types. The latter have \"projection initializers\" where if you don't specify a member name, one will be extracted from the given expression, if possible. Should we do that for tuples too?\r\n\r\n``` c#\r\nvar a = new { Name = c.FirstName, c.Age }; // Will have members Name and Age\r\nvar t = (Name: c.FirstName, c.Age); // (string Name, int Age) or error?\r\n```\r\n\r\nWe don't think so. The difference is that names are optional in tuples. It'd be too easy to pick up a random name by mistake, or get errors because two elements happen to pick up the same name.\r\n\r\n## Extension methods on tuples\r\n\r\nThis should just work according to existing rules. That means that extension methods on a tuple type apply even to tuples with different element names:\r\n\r\n``` c#\r\nstatic void M(this (int x, int y) t) { ... }\r\n\r\n(int a, int b) t = ...;\r\nt.M(); // Sure\r\n```\r\n\r\n## Default parameters\r\n\r\nLike other types, you can use `default(T)` to specify a default parameter of tuple type. Should you also be allowed to specify a tuple literal with suitably constant elements?\r\n\r\n``` c#\r\nvoid M((string, int) t = (\"Bob\", 7)) { ... } // Allowed?\r\n```\r\n\r\nNo. We'd need to introduce a new attribute for this, and we don't even know if it's a useful scenario.\r\n\r\n## Syntax for 0-tuples and 1-tuples?\r\n\r\nWe lovingly refer to 0-tuples as nuples, and 1-tuples as womples. There is already an underlying `ValueTuple<T>` of size one. We should will also have the non-generic `ValueTuple` be an empty struct rather than a static class.\r\n\r\nThe question is whether nuples and womples should have syntactic representation as tuple types and literals? `()` would be a natural syntax for nuples (and would no doubt find popularity as a \"unit type\" alternative to `void`), but womples are harder: parenthesized expressions already have a meaning! \r\n\r\nWe made no final decisions on this, but won't pursue it for now.\r\n\r\n## Return tuple members directly in scope\r\n\r\nThere is an idea to let the members of a tuple type appearing in a return position of a method be in scope throughout the method:\r\n\r\n``` c#\r\n(string first, string last) GetName()\r\n{\r\n  first = ...; last = ...; // Assign the result directly\r\n  return;                  // No need to return an explicit value\r\n}\r\n```\r\n\r\nThe idea here is to enhance the symmetry between tuple types and parameter lists: parameter names are in scope, why should \"result names\"?\r\n\r\nThis is cute, but we won't do it.  It is too much special casing for a specific placement of tuple types, and it is also actually preferable to be able to see exactly what is returned at a given `return` statement.\r\n\r\n\r\n# Integrating pattern matching with is-expressions and switch-statements\r\nFor pattern matching to feel natural in C# it is vital that it is deeply integrated with existing related features, and does in fact take its queues from how they already work. Specifically we want to extend is-expressions to allow patterns where today they have types, and we want to augment switch-statements so that they can switch on any type, use patterns in case-clauses and add additional conditions to case-clauses using when-clauses.\r\n\r\nThis integration is not always straightforward, as witnessed by the following issues. In each we need to decide what patterns should *generally* do, and mitigate any breaking changes this would cause in currently valid code.\r\n\r\n\r\n## Name lookup\r\n\r\nThe following code is legal today:\r\n\r\n``` c#\r\nif (e is X) {...}\r\nswitch(e) { case X: ... }\r\n```\r\n\r\nWe'd like to extend both the places where `X` occurs to be patterns. However, `X` means different things in those two places. In the `is` expression it must refer to a type, whereas in the `case` clause it must refer to a constant. In the `is` expression we look it up as a type, ignoring any intervening members called `X`, whereas in the `case` clause we look it up as an expression (which can include a type), and give an error if the nearest one found is not a constant.\r\n\r\nAs a pattern we think `X` should be able to both refer to a type and a constant. Thus, we prefer the `case` behavior, and would just stop giving an error when `case X:` refers to a type. For `is` expressions, to avoid a breaking change, we will first look for just a type (today's behavior), and if we don't find one, rather than error we will look again for a constant.\r\n\r\n## Conversions in patterns\r\n\r\nAn `is` expression today will only acknowledge identity, reference and boxing conversions from the run-time value to the type. It looks for \"the actual\" type, if you will, without representation changes:\r\n \r\n``` c#\r\nbyte b = 5;\r\nWriteLine(b is byte);           // True:  identity conversion\r\nWriteLine((object)b is byte);   // True:  boxing conversion\r\nWriteLine((object)b is object); // True:  reference conversion\r\nWriteLine(b is int);            // False: numeric conversion changes representation\r\n```\r\n\r\nThis seems like the right semantics for \"type testing\", and we want those to carry over to pattern matching. \r\n\r\nSwitch statements are more weird here today. They have a fixed set of allowed types to switch over (primitive types, their nullable equivalents, strings). If the expression given has a different type, but has a unique implicit conversion to one of the allowed ones, then that conversion is applied! This occurs mainly (only?) when there is a user defined conversion from that type to the allowed one.\r\n\r\nThat of course is intended only for constant cases. It is not consistent with the behavior we want for type matching per the above, and it is also not clear how to generalize it to switch expressions of arbitrary type. It is behavior that we want to limit as much as possible.\r\n\r\nOur solution is that in switches *only* we will apply such a conversion on the incoming value *only* if all the cases are constant. This means that if you add a non-constant case to such a switch (e.g. a type pattern), you will break it. We considered more lenient models involving applying non-constant patterns to the *non*-converted input, but that just leads to weirdness, and we don't think it's necessary. If you *really* want your conversion applied, you can always explicitly apply it to the switch expression yourself.\r\n\r\n\r\n## Pattern variables and multiple case labels\r\n\r\nC# allows multiple case labels on the same body. If patterns in those case labels introduce variables, what does that mean?\r\n\r\n``` c#\r\ncase int i:\r\ncase byte b:\r\n    WriteLine(\"Integral value!\");\r\n    break;\r\n```\r\n\r\nHere's what it means: The variables go into the same declaration space, so it is an error to introduce two of the same name in case clauses for the same body. Furthermore, the variables introduced are not definitely assigned, because the given case clause assigns them, and you didn't necessarily come in that way. So the above example is legal, but the body cannot read from the variables `i` and `b` because they are not definitely assigned.\r\n\r\nIt is tempting to consider allowing case clauses to share variables, so that they could be extracted from similar but different patterns:\r\n``` c#\r\ncase (int age, string name):\r\ncase (string name, int age):\r\n    WriteLine($\"{name} is {age} years old.\");\r\n    break;\r\n```\r\n\r\nWe think that is way overboard right now, but the rules above preserve our ability to allow it in the future.\r\n\r\n## Goto case\r\n\r\nIt is tempting to ponder generalizations of `goto case x`. For instance, maybe you could do the whole switch again, but on the value `x`. That's interesting, but comes with lots of complications and hidden performance traps. Also it is probably not all that useful.\r\n\r\nInstead we just need to preserve the simple meaning of `goto case x` from current switches: it's allowed if `x` is constant, if there's a case with the same constant, and that case doesn't have a `when` clause.\r\n\r\n## Errors and warnings\r\n\r\nToday `3 is string` yields a warning, while `3 as string` yields and error. They philosophy seems to be that the former is just asking a question, whereas the other is requesting a value. Generalized `is` expressions like `3 is string s` are sort of a combination of `is` and `as`, both answering the question and (conditionally) producing a value. Should they yield warnings or errors?\r\n\r\nWe didn't reach consensus and decided to table this for later.\r\n\r\n## Constant pattern equality\r\n\r\nIn today's `switch` statement, the constants in labels must be implicitly convertible to the governing type (of the switch expression). The equality is then straightforward - it works the same as the `==` operator. This means that the following case will print `Match!`.\r\n\r\n``` c#\r\nswitch(7)\r\n{\r\n  case (byte)7:\r\n    WriteLine(\"Match!\");\r\n    break;\r\n}\r\n```\r\n\r\nWhat should be the case if we switch on something of type object instead?:\r\n\r\n``` c#\r\nswitch((object)7)\r\n{\r\n  case (byte)7:\r\n    WriteLine(\"Match!\");\r\n    break;\r\n}\r\n```\r\n\r\nOne philosophy says that it should work the same way regardless of the static type of the expression. But do we want constant patterns everywhere to do \"intelligent matching\" of integral types with each other? That certainly leads to more complex runtime behavior, and would probably require calling helper methods. And what of other related types, such as `float` and `double`? There isn't similar intelligent behavior you can do, because the representations of most numbers will differ slightly and a number such as 2.1 would thus not be \"equal to itself\" across types anyway.\r\n\r\nThe other option is to make the behavior different depending on the compile-time type of the expression. We'll use integral equality only if we know statically which one to pick, because the left hand side was also known to be integral. That would preserve the switch behavior, but make the pattern's behavior dependent on the static type of the expression.\r\n\r\nFor now we prefer the latter, as it is simpler.\r\n\r\n\r\n# Recursive patterns\r\n\r\nThere are several core design questions around the various kinds of recursive patterns we are envisioning. However, they seem to fall in roughly two categories:\r\n\r\n1. Determine the syntactic shape of each recursive pattern in itself, and use that to ensure that the places where patterns can occur are syntactically well-formed and unambiguous.\r\n2. Decide exactly how the patterns work, and what underlying mechanisms enable them.\r\n\r\nThis is an area to focus more on in the future. For now we're just starting to dig in.\r\n\r\n## Recursive pattern syntax\r\n\r\nFor now we envision three shapes of recursive patterns\r\n\r\n1. Property patterns: `Type { Identifier_1 is Pattern_1, ... , Identifier_n is Pattern_n }`\r\n2. Tuple patterns: `(Pattern_1, ... Pattern_n)`\r\n3. Positional patterns: `Type (Pattern_1, ... Pattern_n)`\r\n\r\nThere's certainly room for evolution here. For instance, it is not lost on us that 2 and 3 are identical except for the presence of a type in the latter. At the same time, the presence of a type in the latter seems syntactically superfluous in the cases where the matched expression is already known to be of that type (so the pattern is used purely for deconstruction and/or recursive matching of the elements). Those two observations come together to suggest a more orthogonal model, where the types are optional:\r\n\r\n1. Property patterns: `Type_opt { Identifier_1 is Pattern_1, ... , Identifier_n is Pattern_n }`\r\n2. Positional patterns: `Type_opt (Pattern_1, ... Pattern_n)`\r\n\r\nIn this model, what was called \"tuple patterns\" above would actually not just apply to tuples, but to anything whose static type (somehow) specifies a suitable deconstruction.\r\n\r\nThis is important because it means that \"irrefutable\" patterns - ones that are known to always match - never need to specify the type. This in turn means that they can be used for unconditional deconstruction even in syntactic contexts where positional patterns would be ambiguous with invocation syntax. For instance, we could have what would amount to a \"deconstruction\" variant of a declaration statement, that would introduce all its match variables into scope as local variables for subsequent statements:\r\n \r\n``` c#\r\n(string name, int age) = GetPerson();    // Unconditional deconstruction\r\nWriteLine($\"{name} is {age} years old\"); // name and age are in scope\r\n\r\n```\r\n\r\n## How recursive patterns work\r\n\r\nProperty patterns are pretty straightforward - they translate into access of fields and properties.\r\n\r\nTuple patterns are also straightforward if we decide to handle them specially.\r\n\r\nPositional patterns are more complex. We agree that they need a way to be specified, but the scope and mechanism for this is still up for debate. For instance, the `Type` in the positional pattern may not necessarily trigger a type test on the object. Instead it may name a class where a more general \"matcher\" is defined, which does its own tests on the object. This could be complex stuff, like picking a string apart to see if it matches a certain format, and extracting certain information from it if so.\r\n\r\nThe syntax for declaring such \"matchers\" may be methods, or a new kind of user defined operator (like `is`) or something else entirely. We still do not have consensus on either the scope or shape of this, so there's some work ahead of us.\r\n\r\nThe good news is that we can add pattern matching in several phases. There can be a version of C# that has pattern matching with none or only some of the recursive patterns working, as long as we make sure to \"hold a place for them\" in the way we design the places where patterns can occur. So C# 7 can have great pattern matching even if it doesn't yet have *all* of it. \r\n "},"comment":{"url":"https://api.github.com/repos/dotnet/roslyn/issues/comments/211109525","html_url":"https://github.com/dotnet/roslyn/issues/10429#issuecomment-211109525","issue_url":"https://api.github.com/repos/dotnet/roslyn/issues/10429","id":211109525,"user":{"login":"asik","id":1631157,"avatar_url":"https://avatars.githubusercontent.com/u/1631157?v=3","gravatar_id":"","url":"https://api.github.com/users/asik","html_url":"https://github.com/asik","followers_url":"https://api.github.com/users/asik/followers","following_url":"https://api.github.com/users/asik/following{/other_user}","gists_url":"https://api.github.com/users/asik/gists{/gist_id}","starred_url":"https://api.github.com/users/asik/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/asik/subscriptions","organizations_url":"https://api.github.com/users/asik/orgs","repos_url":"https://api.github.com/users/asik/repos","events_url":"https://api.github.com/users/asik/events{/privacy}","received_events_url":"https://api.github.com/users/asik/received_events","type":"User","site_admin":false},"created_at":"2016-04-17T20:38:57Z","updated_at":"2016-04-17T20:38:57Z","body":"> It's your claim that this is a bug. With the code out of context like that, i see no bug. :) You mentioned 'probably' and i could respond with \"the dev probably just wanted to tweak their copy of the tuple before using the values later on in the method.\"\r\nReally. The dev computed a value and stored it inside an arbitrary field of a function argument just to log it. That's an equally likely interpretation of that code.\r\n\r\nWell, that's kind of a fantastic thing to say. It's like seeing a potential random AccessViolation and saying the dev intended for the AccessViolation to randomly happen. It's _possible_, that doesn't mean it's an equally likely explanation.\r\n\r\nThe code above is almost certainly wrong, and it's but one example of many.\r\n\r\n> Well, you keep stating that it will be a pitfall for people. I think it would be more of a pitfall if these types didn't behave like the existing language features they look nearly identical to. Now the language feels strange.\r\nI must have missed something, but could you point out in what way tuples would behave less like existing language features if they were immutable? You couldn't re-assign them when used in a pattern-matching argument list? something like\r\n`void Func((int a, int b) tuple) // can't re-assign a or b because they're immutable fields`\r\n\r\nYeah, it's a bit strange, but it's caught at compile time, and it's not causing any bugs. I'm a lot more concerned about pitfalls that'll introduce bugs in my codebase, than pitfalls that'll cause developers to think and fix their code.\r\n\r\nIf it's just about maintaining that symmetry I really fail to see how it \"far outweights\" the pitfalls of mutable value types."}}