Sunday, June 12, 2011

Example of translation for Object Oriented style and Functional style.

In C#,
public class X
{
  public string S1 { get; set; }
  public string S2 { get; set; }

  public virtual string A()
  {
    return S2;
  }
}

public class Y : X
{
  public string S3 {get; set; }

  public override string A()
  {
    return S3;
  }
}

In F#,
type X = string * string
type Y = X * string
type Z =
  | X of X
  | Y of Y

let A(z : Z) =
  match z with
    | X (s1, s2) -> s2
    | Y (x, s3) -> s3

No comments:

Post a Comment