oleg-py / better-monadic-for

Desugaring scala `for` without implicit `withFilter`s
MIT License
709 stars 32 forks source link

Variable overflow when using more than 21 assignments #49

Open fmonniot opened 4 years ago

fmonniot commented 4 years ago

Hello there,

First let me thank you for this amazing plugin. Being able to deconstruct values and weaves implicit in for-comprehension is a game changer.

I'm using this plugin to power up the main function of my application. As such, there are a lot of straight assignments (dependencies) in it. I found out that, if there are 22 or more assignments consecutively, the plugin start to reuse variable name in the rewritten code. For example:

def reproducer = for {
    v1 <- Some(1)

    x1 = 1
    x2 = 2
    x3 = 3
    x4 = 4
    x5 = 5
    x6 = 6
    x7 = 7
    x8 = 8
    x9 = 9
    x10 = 10
    x11 = 11
    x12 = 12
    x13 = 13
    x14 = 14
    x15 = 15
    x16 = 16
    x17 = 17
    x18 = 18
    x19 = 19
    x20 = 20
    x21 = 21
    x22 = 22

    v2 <- Some(2)

    useVs = s"$v1$v2"
    useXs = List(x1, x2, x3, x4, x5, x6, x7, x8, x9, x10,x11,x12,x13,x14,x15,x16,x17,x18,x19,x20,x21,x22).mkString
  } yield s"$useVs/$useXs"

will fail to compile with the error

[error] Hello.scala:6:5: not found: value x$1
[error]     v1 <- Some(1)
[error]     ^

For now we bypassed the issue by inserting an artificial <- in the middle of the assignment list, but having to explain why this no-op flatMap is present is a bit confusing :)

22 reminds me of the tuple limit, so maybe there isn't much that can be done to fix this issue. But I though reporting it for others to see would not be a bad idea.

missingfaktor commented 2 years ago

Just ran into this issue myself. :( Is a fix in progress?

mr-git commented 10 months ago

I wound one more "workaround" - add implilcit0(useless: Int) = 42 somewhere in middle of xXX vals - creates virtual "block", which allows to compile code, similar as extra flatMap created by <-

Generated code is using TupleX as "closure" - seems that unwrapping of values has some problem