[swift-evolution] URL Literals
davesweeris at mac.com
Sat Dec 17 12:59:29 CST 2016
Sent from my iPhone
> On Dec 17, 2016, at 09:47, Xiaodi Wu via swift-evolution <swift-evolution at swift.org> wrote:
> I was thinking of even more explicitly venturing into `constexpr` territory by allowing particular initializers and functions to be marked as `@constantExpression` (or however you want to call it) when they use a restricted subset of the language, and having the compiler evaluate those at compile time when all arguments are literals. That way, you'd get compile-time warnings of failable initializers guaranteed to fail _without_ any changes in syntax to the language.
It wouldn't have to be restricted to a subset of the language; just to functions whose evaluation doesn't depend on anything that happens at runtime.
Any "@pure" (or whatever... it's easier to type on my phone than "@constantExpression") function should work fine, if we define a "pure" function to be something like:
1) Doesn't mutate anything outside its own scope
2) Doesn't call anything involving rand() or other non-deterministic functions
3) Doesn't have a result that depends on the host or target architectures (I'm not sure if this extends to FP subtleties)
4) Doesn't reference any non-local variables which don't themselves have a value that's either itself a literal or the result of evaluating a "pure" function
5) Doesn't call any other functions which aren't themselves "pure", or instantiate any variables using inits that aren't "pure"
Since there's already a Swift REPL, at least conceptually speaking, this doesn't seem too hard (although it does raise the bar a bit for what it takes to have a "full" Swift compiler, since it'd then depend on having a working REPL on the host platform, which IIRC wasn't the case on Linux for a while... dunno, maybe this is a non-issue).
- Dave Sweeris
More information about the swift-evolution