3 ms·
I'm a little disappointed that pattern matching doesn't simply narrow the variable, and instead requires a new variable name to hold the narrowed type. Even Typ
by jdmichal 10y ago
I'm a little disappointed that pattern matching doesn't simply narrow the variable, and instead requires a new variable name to hold the narrowed type. Even TypeScript narrows the variable in-place!
- louthy 10y agoYou may want to deconstruct the value and use the original base value. This seems entirely logical, and consistent with most implementations in functional languages (and I'd say makes TypeScript's implementation the poor one - although I haven't seen it myself to have an opinion on).
- naasking 10y ago> You may want to deconstruct the value and use the original base value. I think this is exactly right. Members can be declared visible only when viewing an object through a particular supertype, and you may simultaneously need to invoke methods from the supertype and the subtype.
- int_19h 10y agoThing is, in most cases, you don't want to do that. You just want to narrow the type. It's unfortunate that this isn't readily possible even by explicitly reusing the name - C# shadowing rules for locals preclude it. So you can't do something like `if (x is int x) { ... }`.
- dested 10y agoI believe this would cause a change in the CLR as opposed to just syntax sugar. It's less of an issue for typescript since all type information is erased at runtime.
- ygra 10y agoThe CLR doesn't care, it doesn't see the C# code and the compiler could just as well emit another local for the narrowed variable. The language semantics here don't really depend on what the CLR does.
- seanmcdirmid 10y agoThat would apply only if you were switching on a variable and not a more general expression. Scala does the same thing, I think.