Hi,
you can do this using explicit class syntax (there are two ways of declaring classes in F# and "explicit" is more similar to the usual C# style):

1
2
3
4
5
6
7
8
9
10
11
12
13
 
// base class
type A =
  val x : int
  val y : int
  new(xv, yv) = { x = xv; y = yv }
  new(yv) = A(0, yv)  // CSharp: this(0, yv)

// inherited class  
type B = 
  inherit A
  val a : int
  new() = { inherit A(10, 20); a = 42 }  // CSharp: base(10, 20)

However, unless you want to have exactly this structure of the class (e.g. for C# interoperability), I would consider using the "implicit" syntax (which is a lot simpler to use and is more cofortable way of writing F# code). You can also use optional arguments to get the same behavior as with multiple constructors - it is also more flexible when used from F#:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
 
// Returns value of the argument 'o' or the default value 'v' 
let defaultArg o v =
  match o with | Some(v) -> v | _ -> v
  
type A1(?xv, ?yv) = 
  // get the value or default 
  let x = defaultArg xv 0 
  let y = defaultArg yv 0
  // members as in usual F# class
  member t.X = x

// creating class with optional args:
new A1(10,20)
new A1(5) // sets only "xv"
new A1(yv = 15) // sets only "yv"

type B1() =
  inherit A1(10, 20) // inherit & call the base constructor
  let a = 42
  member t.A = a

Hope this helps!
T.

By on 9/6/2008 6:15 AM ()

Thank you!
That is what I was looking for. A conclusion would be like this:
(Note: I have added a member m to derived type B.)
(Note: F# type are not an exact mirror of C# type here. They are equivalent for our OO coding. I do not know that much F# yet that how will look like these F# codes from C#).

The C# code:
class A
{
public int i;
public int j;
public int k;

public A () { }
public A (int val) { i = val; }
public A (int val_i, int val_j)
{
i = val_i;
j = val_j;
}
public A (int val_i, int val_j, int val_k)
{
i = val_i;
j = val_j;
k = val_k;
}
}

class B : A
{
public int m;

public B (int val_i, int val_j, int val_k, int val_m) : base (val_i, val_j, val_k) { this.m = val_m; }
public B (int val_i, int val_j, int val_k) : base (val_i, val_j, val_k) { }
public B (int val_i, int val_j) : base (val_i, val_j) { }
public B (int val) : this (val, -1) { }
public B () : this (-1) { }
}

The F# code:
type A =
val mutable i : int
val mutable j : int
val mutable k : int

new (?val_i, ?val_j, ?val_k) as this =
{ i = 0
j = 0
k = 0 }
then
this.i <- defaultArg val_i 0
this.j <- defaultArg val_j 0
this.k <- defaultArg val_k 0

type B =
inherit A

val mutable m : int

new (?val_i, ?val_j, ?val_k, ?val_m) =
{ inherit A(defaultArg val_i -1, defaultArg val_j -1, defaultArg val_k -1);
m = defaultArg val_m -1 }

new B(val_j = 2, val_m = 11)

(*
output will be:
val it : B = FSI_0010+B {i = -1;
j = 2;
k = -1;
m = 11;}
*)

By on 9/6/2008 9:00 AM ()
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us | Terms of Use | Privacy Policy | Cookie Policy
Built with WebSharper