<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 29, 2017, at 5:22 PM, David Sweeris via swift-dev <<a href="mailto:swift-dev@swift.org" class="">swift-dev@swift.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="Content-Type" content="text/html charset=us-ascii" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><br class=""><div class=""><blockquote type="cite" class=""><div class="">On Aug 29, 2017, at 1:49 PM, Slava Pestov <<a href="mailto:spestov@apple.com" class="">spestov@apple.com</a>> 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 <<a href="mailto:swift-dev@swift.org" class="">swift-dev@swift.org</a>> 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) -> Int = fn<br class="">let f2: (Float) -> 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<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 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 <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></div></blockquote><div><br class=""></div><div>Constant folding occurs too late in the compilation process for you to take advantage of it in the solver. You’ll need to write your own evaluator and teach CSSimplify to invoke it during the type matching process. I would start by adding a new kind of Type and TypeRepr that hold SequenceExpr nodes and a CanType that holds an APSInt representing the result of evaluating the expression. </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=""><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></div>_______________________________________________<br class="">swift-dev mailing list<br class=""><a href="mailto:swift-dev@swift.org" class="">swift-dev@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-dev<br class=""></div></blockquote></div><br class=""></body></html>