fslang-suggestions icon indicating copy to clipboard operation
fslang-suggestions copied to clipboard

Computation Expressions bug when using MaintainsVariableSpace / ProjectionParameter

Open GratianPlume opened this issue 8 months ago • 0 comments

   type AH =
      | AH

      member _.For(x, f) = f x

      member _.Yield x = x

      [<CustomOperation(MaintainsVariableSpace=true,AllowIntoPattern=true)>]
      member _.foo(x, [<ProjectionParameter>]y) = 
         let x = y x
         x, "A"

      [<CustomOperation(MaintainsVariableSpace=true, AllowIntoPattern=true)>]
      member _.select(src, [<ProjectionParameter>]f) = f src

   let foo (x: int) = 
      AH {
         for x in x do
         let n = x
         foo (n + x) 
         select n
      }

The above code compiles successfully, but the result is not the value of n, as intuitively expected, but "A". I understand why this is the case, but can we be smarter when parsing the CE?

Because if you don't consider the implementation of CE, it is easy for users to write wrong expressions.

But if the CE implementation considers how to avoid this, there is only one conclusion: do not generate tuples.

Not generating tuples often makes some code impossible to optimize.

GratianPlume avatar Jun 20 '24 04:06 GratianPlume