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

Gor Gyolchanyan gor.f.gyolchanyan at icloud.com
Sun Jul 30 01:52:36 CDT 2017


I’ve thought about this a lot lately. I think, given llvm’s built-in MCJIT engine and interpreter, it would be relatively easy to skip the under-developed constexpr that C++ settled on and go straight for a more elaborate compile-time facilities, which would also provide extremely powerful meta programming features (think C preprocessor macros, except context-aware, more powerful, convenient and safe). D (http://dlang.org) has this feature, which is really great, but it doesn’t save the otherwise sloppy and lackluster language.
In a nutshell, marking something as compiletime (or some more fitting keyword for the purpose) would do two things:
Any runtime-defined facilities (code to be executed, values of function parameters, assertion and precondition checks) would be attempted to be run/acquired at compiletime.
If any of those facilities are not possible to compute at compiletime, a compiler error would occur.
Besides being useful in generic value parameters, this could also be used to define a much more elaborate reflection system in Swift (which would now be much less of a magic).
If in addition to this, select parts of Swift’s frontend would be exposed to the language in the form of compile-time mutable values (like types, variables, functions, attributes), then users could use these compiletime facilities to do what Swift’s gyb.py (https://github.com/apple/swift/blob/master/utils/gyb.py <https://github.com/apple/swift/blob/master/utils/gyb.py>) currently does (and if you’re like me, you find yourself using it a lot, especially for libraries with a complete integration with some other library).

This is a huge undertaking, but if we split it in two parts: "executing at compiletime" and "exposing swift’s frontend”, this could surely be done by Swift 5. From all the ideas that I’ve had or seen on this mailing list, in my opinion this is the most ground-braking, powerful and useful, so I’d be willing to work day and night to help implement this.

> On Jul 30, 2017, at 2:02 AM, Daryle Walker via swift-evolution <swift-evolution at swift.org> wrote:
> 
> The “constexpr” facility from C++ allows users to define constants and functions that are determined and usable at compile-time, for compile-time constructs but still usable at run-time. The facility is a key step for value-based generic parameters (and fixed-size arrays if you don’t want to be stuck with integer literals for bounds). Can figuring out Swift’s story here be part of Swift 5?
> 
>> Daryle Walker
> Mac, Internet, and Video Game Junkie
> darylew AT mac DOT com 
> 
> _______________________________________________
> 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/3cabc935/attachment.html>


More information about the swift-evolution mailing list