3 ms·
> case let .NoAction(_, v): return v That's amazingly awkward syntax for a brand-new language. "case let"? In many other languages with pattern matchin
by JoshTriplett 12y ago
> case let .NoAction(_, v): return v
That's amazingly awkward syntax for a brand-new language. "case let"? In many other languages with pattern matching and destructuring, the "let" occurs implicitly.
- thinkpad20 12y agoThe reason why they don't do that is that you can reference variable names: foo = 1 switch bar { // Equals the value of (existing) variable foo case foo: return 0 // Catch-all, binds `foo` to whatever is in `bar` case let foo: return foo + 1 } (My syntax might be off, I've never written Swift) To do this in Haskell, you'd have to write something like: foo = 1 case bar of foo' | foo' == foo -> 0 foo -> foo + 1 Because you're only able to pattern-match against literal objects (constructors or numeric/string literals). This might be different in some other ML-like languages, or there could be GHC extensions for this, of course.
- yeldarb 12y agoIn Swift this could also be written as > case .NoAction(_, let v): return v Putting it at the beginning makes it apply to all the variables in the parens (although in this case they are ignoring the first one by using the underscore). Using "let" makes it immutable; you can also use "var" there to make it a variable you can modify in the case body.