lito
lito

Reputation: 989

scalaz lens from example does not work

When i try to use Lens.lensu from scalaz.7.2.15, (i check http://eed3si9n.com/learning-scalaz/Lens.html)

case class Person(id: Int, name: String)

val idLens: Person @> Int = Lens.lensu(
  (p, id) => p.copy(id = id),
  _.id
)
val nameLens: Person @> String = Lens.lensu(
  (p, name) => p.copy(name = name),
  _.name
)

val c = idLens <=< nameLens

But i get error:

found   : Person @> String
[error]     (which expands to)   scalaz.LensFamily[Person,Person,String,String]
[error]  required: scalaz.LensFamily[?,?,Person,Person]
[error]     val c = idLens <=< nameLens

But it's the same as in example, what is bad with this code?

Upvotes: 0

Views: 32

Answers (1)

Tomas Mikula
Tomas Mikula

Reputation: 6537

You can read <=< as "after". Then

idLens <=< nameLens

means: Use idLens after nameLens. For this to work, the "input type" of idLens (which is Person) has to match the "output type" of nameLens. That's why the compiler expects LensFamily[?,?,Person,Person] (that is, output type Person). But the output type of nameLens is String, not Person.

What type do you expect c to have? If you want Person @> (Int, String), then use parallel composition:

val c = idLens *** nameLens

Upvotes: 1

Related Questions