<div dir="ltr"><div class="markdown-here-wrapper" style=""><p style="margin:0px 0px 1.2em!important">2017-03-03 2:13 GMT+03:00 Slava Pestov <<a href="mailto:spestov@apple.com" target="_blank">spestov@apple.com</a>>:</p>
<p style="margin:0px 0px 1.2em!important"></p><div class="markdown-here-exclude"><p></p><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div style="word-wrap:break-word"><div>Does newtype add any new capability that’s not already covered by defining a struct?</div></div></blockquote><p></p></div><p style="margin:0px 0px 1.2em!important"></p>
<p style="margin:0px 0px 1.2em!important"><code style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);background-color:rgb(248,248,248);border-radius:3px;display:inline">newtype</code> would forward all members and conformances of the underlying type:</p>
<pre style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;font-size:1em;line-height:1.2em;margin:1.2em 0px"><code class="hljs language-swift" style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);background-color:rgb(248,248,248);border-radius:3px;display:inline;white-space:pre;overflow:auto;border-radius:3px;border:1px solid rgb(204,204,204);padding:0.5em 0.7em;display:block!important;display:block;overflow-x:auto;padding:0.5em;color:rgb(51,51,51);background:rgb(248,248,248)">newtype <span class="hljs-type" style="color:rgb(68,85,136);font-weight:bold">RecordId</span> = <span class="hljs-type" style="color:rgb(68,85,136);font-weight:bold">Int</span>
<span class="hljs-keyword" style="color:rgb(51,51,51);font-weight:bold">let</span> x: <span class="hljs-type" style="color:rgb(68,85,136);font-weight:bold">RecordId</span> = <span class="hljs-number" style="color:rgb(0,128,128)">5</span>
<span class="hljs-keyword" style="color:rgb(51,51,51);font-weight:bold">let</span> y = x + <span class="hljs-number" style="color:rgb(0,128,128)">10</span>
<span class="hljs-class"><span class="hljs-keyword" style="color:rgb(51,51,51);font-weight:bold">extension</span> <span class="hljs-title" style="color:rgb(153,0,0);font-weight:bold;color:rgb(68,85,136);font-weight:bold">RecordId</span> </span>{
<span class="hljs-func"><span class="hljs-keyword" style="color:rgb(51,51,51);font-weight:bold">func</span> <span class="hljs-title" style="color:rgb(153,0,0);font-weight:bold">load</span><span class="hljs-params">()</span> -> <span class="hljs-title" style="color:rgb(153,0,0);font-weight:bold">String</span> </span>{ … }
}
<span class="hljs-keyword" style="color:rgb(51,51,51);font-weight:bold">let</span> a = x.load()
<span class="hljs-keyword" style="color:rgb(51,51,51);font-weight:bold">let</span> b = <span class="hljs-number" style="color:rgb(0,128,128)">42</span>.load() <span class="hljs-comment" style="color:rgb(153,153,136);font-style:italic">// error</span>
</code></pre>
<p style="margin:0px 0px 1.2em!important"><code style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);background-color:rgb(248,248,248);border-radius:3px;display:inline">newtype</code>s 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.</p>
<p style="margin:0px 0px 1.2em!important">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 <code style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);background-color:rgb(248,248,248);border-radius:3px;display:inline">newtype</code>.</p>
<div title="MDH:PGRpdiBjbGFzcz0iZ21haWxfZXh0cmEiPjxkaXYgY2xhc3M9ImdtYWlsX3F1b3RlIj4yMDE3LTAz
LTAzIDI6MTMgR01UKzAzOjAwIFNsYXZhIFBlc3RvdiA8c3BhbiBkaXI9Imx0ciI+Jmx0OzxhIGhy
ZWY9Im1haWx0bzpzcGVzdG92QGFwcGxlLmNvbSIg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