FAQ
Last changed: -68.36.188.254

.
Summary
Note

Questions on the F# Language

Q: What is the 'Value Restriction'?

See the separate entry on the ValueRestriction.

Q: How do I define a parameterized type?

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

Questions on F# and .NET types.

Q: How to create delegates, e.g. for "Control.Invoke"?

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"))

Q: How to create a new object?

Instances of .NET classes are created using ObjectExpressions. You may also define your own Classes, this is described in the ObjectsAndAllThat section.

Q: How to use object methods?

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")

Q: How to use object properties?

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

Q: How to use object events?

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!!")))

Q: How do I work with classes with generic parameters?

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

Q: How to define new classes and new subclasses?

Please see section on ObjectsAndAllThat

Questions on F# Tools

Q: Using the no-tailcalls flag gives an error Unable to find the file fslibntc.dll. The distribution has fslib10ntc.dll, but no fslibntc.dll. What do I do?

Use the cli-version 1.0 flag in conjunction with no-tailcalls.

  ..\bin\fsc --cli-version 1.0 --no-tailcalls hello.fs