<html><head><meta http-equiv="Content-Type" content="text/html charset=us-ascii"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Aug 29, 2017, at 1:49 PM, Slava Pestov &lt;<a href="mailto:spestov@apple.com" class="">spestov@apple.com</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><div class=""><br class=""><blockquote type="cite" class="">On Aug 29, 2017, at 11:03 AM, David Sweeris via swift-dev &lt;<a href="mailto:swift-dev@swift.org" class="">swift-dev@swift.org</a>&gt; wrote:<br class=""><br class="">Hi everyone! I'm trying to implement literal values as generic types.<br class=""></blockquote><br class="">Can you briefly explain what you mean by this?<br class=""><br class="">Are you referring to let-polymorphism, like<br class=""><br class="">let fn = { $0 }<br class="">let f1: (Int) -&gt; Int = fn<br class="">let f2: (Float) -&gt; Float = fn<br class=""></div></div></blockquote></div><br class=""><div class="">No, I mean so that a vector's or matrix's dimensions can be part of its type (strawman syntax and protocol name, but this is pretty much what I'll be trying to support, at least at first):</div><div class=""><div style="margin: 0px; font-size: 18px; line-height: normal; font-family: 'Fantasque Sans Mono'; color: rgb(127, 135, 207); background-color: rgb(0, 57, 70);" class=""><span style="color: #de5194" class="">struct</span><span style="color: #a4b0b1" class=""> Vector&lt;T: </span>ExpressibleByIntegerLiteral<span style="color: #a4b0b1" class="">, L: </span>IntegerLiteralExpr<span style="color: #a4b0b1" class="">&gt; {</span></div><div style="margin: 0px; font-size: 18px; line-height: normal; font-family: 'Fantasque Sans Mono'; color: rgb(164, 176, 177); background-color: rgb(0, 57, 70);" class="">&nbsp; <span style="color: #de5194" class="">var</span> elements: [<span style="color: #7f87cf" class="">T</span>]</div><div style="margin: 0px; font-size: 18px; line-height: normal; font-family: 'Fantasque Sans Mono'; color: rgb(164, 176, 177); background-color: rgb(0, 57, 70);" class="">&nbsp; <span style="color: #de5194" class="">init</span>() {</div><div style="margin: 0px; font-size: 18px; line-height: normal; font-family: 'Fantasque Sans Mono'; background-color: rgb(0, 57, 70);" class=""><span style="color: rgb(164, 176, 177);" class="">&nbsp; &nbsp; <span style="color: #2fafa9" class="">elements</span> = [<span style="color: #7f87cf" class="">T</span>](repeating: <span style="color: #e5493d" class="">0</span>, count: </span><font color="#7f87cf" class="">L</font><font color="#a4b0b1" class="">)</font></div><div style="margin: 0px; font-size: 18px; line-height: normal; font-family: 'Fantasque Sans Mono'; color: rgb(164, 176, 177); background-color: rgb(0, 57, 70);" class="">&nbsp; }</div><div style="margin: 0px; font-size: 18px; line-height: normal; font-family: 'Fantasque Sans Mono'; color: rgb(164, 176, 177); background-color: rgb(0, 57, 70);" class="">}</div><div style="margin: 0px; line-height: normal; background-color: rgb(0, 57, 70); min-height: 14px;" class=""><br class=""></div><div style="margin: 0px; font-size: 18px; line-height: normal; font-family: 'Fantasque Sans Mono'; color: rgb(164, 176, 177); background-color: rgb(0, 57, 70);" class=""><span style="color: #de5194" class="">let</span> vect = <span style="color: #7f87cf" class="">Vector</span><span style="text-decoration: underline" class="">&lt;</span><span style="color: #7f87cf" class="">Int</span>, <span style="text-decoration: underline ; color: #e5493d" class="">5</span>&gt;()</div></div><div class=""><br class=""></div><div class="">And, once that's working, I'm going to add support simple "type functions":</div><div class=""><div style="margin: 0px; font-size: 18px; line-height: normal; font-family: 'Fantasque Sans Mono'; color: rgb(164, 176, 177); background-color: rgb(0, 57, 70);" class=""><span style="color: #de5194" class="">func</span> join &lt;T, L1, L2&gt; (<span style="color: #de5194" class="">_</span> lhs: <span style="color: #7f87cf" class="">Vector</span>&lt;<span style="color: #7f87cf" class="">T</span>, <span style="color: #7f87cf" class="">L1</span>&gt;, <span style="color: #de5194" class="">_</span> rhs: <span style="color: #7f87cf" class="">Vector</span>&lt;<span style="color: #7f87cf" class="">T</span>, <span style="color: #7f87cf" class="">L2</span>&gt;) -&gt;&nbsp;<span style="color: rgb(127, 135, 207);" class="">Vector</span>&lt;<span style="color: rgb(127, 135, 207);" class="">T</span>,&nbsp;<span style="color: rgb(127, 135, 207);" class="">L1</span>&nbsp;+&nbsp;<span style="color: rgb(127, 135, 207);" class="">L2</span>&nbsp;&gt; {...}</div></div><div class="">I think restricting the supported "type functions" to expressions that could be evaluated by the compiler's "constant folding" code would be a reasonable place to start, until we figure out what we want to do about "pure"/"constexpr" stuff... even just "+" for numeric and string literals, and "-" for numeric literals, seems like a reasonable starting goal, and I <i class="">think</i>&nbsp;that'd be simple enough to implement (famous last words, right?)... It's all academic until I get the simple cases working first, though.</div><div class=""><br class=""></div><div class="">Anyway, I think it'll be worth writing up as a proposal, once it's working and I've figured out how to present these "literal types" to Swift's type system (I'd like to keep it as a literal of some sort, so that, say, `L` in this example could be used to set the value of any type that conforms to `ExpressibleByIntegerLiteral`).</div><div class=""><br class=""></div><div class="">- Dave Sweeris</div><div class=""><br class=""></div></body></html>