Search code examples
f#suave

strange f# list notation in suave.io


In suave.io we can use choose combinator that has the Types.WebPart list -> Types.WebPart type. Examples from their website show that I can use this combinator like this:

choose
    [ path "/hello" >>= OK "Hello GET"
      path "/goodbye" >>= OK "Good bye GET" ]

This list notation seems strange as it doesn't demand semicolon separating the elements. Yet, I have not been able to use it this way in different context. So the following works:

> choose
    [OK ""
     OK ""];;
val it : Types.WebPart = <fun:choose@47>'

as well as

> choose [OK "" ;OK ""];;
val it : Types.WebPart = <fun:choose@47>

But the following don't compile:

>choose [OK "" OK ""];; //ERROR
> [OK ""
  OK ""];; //ERROR

So, how does this notation work?


Solution

  • If you look at your last example

    [OK ""
    OK ""]
    

    you should see that the in last line: OK "" is not directly below the OK in the previous line: [OK "" (it's one of to the left)

    This should give you a hint ;)

    You just have to align the elements in the same column (recommended: just use spaces)

    this is why you usually write

    [
       OK ""
       OK ""
    ]
    

    instead of beginning the first element after [ - or some like

    [ OK ""
    ; OK "" ]
    

    too but I think that's not idiomatic F#.

    remark

    this also works with record syntax:

    {
       surname = "Smith"
       givenname = "Adam"
    }