[swift-evolution] Proposal: allow arbitrary compile-time code execution

Haravikk swift-evolution at haravikk.me
Tue Feb 9 03:18:51 CST 2016


It seems a bit like an all new syntax though; what about an attribute that places some extra requirements on the function that throws an error if it can’t be produced at compile time? Not sure what it should be called, but I was thinking something like:

	@compile_time func myFunction(foo:String) -> Expr { … }

If the function can’t be made available at compile time due to dependencies or runtime requirements or whatever, then it will generate an error, otherwise it will be compiled ahead of other functions, and will explicitly have a precomputed value.

The idea is that while the attribute explicitly tells the compiler to compile this function in advance, it doesn’t introduce a separate class of function or a kind of language within a language, and the function could even still be called at runtime as well with dynamic values for foo (rather than constant ones).

That said, I still think that ideally the compiler should do as much of this pre computation as it can, the difference here is that the attribute explicitly requires it, while another could explicitly prevent it (if you’re concerned about memory).

> On 8 Feb 2016, at 19:58, Joe Groff via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
> 
> This would be cool, but allowing arbitrary code from the enclosing context to be executed introduces difficult dependency ordering issues. I like the design of Metalua (http://metalua.luaforge.net <http://metalua.luaforge.net/>), which allows for pretty explicit and flexible jumping up and down "meta" levels, which in addition to building values, can also build arbitrary ASTs. In Swift, that might look something like this:
> 
> 
> // Compile-time available definitions, that produce Expr nodes
> #{
> func myCompileTimeFunction() -> Expr { ... }
> }#
> 
> // Evaluate a compile-time function and expand the result expression here
> let myValue = #(myFunction())
> 
> -Joe
> 
>> On Feb 7, 2016, at 8:29 PM, Steve Richey via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
>> 
>> It is not uncommon to want to run code at build time. Xcode solves this need by allowing "Build Phases", which are often shell scripts which handle compile-time tasks.
>> 
>> It may be beneficial to Swift developers, especially those who are not programming on platforms supported by Xcode, to handle compile-time tasks in a powerful and expressive language with which they are already familiar: Swift!
>> 
>> In short, I propose the addition of a `#run` expression that allows the execution of code at compile time. This syntax and concept are borrowed from the programming language Jai (https://sites.google.com/site/jailanguageprimer/#TOC-Arbitrary-Compile-Time-Code-Execution <https://sites.google.com/site/jailanguageprimer/#TOC-Arbitrary-Compile-Time-Code-Execution>) but the syntax fits nicely with other special expression syntax in Swift such as `#available`, `#selector`, `#file`, and so on.
>> 
>> Example:
>> ```
>> func myFunction() -> String {
>> return "hello"
>> }
>> 
>> let myValue = #run myFunction()
>> ```
>> 
>> At compile time, `myFunction` is evaluated and the result inlined to the `myValue` definition. At run time, `myValue` is a `String` containing `"hello"`.
>> 
>> This is useful for tasks that are relatively expensive to run but only need to be done once, such as lookup tables. Running the algorithm to generate those tables can be handled at compile-time, and the results retrieved at no cost at run time. Furthermore, this structure allows code reuse between the run time and build time code, obviating the need to perform similar tasks in, say, a Swift method and a Python script.
>> 
>> This is my first attempt at a Swift proposal, so any and all feedback is welcome! This seems like it should be easy enough to implement but I'm not especially familiar with Swift's internals. If there are any hurdles or unforeseen consequences associated with this from an implementation standpoint, that would be very useful to know.
>> 
>> Thank you for your time!
>> 
>> Best regards,
>> Steve Richey
>> _______________________________________________
>> swift-evolution mailing list
>> swift-evolution at swift.org <mailto:swift-evolution at swift.org>
>> https://lists.swift.org/mailman/listinfo/swift-evolution <https://lists.swift.org/mailman/listinfo/swift-evolution>
> 
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org <mailto:swift-evolution at swift.org>
> https://lists.swift.org/mailman/listinfo/swift-evolution <https://lists.swift.org/mailman/listinfo/swift-evolution>

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


More information about the swift-evolution mailing list