cocoa - "type of expression is ambiguous without more context" error in Swift closure -


i'm getting strange type-related error in swift code:

type of expression ambiguous without more context.

this happens if provide full type information.

here's code reproduces it.

i have 2 structures:

struct person{     let name_ : string     let address_ : address }  struct address {     let street_ : string     let city_ : string } 

i create structure contains 2 functions , set address of person:

struct lens<a,b> {     let extract: (a)->b     let create: (b,a) -> } 

when try create instance of lens gets , sets address (in later case returns new person new address), error in first closure.

let lens : lens<person, address> = lens(     extract: {(p:person)->address in         return p.address_}, // here's error     create: {person($0.name_,         address(street_: $1, city_: $0.address_.city_))}) 

not type of parameter of first closure specified in type lens, in closure itself.

what's going on????

while suggests error in extract, in create. $0 , $1 backwards. you're referring $0.name_, $0 of create closure b, address, name_ property of person. think meant:

let lens : lens<person, address> = lens(     extract: { $0.address_ },     create: { person(name_: $1.name_, address_: address(street_: $0.street_, city_: $1.address_.city_)) } ) 

or can redefine lens:

struct lens<a, b> {     let extract: (a) -> b     let create: (a, b) ->   // note, & b swapped } 

and can do:

let lens : lens<person, address> = lens(     extract: { $0.address_ },     create: { person(name_: $0.name_, address_: address(street_: $1.street_, city_: $0.address_.city_)) } ) 

or, perhaps meant:

let lens : lens<person, address> = lens(     extract: { $0.address_ },     create: { person(name_: $0.name_, address_: $1) } ) 

that way, create uses both street , city of supplied address. (it doesn't make sense me use street of address, not city.)


Comments

Popular posts from this blog

how to insert data php javascript mysql with multiple array session 2 -

multithreading - Exception in Application constructor -

windows - CertCreateCertificateContext returns CRYPT_E_ASN1_BADTAG / 8009310b -