0

I'm trying to follow this documentation about Lens in Arrow. However when I try to write a Lens

data class Player(val health: Int)

val playerLens: Lens<Player, Int> = Lens(
    get = { player -> player.health },
    set = { player, value -> player.copy(health = value) }
)

I get the following error:

4 type arguments expected for operator fun <S, T, A, B> invoke(): PLens<S, T, A, B>

I see that Lens<S, T> is a typealias for PLens<S, S, T, T>, so why this compilation error?

Kotlin version 1.3.50

Arrow Optics version 0.10.0

m0skit0
  • 25,268
  • 11
  • 79
  • 127
  • Couldn't reproduce your error, it works for me. Could you please provide kotlin and arrow version you are using? Or a full sample project – Evgeny Bovykin Oct 05 '19 at 10:53
  • @EvgenyBovykin Edited with the versions – m0skit0 Oct 05 '19 at 12:03
  • I've copy-pasted your code and it works fine for me: https://github.com/missingdays/ultimate-test/blob/master/src/main/kotlin/main.kt Here you can see that I'm using the same versions as you provided: https://github.com/missingdays/ultimate-test/blob/master/build.gradle#L20 – Evgeny Bovykin Oct 06 '19 at 10:23
  • @EvgenyBovykin I cloned your repo and it is working fine. Is arrow-syntax needed as well? I don't have that dependency. – m0skit0 Oct 06 '19 at 13:53
  • Using arrow-syntax doesn't change anything, still the same error. Weird. – m0skit0 Oct 06 '19 at 13:56
  • Here's a sample project with the problem: https://github.com/m0skit0/test – m0skit0 Oct 06 '19 at 14:15

1 Answers1

1

Kotlin typealias doesn't work here as expected. When calling constructor, you need to specify 4 params here:

Lens<Player, Player, String, String>(
        get = { v -> v.health },
        set = { v, value -> player.copy(health = value)  }
    )

Our don't specify them at all, compiler could infer them for you from lambda parameters types

Lens(
        get = { player: Player -> player.health },
        set = { player: Player, value: Int -> player.copy(health = value) }
    )
Evgeny Bovykin
  • 2,572
  • 2
  • 14
  • 27