I am working on a FAKE project using F# - I am not fluent in F# or functional first programming, and am mostly doing front-end formatting. I am trying to set the color of an item based off of a class variable assigned to it. I am having issues converting this into string or using it in an if statement. I know its a long-shot, but are there any F# developers out there who might have experience in this area?
the class:
type State =
| New
| Open
| Closed
| Archived
the item to be recolored:
td [ ClassName "text-center" ] [ statusTag appt.state ]
I am primarily a C# developer, so the way that F# does things is very foreign. Normally i would just create an if statement and plug the state into it with a .ToString(), then use that to restyle but I am stumped as to how to go about it using F#. Any help would be greatly appreciated.
The usual way is to examine your value with a match
expression:
let stateToColor state =
match state with
| New -> "red"
| Open -> "green"
| Closed -> "blue"
| Archived -> "yellow"
But of course you can also use an if
expression, almost like in C#:
let stateToColor state =
if state = New then "red"
elif state = Open then "green"
elif state = Closed then "blue"
else "yellow"
The latter is a bit less safe though: if you later add another State
and forget to also add it to stateToColor
, the compiler will catch you if you use the match
expression, but not for if/then/else
.