<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></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 30, 2017, at 8:15 AM, Tino Heth <<a href="mailto:2th@gmx.de" class="">2th@gmx.de</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="Content-Type" content="text/html charset=utf-8" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">That's a great choice ;-) — if I had the time, this would be the Swift-feature I'd spend it for…</div></div></div></blockquote><div><br class=""></div>Yeah, I remember your enthusiasm for it back when the topic first came up :-)</div><div><br class=""></div><div><br class=""></div><div><br class=""><blockquote type="cite" class=""><div class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">I hope you have the stamina to finish that</div></div></div></blockquote><div><br class=""></div>lol, me too!</div><div><br class=""></div><div><br class=""></div><div><br class=""><blockquote type="cite" class=""><div class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">and although it's no real help for implementation, I have some remarks that might be useful in the long run.</div><div class=""><br class=""></div><div class="">There are already some thoughts on the topic online (afair even core had something to say about it — I can try to find it if you didn't do so already ;-).</div><div class="">Your Vector example illustrates one problem: "T: X" can now have two different meanings; either T is a subtype of X, or T is a literal of type X.</div><div class="">Even if the compiler can figure out what meaning to chose, it would help humans to have a slightly different syntax.</div></div></div></blockquote><div><br class=""></div><div><br class=""></div><div>Maybe, yeah... I'm trying to make it so that the literal value itself is what's conforming to X (which is why this feature is only about <i class="">literal</i> values, and not arbitrary values of a type that conforms to the relevant `ExpressibleBy*Literal` protocol), to preserve the "untyped" nature of literal values. This would let us simply use the "type" by itself (like in the init body from the original Vector example) wherever an integer literal would work. How such a protocol would be implemented is something I haven't worked on yet, but I'll probably start by taking a look at how the existing `ExpressibleBy*Literal` protocols work. Fully implementing this (by which I mean allowing for adding generic literals like in the "join" function) would require some way of having the compiler track which identifiers can be evaluated at compile-time and which are strictly run-time objects, which is why I think this is so closely tied to what C++ calls "constexpr".</div><div><br class=""></div><div>Failing that, plan B is to write a protocol something like: </div><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 style="margin: 0px; line-height: normal;" class=""><span style="color: #de5194" class="">protocol</span> IntegerLiteralExpr {</div><div style="margin: 0px; line-height: normal; color: rgb(127, 135, 207);" class=""><span style="color: #a4b0b1" class=""> </span><span style="color: #de5194" class="">static</span><span style="color: #a4b0b1" class=""> </span><span style="color: #de5194" class="">var</span><span style="color: #a4b0b1" class=""> value: </span>IntegerLiteralType<span style="color: #a4b0b1" class=""> {</span><span style="color: #de5194" class="">get</span><span style="color: #a4b0b1" class="">}</span></div><div style="margin: 0px; line-height: normal;" class="">}</div></div><div class="">which would change the init function in my example to;</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="">init</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=""> <span style="color: #2fafa9" class="">elements</span> = [<span style="color: #7f87cf" class="">T</span>](repeating: <span style="color: #e5493d" class="">0</span>, count: <span style="color: #7f87cf" class="">L</span>.<span style="color: #2fafa9" class="">value</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=""> }</div></div><div class=""><br class=""></div><div class=""><br class=""></div></div><br class=""><blockquote type="cite" class=""><div class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">There's also the idea of labeled generic parameters, and default values for them.</div><div class="">Both aren't required, but imho at least the labels might be something that people request before another kind of generics is accepted.</div></div></div></blockquote><div><br class=""></div><div>I might start on those next, if nobody else has done it before I get this feature working. No promises, though... not until I've gone through the process once and have a better idea of what I'm getting into.</div><div><br class=""></div><div><br class=""></div><div><br class=""></div><blockquote type="cite" class=""><div class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class=""><blockquote type="cite" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><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<T: </span>ExpressibleByIntegerLiteral<span style="color: #a4b0b1" class="">, L: </span>IntegerLiteralExpr<span style="color: #a4b0b1" class="">> {</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=""> <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=""> <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=""> <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=""> }</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=""><</span><span style="color: #7f87cf" class="">Int</span>, <span style="text-decoration: underline ; color: #e5493d" class="">5</span>>()</div></div></div></blockquote><div class="">I have strong hope that literal generics will form the basis for fixed-size vectors, so that there will be no need to define such a type ;-) — but besides that:</div><div class="">Why is T restricted here? Even if it should be a numeric type, I would allow floats as well.</div></div></div></div></blockquote><div><br class=""></div><div>Floating point types do conform to `ExpressibleByIntegerLiteral`. Unless you meant to ask why `L` was restricted, in which case it's simply that Vector/Matrix types are my main motivation, so I started with integers. My intention is to implement it in such a way that <i class="">any</i> literal value will work, even, say, a hypothetical "ClosureLiteralExpr", "let x = Foo<let bar = whatever(something)>()". Anyway, the point is that I very much don't want to tie this feature's implementation to the existing set of literal expressions. As a more likely example, given how the evolution discussions were going, it seems reasonable to think it's possible we might get a RegExLiteral some day. IIUC (which is far from certain), in the worst case, I could just store the relevant characters of source code. Come to think of it, I might want to do that even for integer literals, if that makes it easier to preserve their "literalness" in the type system, but I'll cross that bridge when I come to it.</div></div><div><br class=""></div><div><br class=""></div><div><br class=""><blockquote type="cite" class=""><div class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class=""><blockquote type="cite" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><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 <T, L1, L2> (<span style="color: #de5194" class="">_</span> lhs: <span style="color: #7f87cf" class="">Vector</span><<span style="color: #7f87cf" class="">T</span>, <span style="color: #7f87cf" class="">L1</span>>, <span style="color: #de5194" class="">_</span> rhs: <span style="color: #7f87cf" class="">Vector</span><<span style="color: #7f87cf" class="">T</span>, <span style="color: #7f87cf" class="">L2</span>>) -> <span style="color: rgb(127, 135, 207);" class="">Vector</span><<span style="color: rgb(127, 135, 207);" class="">T</span>, <span style="color: rgb(127, 135, 207);" class="">L1</span> + <span style="color: rgb(127, 135, 207);" class="">L2</span> > {...}</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> 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></blockquote>Even without such calculations, the feature would be quite useful:</div><div class="">For type-safe matrices, it would already be enough to have the dimensions attached to the type.</div></div></div></blockquote><div><br class=""></div>Agreed, but it'd be a pretty severe limitation, IMHO, to not be able to split/join values of such types.</div><div><br class=""></div><div>- Dave Sweeris</div><br class=""></body></html>