F# syntax question

Looking at the Robot Simulator exercise, where we have a Robot type

type Robot = Robot of direction: Direction * position: Position

And the turnRight function has this

let turnRight (Robot(direction, position)) = ...

2 questions:

  1. Why are parentheses needed around Robot(direction, position)?
  2. Is it more idiomatic to include or omit the return type?

I believe the parentheses are needed to avoid a syntactical ambiguity. Without parentheses you’d have

let turnRight Robot(direction, position) = ...

and the parser could interpret what comes after the function name as either two parameters (Robot and the tuple) or a constructor applied to the tuple. The parser has to solve the ambiguity somehow and, in this case, it seems to decide it’s two arguments instead of a single argument that is a constructor. Hence the parentheses.

As for 2, I’m more used to OCaml than F# but I think F# follows OCaml and other ML languages in the custom of not usually including a return type annotation in functions.

@tautologico is correct, the parameter needs to be parenthesized for the compiler’s pleasure

As for the return type, there is no real convention, but it is often considered good practice to at least annotate public facing (non-private) functions.
But it’s up to you basically!