Monocle icon indicating copy to clipboard operation
Monocle copied to clipboard

Code snippet from Lens manual does not compile

Open note opened this issue 8 years ago • 5 comments

import monocle.Lens
val streetNumber = Lens[Address, Int](_.streetNumber)(n => a => a.copy(streetNumber = n))

Fails compilation with error not found: value n. I wanted to fix that but then I noticed that actually it's somehow already mentioned in lens.md file:

// REPL bug: `error: not found: value n` if I rename _streetNumber to streetNumber
import monocle.Lens
val _streetNumber = Lens[Address, Int](_.streetNumber)(n => a => a.copy(streetNumber = n))
val streetNumber = _streetNumber

But I cannot see this snippet at http://julien-truffaut.github.io/Monocle/optics/lens.html and I blame it on tut:silent:invisible. Also it's not just in REPL - I cannot compile it with scala 2.12.2.

Would be nice to this hidden code instead of current visible one. Spent at least half an hour on that, it's really confusing.

note avatar May 12 '17 15:05 note

@note I added a test for the snippet you pasted here https://github.com/julien-truffaut/Monocle/blob/522-example/test/shared/src/test/scala/monocle/RegressionSpec.scala#L26-L32

It does work on a normal scala project, as far as I know it only fails in tut and repl

julien-truffaut avatar May 13 '17 14:05 julien-truffaut

@julien-truffaut Prepared a repository with reproducer: https://github.com/note/monocle-lens-doc-reproducer/blob/master/src/main/scala/com/example/Hello.scala

It fails with Hello2 and compiles without Hello2 - so it matters if streetNumber is just a local variable or rather a class field. Must be some compiler quirk. Taking into account that it's a tutorial and it's hard to predict how users will try it out I think it would valuable to have some info about it in manual.

note avatar May 13 '17 16:05 note

Thanks for creating this project. I wasn't aware of this problem. It is interesting that Hello2 compiles if you add a type annotation to streetNumber:

object Hello2 {
  val streetNumber: Lens[Address, Int] = Lens[Address, Int](_.streetNumber)(n => a => a.copy(streetNumber = n))
}

I don't know what is causing this issue ...

julien-truffaut avatar May 14 '17 10:05 julien-truffaut

I too can't get official example of nested Map update to compile, neither an answer posted on SO by @julien-truffaut. I can open another issue if need be, but thought of commenting here. https://stackoverflow.com/q/47786656/839733

I think the problem is exasperated by having the examples module as part of the main project. In order to figure out what minimal plugins and dependencies are required, I need to go through a giant build file with hundreds of lines of code.

asarkar avatar Dec 13 '17 19:12 asarkar

@asarkar it seems that someone answered your stackoverflow question

julien-truffaut avatar Dec 14 '17 08:12 julien-truffaut