Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement an abstract class in F#?

After I've seen this PDC session I wanted to try to learn a bit of F#. So I thought the best way would be to rewrite in F# something I've already done in C# but my brain just refuses to think in functional mode. I have this abstract class that has some abstract methods and some virtual methods. I would like to override some of virtual methods as well. This class is written in C# and compiled and I don't intend to rewrite it i F#. So my question is:

  1. does anyone have a short sample of how to implement an abstract class, abstract method and virtual method
  2. can I have overloaded constructors?
  3. are there any constraints if I want to compile it in a dll and make it available to my C# based programs.

Any help would be appreciated.


Update: I really really appreciated Brian's answer but it is still not clear to me so I want to clarify. Let's pretend this is my abstract class written in C# and compiled in dll. How do I implement it in F#?

public abstract class MyAbstractClass
{
    public abstract Person GetFullName(string firstName, string lastName);

    public abstract bool TryParse(string fullName, out Person person);

    public virtual IEnumerable<Person> GetChildren(Person parent)
    {
        List<Person> kids = new List<Person>();
        foreach(Child person in GroupOfPeople)
        {
            if(person.Parent == parent)
               kids.Add(child as Person);
        }
        return kids;
    }

    public virtual IEnumerable<Child> GroupOfPeople { get; set; }
}

Some documentation for whomever is looking for some F# resources: - if any other F# is interested to get some documentation I found on Don Syme's (creator of F#) blog free chapters of his book F# Expert. You can download those in doc format.

  • Real World Functional Programming by Tomas Petricek has free chapter here

Some other resources that might of interest:

  • Scott Hanselman's blog post
  • Dustin Campbell's blog
  • Tomas Petricek's blog
  • The place for F# - hubFS
  • F# WIKI
  • Microsoft Research
like image 816
Enes Avatar asked Nov 22 '08 00:11

Enes


People also ask

How do you implement an abstract class?

To implement features of an abstract class, we inherit subclasses from it and create objects of the subclass. A subclass must override all abstract methods of an abstract class. However, if the subclass is declared abstract, it's not mandatory to override abstract methods.

Can we do implement in abstract class?

An abstract class can have an abstract method without body and it can have methods with implementation also. abstract keyword is used to create a abstract class and method.

What is abstract class how it is implemented in Java?

Abstract class: is a restricted class that cannot be used to create objects (to access it, it must be inherited from another class). Abstract method: can only be used in an abstract class, and it does not have a body. The body is provided by the subclass (inherited from).


1 Answers

Here's some sample code

type IBaz =
    abstract member Baz : int -> int

[<AbstractClass>]
type MyAbsClass() =
    // abstract
    abstract member Foo : int -> int
    // virtual (abstract with default value)
    abstract member Bar : string -> int
    default this.Bar s = s.Length 
    // concrete
    member this.Qux x = x + 1

    // a way to implement an interface
    abstract member Baz: int -> int
    interface IBaz with
        member this.Baz x = this.Baz x

type MySubClass(z : int) =
    inherit MyAbsClass()
    override this.Foo x = x + 2
    override this.Bar s = (base.Bar s) - 1
    override this.Baz x = x + 100
    member this.Z = z
    new () = MySubClass(0)

let c = new MySubClass(42)    
printfn "%d %d %d %d %d" c.Z (c.Foo 40) (c.Bar "two") (c.Qux 41) (c.Baz 42000)
like image 165
Brian Avatar answered Oct 19 '22 16:10

Brian