3 ms·
> He complains about a lack of inferred typing and pattern matching, two things which Swift does support. 1. Swift only implements local type inference (which
by masklinn 11y ago
> He complains about a lack of inferred typing and pattern matching, two things which Swift does support.
1. Swift only implements local type inference (which the user makes use of), languages like Haskell or MLs support global type inference. That is, Swift does not infer function types.
2. Swift's pattern-matching support is more limited than Haskell's or Erlang's[0] and function toplevels aren't pattern-matched whereas Haskell's or Erlang's are.
[0] notably, you can't use refutable patterns in Swift, while they're very common in Erlang, and by default only a limited number of type classes — tuples, enums, ranges and atoms IIRC — can be matched, you can't destructure an arbitrary structs by pattern matching. On the other hand, Swift's pattern-matching can be extended (by overriding ~=)
- ridiculous_fish 11y agoIsn't switch an example of refutable pattern matching in Swift? You can distinguish enums, and use where clauses: switch read(/* stuff */) { case 0: print("EOF") case let amt where amt > 0: print("Read \(amt)") default: print("Errored") } The 'let amt where...' is a refutable pattern, isn't it?
- masklinn 11y agoAh yes I forgot to write the "in var/let" part which was supposed to be there. That is you can only use refutable patterns in `switch` (and `if let`/`while let` though I believe those are still restricted to Option, not general-purpose as in Rust, that correct?). In Erlang you can use a refutable match in an "assignment", that's basically a pattern assertion, a more general-purpose version of Swift's forced unwrapping.