Here we see three object-oriented mechanisms in action inside of F#. hotel and motel are classes, class motel inherits class hotel, and the numberOfRooms is a privately encapsulated field of the motel class. Also note that we publicly expose numberOfRooms with the property NumberOfRooms.
#light
type hotel = classval name : string
new() = {name = "Hotel California"}
end
type
motel = classinherit hotel
val mutable private numberOfRooms: int
val CAPACITY : int
member x.CalculateRate = (float)x.numberOfRooms * 125.50
member x.IncrementRooms(n) = x.numberOfRooms <- x.numberOfRooms + n
member x.Vacant = (x.numberOfRooms = 0)
member x.Filled = (x.numberOfRooms > x.CAPACITY)
member this.NumberOfRooms
with get() = this.numberOfRooms
and set((v:int)) = (this.numberOfRooms <- v)
new() = { CAPACITY = 400
numberOfRooms = 0 }
end
open
Systemlet motel1 = new motel()
Console.WriteLine("***** {0} *******", motel1.name)
Console.WriteLine ("number of rooms = {0}", motel1.NumberOfRooms)
if (motel1.Vacant) then
Console.WriteLine ("The motel is empty")
motel1.IncrementRooms (5)
Console.WriteLine ("number of rooms = {0} costing {1:c}", motel1.NumberOfRooms, motel1 .CalculateRate )
if
(motel1.Vacant) thenConsole.WriteLine ("The hotel is empty")motel1.NumberOfRooms <- 400
Console.WriteLine ("number of rooms = {0} costing {1:c}", motel1.NumberOfRooms, motel1.CalculateRate )
Console.ReadLine()
The code above excercises the IncrementRoom method and the NumberOfRooms Property to simulate the occupation of a motel. It also calculates the total rate from the number of rooms occupied at any given time.
The result of running the code listed above is shown in the screenshot below.


Joeri BelisPosted Jan 28, 2012, 11:12 AM
line new() = { CAPACITY = 400 numberOfRooms = 0 } should be new() = { CAPACITY = 400; numberOfRooms = 0 }
Mike GoldPosted Jun 22, 2008, 1:03 PM
As far as object-oriented features, F# does not necessarily have a lot over Delphi, C#, etc. As far as structural features, short-cut coding, pattern-matching, language defining, thread-safety mechanisms, collection defining, instance assignments, List manipulation, .NET integration, asynchronous functions, and other cool features missing in the other languages, F# has some advantages.
Bechir BejaouiPosted Jun 19, 2008, 8:29 PM
It's very similar to Delphi in the declaration types and memebers It's very similar to vb for not using ; to mark the end of instruction It's very similar to c family according to the conditional if( ) structure and then for both Delphi and VB So what is the plus of the F#over those programming languages?????????????????????????????????????????????????