2

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

4

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
2
  • 7
    Also note that this is not an array, but a list. Array is denoted with "bracket-pipe": [| 1; 2; 3 |] Commented May 9, 2015 at 21:15
  • 1
    @FyodorSoikin true, but for array : obj [] annotation is also needed let objects : obj[] = [|"myString"; 12; 2.06; null; "otherString"|] Commented May 10, 2015 at 10:48

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.