[swift-evolution] [Planning][Request] "constexpr" for Swift 5

Chris Lattner clattner at nondot.org
Sun Jul 30 16:34:37 CDT 2017


On Jul 30, 2017, at 10:03 AM, Gor Gyolchanyan via swift-evolution <swift-evolution at swift.org> wrote:
> Tino Heth:
> If you read my reply to Daniel Vollmer, you’ll find that we’re thinking about the exact same thing. Your code snippers show my vision of compiletime beautifully 🙂.
> Now what I really want at this point is to here the opinions of the core team on this topic.
> 
> Swift Core Team:
> Have you guys thought of this? Do you think this is a good idea to put on the table or do you have different plans?

I don’t speak for the core team as a whole, but here are my personal opinions.  Note that this is off the cuff, without a lot of deliberate consideration, so YMMV:

1) I don’t think it makes sense to add constexpr before there is a motivating use-case for it.  I agree that it could be useful with fixed sized arrays and with constant parameters to generics, but we don’t have either of those yet.  If it were up to me, the place to start would be figuring out constant generic parameters, since the answer there could inform fixed size arrays.

2) I agree that there is heavy overlap with constexpr and macros, and that it would be (probably showstoppingly) unfortunate to require duplication of large swaths for the stdlib to make it work with constexprs.  Adding “constexpr” to a few operator implementations would be reasonable though, and may be a way to formalize “transparent”.

3) I’m a fan of eventually adding a very general, but still hygienic, macro system at some point, and assume that the # sigil will be used to for all things macros.  For example, I’ve come to see the old “property behaviors” proposal as being better reimagined as “property macros”, which suggest syntax like “var #resettable foo : T” or something like that.

4) The macro system should eventually support unstructured compile time expansion, along the lines of what people do with Gyb and Sourcery with all the tradeoffs they bring.

I tend to prefer that exploration in this space happen in order of points above, and would prefer not to get to a generalized macro system for a couple of years - just to give Swift more time to mature and develop without it.  Other languages have gotten macro systems too early in their development, and become too reliant on it.  Perhaps if C didn’t have one it would have ended up with a proper import mechanism.  Perhaps if Rust didn’t have one, some of its decisions would have been different.  etc.

-Chris



> 
>> On Jul 30, 2017, at 7:56 PM, Tino Heth <2th at gmx.de <mailto:2th at gmx.de>> wrote:
>> 
>> 
>>> more elaborate compile-time facilities, which would also provide extremely powerful meta programming features
>> That's an interesting twist — but whenever you put a "meta" somewhere, things tend to get complicated, and people end up with different associations to the topic… ;-)
>> I took a brief look at the C++ document, but it seemed still to much macro-like to me.
>> 
>> My take on the topic would be he ability to express common programming tasks (declaring a class, overriding a method…) in the language itself.
>> Imagine
>> public class ProxyViewController: UIView {}
>> Could be written as
>> let subclass = createClass(classname: "ProxyViewController", superclass: UIViewController, accessLevel: .public)
>> 
>> Quite stupid at first sight, and basically the exact opposite of syntactic sugar ("syntactic salt" already has a meaning… so I'd call it "syntactic pepper" ;-).
>> But now imagine that:
>> 
>> for (method, implementation) in UIViewController.methods where method.accessLevel == .open {
>>     subclass.methods[method] = { parameters in
>>         print("Subclass method \(method) called with \(parameters)")
>>         return implementation(parameters)
>>     }
>> }
>> 
>> 
>> Not that stupid anymore, isn't it?
>> I think this would be way cooler than poking around with variants of search & replace…
>> 
>> - Tino
>> 
>> (to get syntax colouring, I wrote ~30 lines of Swift that turn the straw man example into valid code… it's fun, maybe I play with it a little bit more ;-)
> 
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170730/ff1aa4ff/attachment.html>


More information about the swift-evolution mailing list