0

Given the following code

type Init<'a> = Init of 'a
type Right<'a> = Right of 'a
type Left<'a> = Left of 'a

type MovesBuilder(init) =
    member x.Yield(()) = Init init
    [<CustomOperation("left")>]
    member x.Left(Init v) = Left "Left"
    [<CustomOperation("right")>]
    member x.Right(Left v) = Right "Right"


let moves v = MovesBuilder(v)

I'd like to do this

let test1 = 
    moves 1 { left
        right }

But I get this error message

moves 1 { left
----------^^^^
stdin(565,13): error FS3099: 'left' is used with an incorrect number of 
arguments. This is a custom operation in this query or computation 
expression. Expected 0 argument(s), but given 1.

I also tried this

let test2 = 
   moves(1) { left
      right }

let test3 = 
   (moves 1) { left
      right }


type MovesFactory(init) =
    member self.create = MovesBuilder(init)

let test4 = 
    MovesFactory(3).create { left
      right }

Sadly all give me an error.

robkuz
  • 9,488
  • 5
  • 29
  • 50
  • 3
    It's just an indentation problem - the compiler thinks you're specifying `right` as an argument to `left`. – kvb Jan 28 '15 at 16:11
  • omg! #facepalm. Thanks – robkuz Jan 28 '15 at 16:16
  • 2
    See also [this answer](http://stackoverflow.com/a/12340269/974789) for an example of parameterized computation expressions. – Be Brave Be Like Ukraine Jan 28 '15 at 16:17
  • The compiler error aside, I can't help but wonder what this is trying to achieve. The custom operations are intended for query expressions and have *very* unintuitive semantics. They don't behave like normal F# expressions at all, messing with indentation and variable space, disallowing `if`, and the spec on them is horrid. If you need them, good luck, but if this is for something simple like readable step sequences, I recommend to simply define `type Step = Left | Right`, have walking operations typed as `Step seq`, and save yourself a lot of hassle. – Vandroiy Jan 28 '15 at 23:55

0 Answers0