Targets · Swift · Example

Example

The whole language in one short program — a module with a record, a tagged union, a function with per-platform native bodies, and a function that wires up state, a write-back and a return — and the complete Swift it renders to. It is the same program shown on the language page.

Contents

How a Program becomes Swift, in parts. Start with the introduction, then follow each part into the rendered output and the runtime it links against.

Every keyword, in one program

Each keyword has its own page under keywords; here they are all together.

MODULE App

  TYPE Profile
    FIELD Text name
    FIELD Maths.Integer age

  TYPE Status
    OPTION App.Profile active
    OPTION Text pending

  FUNCTION Text Greet
    INPUT Text name
    NATIVE js    "return 'Hi ' + name;"
    NATIVE dart  "return 'Hi ' + name;"
    NATIVE swift "return \"Hi \" + name"

  FUNCTION Text Welcome
    INPUT BINDING App.Profile profile
    HOLE Text row
      INPUT Text line
    STATE Maths.Integer count = 0
    STATE LOCKED Network.Web.Response saved = null
    REVERSE App.Profile submit
      LET Network.Web.Response response = Network.Web.Client.Post profile
      SET saved = response
    LET Text label = Maths.Integer.ToString count
    = label

Rendered Swift

The module becomes a caseless enum; the record a struct and the union an enum with cases, both keeping the dotted path; the function's swift body is spliced in; and the trailing = becomes a return.

enum App {

  struct Profile {
    let name: String
    let age: Int
    init(_ name: String, _ age: Int) { self.name = name; self.age = age }
  }

  enum Status {
    case active(Profile)
    case pending(String)
  }

  static func Greet(_ name: String) -> String {
    return "Hi " + name                  // the swift NATIVE body
  }

  static func Welcome(_ profile: Profile, _ row: (String) -> ReactiveValue<String>) -> ReactiveValue<String> {
    let count = Program._Runtime.Reactive.State<Int>(0)
    let saved = Program._Runtime.Reactive.StateLocked<Network.Web.Response>(nil)
    Program._Runtime.Reactive.Reverse(submit) {
      let response = Network.Web.Client.Post(profile)
      Program._Runtime.Reactive.Set(saved, response)      // SET
    }
    let label = Maths.Integer.ToString(count)
    return label
  }
}

Reading it

Indentation becomes nested trailing closures — the REVERSE body is a { … }. Dotted names stay real through nested enums (Network.Web.Client.Post) — see naming considerations. The per-keyword mapping is on keywords.