[swift-evolution] [Design Question] Metaprogramming as a non-goal

Dave Abrahams dabrahams at apple.com
Sun Jan 24 16:54:29 CST 2016

on Sat Jan 23 2016, Michael Henson <swift-evolution at swift.org> wrote:

> In the swift/docs/Generics.rst documentation, I see:
> "
> As important as the goals of a feature are the explicit non-goals, which we
> don't want
> or don't need to support:
> * Compile-time "metaprogramming" in any form
> * Expression-template tricks a la Boost.Spirit, POOMA
> "
> What kinds of things count as compile-time metaprogramming? I've been
> tinkering with some ideas related to the type system and having a more
> specific description here might help me pare many / most / all of them
> down.

Some of us suspect that the generics model we already have would offer
this capability, if it weren't for bugs in the implementation:

  protocol BooleanType {}

  struct False : BooleanType {}
  struct True : BooleanType {}

  protocol IfType {
    typealias C : BooleanType
    typealias T
    typealias F
    typealias Result = F

    var result: Result? {get}

  extension IfType {
    var result: Result? { return nil }

  // Take this extension away to change the result
  extension IfType where Self.C == True {
    var result: T? { return nil }

  struct If<C_: BooleanType, T_, F_> : IfType {
    typealias C = C_
    typealias T = T_
    typealias F = F_

  print(If<True, Int, String>.Result.self)  // prints Int; OK
  print(If<False, Int, String>.Result.self) // prints Int; should print String

But, basically, that part is saying we would prefer it if ideas like
this were expressed directly (e.g. by an actual "if" statement used with
a macro system) rather than by hijacking features of generics.



More information about the swift-evolution mailing list