Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I cast a "string" into a "option<string>"

Tags:

f#

I have a function declared as

let GetLength (value : option<string>) = 
    if value.IsSome then value.Value.Length else 0

And I have the variable

let a : string = "tom"

How do I pass a to the function GetLength?

like image 901
Jonathan Allen Avatar asked Jun 03 '09 23:06

Jonathan Allen


1 Answers

The accepted answer doesn't compile, and produces...

GetLength Some a;;

^^^^^^^^^^^^^^ error FS0003: This value is not a function and cannot be applied

F# thinks you are building a function (GetLength Some) to apply to the value a. That is because it's a functional language.

The correct form is

GetLength (Some a);;
like image 158
Stephen Hosking Avatar answered Sep 17 '22 20:09

Stephen Hosking