[Pitch #2] Function builders

Yeah, something like this could fit. I have a couple of questions about the intended behavior here.

As I understand it, the goal is to effectively leave the declarations unchanged, but allow the declaration to produce a "partial result" (per the proposal's terminology) as well. So, your schema example would be translated into something like this:

let thingSchema = Schema(id: "thing")
let v0 = Builder.buildDeclaration(thingSchema)
let otherSchema = Schema(id: "other")
let v1 = Builder.buildDeclaration(otherSchema)

let v2 = Builder.buildExpression(Table(schemas: thingSchema, otherSchema) { ... })
let v3 = Builder.buildExpression(Table(schema: thingSchema) { ... })
let v4 = Builder.buildExpression(Table(schema: otherSchema) { ... })
return Builder.buildBlock(v0, v1, v2, v3, v4)

Does that look correct?

So, my questions:

  1. If multiple variables are bound by the declaration, should there be multiple buildDeclaration calls (one per variable, with the value actually bound to that variable) or should there be a single buildDeclaration call with the type of the initializer. E.g., given

    let (a, b) = buildTwoTables()
    

    is that going to produce

    let v0 = Builder.buildDeclaration(a)
    let v1 = Builder.buildDeclaration(b)
    

    or

    let v1 = Builder.buildDeclaration((a, b))
    

    ?

  2. Should this feature allow one to affect the type of the declaration? For example, if I have

    let a = getIntValue()
    

    would it be possible to buildDeclaration to make the type of a something other than Int, e.g., RefCell<Int>?

  3. Should this feature make it possible to reason about the identity of the declarations? I could certainly imagine a DSL where one wants to be able to define variables and then reason about them as entities, rather than as the values they produce. For example, if we had something like this:

    let a = getSomeValue()
    let b = getSomeValue()
    a >= 2 * b // form a relationship between the two variables              
    

    We might want to work with a and b via key-paths, or some other mechanism for identity. I'm not sure how to do this transformation, short of having the function builder be able to wrap up a and b in a property wrapper that it gets to initialize.

Doug

3 Likes