HomeiOS Developmentios - Can type-inference in buildEither be used to find out kind...

ios – Can type-inference in buildEither be used to find out kind parameters when utilizing resultBuilders?


Is it a requirement to have buildEither(first:) and buildEither(second:) return the identical kind? I used to be experimenting with resultBuilders and wasn’t capable of purpose about how the operate is remodeled.

For example what I imply, I’m attempting to get the TwoTypes initialiser to print out two totally different sorts. I used to be attempting to have the if-else assertion decide the varieties of A and B, and have these sorts be printed:

struct TwoTypes<A, B> {
    init(a: A.Kind, b: B.Kind) {
        print(a)  
        print(b)
    }
}

@resultBuilder
struct TwoTypesBuilder<A, B> {
    // From the instance under, I anticipated type-inference to find out "A" to be "Int"
    static func buildBlock(_ element: A) -> A {
        return element
    }
    static func buildEither(first element: A) -> A {
        return element
    }

    // From the instance under, I anticipated type-inference to find out "B" to be "Double"
    static func buildBlock(_ element: B) -> B {
        return element
    }
    static func buildEither(second element: B) -> B {
        return element
    }

    // Since type-inference now is aware of A and B, we will print "Int" and "Double"
    // "T" represents *both* A or B
    static func buildFinalResult<T>(_ element: T) -> TwoTypes<A, B> {
        TwoTypes(a: A.self, b: B.self)
    }
}

func TwoTypesMake<A, B>(@TwoTypesBuilder<A, B> builder: () -> TwoTypes<A, B>) {
    builder()
}

func most important() {
    TwoTypesMake {
        // This if-else assertion would decide A and B, and thus, print out A.kind and B.kind within the 
        // TwoTypes initialiser 
        if Bool.random() {
            2  // << Error: Can't convert worth of kind 'Int' to anticipated argument kind 'Double'
        } else {
            3.4  // Error: Can't convert worth of kind 'Double' to anticipated argument kind 'Int'
        }
    }
}

most important()

Based mostly on my understanding of resultBuilders, I believe that is not possible to realize (thus a easy “not attainable” within the replies is greater than ample) as a result of the kind of vMerged (described within the proposal for outcome builders) cannot be decided. Nonetheless, I’d like to be proved improper as possibly there’s a approach for vMerged to have a placeholder/generic kind and be initialised at runtime.

Wouldn’t it be attainable to one way or the other get the TwoTypes initialiser to print out two totally different values? Extra particularly, would it not be attainable for every department to supply a unique worth and have type-inference decide every kind and thus decide A and B?


Just a little snippet for these too lazy to search out out what vMerged is from the outcome builder proposal:

  • If the outcome builder kind declares the buildEither(first:) and buildEither(second:) result-building strategies, a full binary tree with N leaves (the injection tree) is chosen, and every result-producing case is uniquely assigned a leaf in it; these selections are implementation-defined. A novel variable vMerged of contemporary kind is said earlier than the assertion.

(no clue what “contemporary kind” means)

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments