2017-03-03 2:13 GMT+03:00 Slava Pestov <[email protected]>:
Does newtype add any new capability that’s not already covered by defining
> a struct?
>
newtype would forward all members and conformances of the underlying type:
newtype RecordId = Int
let x: RecordId = 5let y = x + 10
extension RecordId {
func load() -> String { … }
}
let a = x.load()let b = 42.load() // error
newtypes aim to carry purely semantic information, in this case, that those
integers can be used to fetch records from a database. We get additional
members only when we are sure that semantic of current instance is
appropriate.
As a side effect, it essentially allows to declare one-liner structs with
pattern matching. But I agree with Jaden, just adding pattern matching to
structs feels more practical. And this feature is more or less orthogonal
to the core functionality of newtype.
_______________________________________________
swift-evolution mailing list
[email protected]
https://lists.swift.org/mailman/listinfo/swift-evolution