"type of expression is ambiguous without additional context" error while closing Swift

A strange type-related error appears in my Swift code:

the type of expression is ambiguous without additional context.

This happens even if I provide complete type information.

Here is the code that reproduces it.

I have 2 structures:

struct Person{
    let name_ : String
    let address_ : Address
}

struct Address {
    let street_ : String
    let city_ : String
}

Then I create a structure containing 2 functions to get and set addressa Person:

struct Lens<A,B> {
    let extract: (A)->B
    let create: (B,A) -> A
}

When I try to create an instance of the lens that receives and sets the address (in the latter case, it returns a new person with a new address), I get an error in the first close.

let lens : Lens<Person, Address> =
Lens(
    extract: {(p:Person)->Address in
        return p.address_}, // here the error
    create: {Person($0.name_,
        Address(street_: $1, city_: $0.address_.city_))})

Not only the type of the first-closure parameter is indicated in the type of lens, but also in the closure itself.

What's happening????

+4
2

, extract, create. $0 $1 - . $0.name_, $0 create B, Address, name_ Person. , :

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

Lens:

struct Lens<A, B> {
    let extract: (A) -> B
    let create: (A, B) -> A   // note, A & B swapped
}

:

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

, , :

let lens : Lens<Person, Address> = Lens(
    extract: { $0.address_ },
    create: { Person(name_: $0.name_, address_: $1) }
)

, create , . ( , .)

+3

lens :

let lens = Lens<Person, Address>(
    extract: {p in p.address_},
    create: {(a,p) in Person(name_: p.name_, address_: Address(street_: a.street_, city_: p.address_.city_))})

, create. , (= ) ( ) ( , ). , .

, ((p:Person)->Address in ...), Swift ( ) : p in ... (. ). , , $0 $1 ( ), Robs.

+2

Source: https://habr.com/ru/post/1622981/


All Articles