Reputation: 1315
I'm trying to create som sample data for a project I'm doing. For that I need a simple object structure where on objects holds an array of objects. In this case a critict that can hold reviews. The code looks like this:
type Review =
{ MovieName: string;
Rating: double }
type Critic =
{ Name: string;
Ratings: Review[] }
let theReviews = [{ MovieName = "First Movie"; Rating = 5.5 }; { MovieName = "Second Movie"; Rating = 7.5 };]
let theCritic = { Name = "The Critic"; Ratings = [{ MovieName = "First Movie"; Rating = 5.5 }; { MovieName = "Second Movie"; Rating = 7.5 };] }
"the reviews" line is working fine but I can't get the "theCritict" line to work. The error I'm getting is:
Error 1 This expression was expected to have type Review []
but here has type 'a list
How do I get this to work?
Upvotes: 2
Views: 1131
Reputation: 2324
actually you're using array in your Review definition and list in the implementation..and these are really different beasts, list are inmutables (better for concurrency) and support pattern matching while arrays are mutables..I recommend you keep using list except for long collections (really longs)
if you wish keep using list you can do it:
type Review =
{ MovieName: string;
Rating: double }
type Critic =
{ Name: string;
Ratings: Review list }
here you've a more detailed list about differences between list and arrays:
http://en.wikibooks.org/wiki/F_Sharp_Programming/Arrays#Differences_Between_Arrays_and_Lists ...
Upvotes: 1
Reputation: 25516
Array literals use [| ... |]
rather than [ ... ]
which are list literals - you need something like
let theCritic = { Name = "The Critic"; Ratings = [|{ MovieName = "First Movie"; Rating = 5.5 }; { MovieName = "Second Movie"; Rating = 7.5 };|] }
This explains the error - you have used a list literal instead of an array literal
Upvotes: 4