See the separate entry on the ValueRestriction.
Use prefix or postfix notation in the type definition. Postfix notation uses angle brackets:
type 'a option =
| Some of 'a
| None
or
type option <'a> =
| Some of 'a
| None
The defined type can be parameterized by multiple types:
type either <'a, 'b> =
| Left of 'a
| Right of 'b
Use "new DelegateType(function-value)". This can be a named function or an anonymous function:
let myMethod() = printf "Invoked!\n" form.BeginInvoke(new MethodInvoker(myMethod) form.BeginInvoke(new MethodInvoker(fun () -> printf "Invoked!\n"))
Instances of .NET classes are created using ObjectExpressions. You may also define your own Classes, this is described in the ObjectsAndAllThat section.
Methods on .Net objects can be accessed via the . notation, which is <objectRefernceName> or <className> . <MethodName> ([parameters]*). It should be noted that .Net methods can not be called in the curried style.
Calling an instance method:
let file = new System.IO.FileInfo("c:\\test.txt")
let stream = file.CreateText()
Calling a static method:
let stream = System.IO.File.CreateText("c:\\test.txt")
Properties on .Net objects are accessed using the . notation, which is <objectRefernceName> or <className> . <PropertyName>.
Calling the get method of a property:
let file = new System.IO.FileInfo(@"c:\test.txt")
let exists = file.Exists
Calling the set method of a property:
let file = new System.IO.FileInfo(@"c:\test.txt")
let _ = file.Exists <- FileAttributes.Hidden
Events can be added add removed by calling the underlying add and remove methods. They are not accessed by the event name like in C# and VB.NET.
let form = new System.Windows.Forms.Form()
let _ = form.add_Load(new System.EventHandler(fun sender e -> new MessageBox.Show("hello there!!")))
The type of the generic parameter is specified after the type name and enclosed in angled brackets. The below example show the create of a generic list:
type MyRecord =
{ Id : int;
Thing : string; }
let myList =
let list = new System.Collections.Generic.List<MyRecord>() in
list.Add({Id =1; Thing = "Robert" });
list.Add({Id =2; Thing = "Susan" });
list.Add({Id =3; Thing = "Sable" });
list.Add({Id =3; Thing = "Venus" });
list
Often it not necessary to specify the type explicitly the compiler can usually infer it. In these cases it is only necessary to put an underscore for each generic parameter. (Types names can be overloaded by the number or generic parameters so it is import that the number of underscores matches the number of parameters). Here is a revised version of the sample:
let myList2 =
let list = new System.Collections.Generic.List<_>() in
list.Add({Id =1; Thing = "Robert" });
list.Add({Id =2; Thing = "Susan" });
list.Add({Id =3; Thing = "Sable" });
list.Add({Id =3; Thing = "Venus" });
list
Please see section on ObjectsAndAllThat
Use the cli-version 1.0 flag in conjunction with no-tailcalls.
..\bin\fsc --cli-version 1.0 --no-tailcalls hello.fs