I have this declaration of an array in C#:
object[] objects = { "myString", 12, 2.06, null, "otherString" };
Now I want to declare a similar array in F#, so I try:
let objects = ["myString"; 12; 2.06; null; "otherString"]
But this gives me compile errors:
"This expression was expected to have type string but here has type int"
and
"This expression was expected to have type string but here has type float"
for values 12 and 2.06.
How should I proceed to create a similar array in F#?
asked May 9, 2015 at 20:44
1 Answer 1
In your code you are declaring a list:
let objects : obj list = ["myString"; 12; 2.06; null; "otherString"]
an array would be:
let objects : obj[] = [|"myString"; 12; 2.06; null; "otherString"|]
just add a type annotation :obj list
or : obj[]
to any of those, otherwise F# infers the type from the first element.
answered May 9, 2015 at 20:57
-
7Also note that this is not an array, but a list. Array is denoted with "bracket-pipe":
[| 1; 2; 3 |]
Fyodor Soikin– Fyodor Soikin2015年05月09日 21:15:51 +00:00Commented May 9, 2015 at 21:15 -
1@FyodorSoikin true, but for array
: obj []
annotation is also neededlet objects : obj[] = [|"myString"; 12; 2.06; null; "otherString"|]
V.B.– V.B.2015年05月10日 10:48:28 +00:00Commented May 10, 2015 at 10:48
lang-ml