strangelights.com

Main F# Site

Edit Edit
Print Print
Recent Changes Recent Changes
Subscriptions Subscriptions
Lost and Found Lost and Found
Find References Find References
Rename Rename
Search
List all versions List all versions
Object Expressions
.

Check out the formatting tips on the right for help formatting and making links.

Use the template below:

Summary

Object expressions generate instances of classes and interfaces. There are both simple object expressions (which do not generate a new private class under the hood) and more sophisticated object expressions that specify overrides and implement interfaces. Simple object expressions use the syntax "new":

  let myForm = new System.Windows.Forms.Form()
  myForm.Visible <- true
  myForm.Activate()

Object expressions that implement an interface use the syntax "{ new InterfaceType with ... and ... }":

  let myComparer = 
    { new System.Collections.Generic.IComparer<string>
        with Compare(a,b) = compare a.Length b.Length }

Object expressions that extend a class use the syntax "{ new ClassType(arg) with ... and ... }".

   { new System.Windows.Forms.Form() 
       with OnPaint(args) = Printf.printf "OnPaint\n"
       and  OnResize(args) = Printf.printf "OnResize\n" }

Object expressions may capture variables.

  let myObjectGenerator(n) =
    {new System.Windows.Forms.Form() 
      with OnPaint(args) = Printf.printf "OnPaint: n = %d\n" n
      and  OnResize(args) = Printf.printf "OnResize: n = %d\n" n }

Object expressions may reference members of the class being extended using "base":

    {new System.Windows.Forms.Form() as base
      with OnPaint(args) = base.OnPaint(args);  Printf.printf "OnPaint\n"
      and  OnResize(args) = base.OnResize(args); Printf.printf "OnResize\n" }

Object expressions may give additional interface implementations. The type of such an object expression is the type of the class.

  { new System.Collections.Generic.IEnumerator<string>
      with ...
      and  ...
    interface System.Collections.IEnumerator
      with ...
      and  ...
    interface System.IDisposable
      with ... }
Welcome to F Sharp Wiki, view the HomePage

This site supports the new NoFollow anti-spam initiative.

Recent Topics

Copyright 2005, Robert Pickering (Others where credited) | Terms of Use