usingSystem;classProgram{publicclassMovie{privateuintrating=0;publicuintRating/* Values between 0 (default) and 5 */{get{returnrating;}set{if(value>5){rating=5;/* Normalize high values to 0 - 5 scale */}else{rating=value;}}}}staticvoidMain(){varmovie=newMovie();/* Computer says no:
Movie.cs(33,8): error CS0122: `Program.Movie.rating' is inaccessible
due to its protection level
movie.rating = 1024; */movie.Rating=1024;Console.WriteLine("Movie rating equals 5: {0}",movie.Rating==5);}}
#!/usr/bin/envfsharpimoduleMovie=typemovie={rating:rating}andrating=privateRatingofuint32(* Values between 0 (default) and 5 *)letinit()={rating=Rating0u}letget{rating=Ratingvalue}=valueletsetvaluemovie=letvalue'=matchvalue>5uwith|true->5u|false->value{moviewithrating=Ratingvalue'}letmovie=Movie.init()(* Computer says no:
Movie.fsx(23,36): error FS0039: The value or constructor 'Rating' is not
defined
let movie' = { movie with rating = Rating 1024u } *)letmovie'=movie|>Movie.set1024uprintfn"Movie rating equals 5: %b"(movie'|>Movie.get=5u)
moduleMovie(Movie,create,get,set)wherenewtypeRating=RatingIntegerdataMovie=Movie{rating::Rating}{- Values between 0 (default) and 5 -}create()=Movie(Rating0)get(Movie(Rating(value)))=valuesetmovievalue=letvalue'=casevalue>5ofTrue->5False->valuevalue''=casevalue'<0ofTrue->0False->value'inmovie{rating=Ratingvalue''}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#!/usr/bin/envrunhaskellmoduleMain(main)whereimportMovie(Movie,create,get,set)main=letmovie=create(){- Computer says no:
Program.hs:9:24: ‘rating’ is not a (visible) constructor field name
Program.hs:9:33: Not in scope: data constructor ‘Rating’
movie' = movie { rating = Rating 1024 } -}movie'=setmovie1024inprint("Movie rating equals 5: "++show((getmovie')==5))