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:
- does anyone have a short sample of how to implement an abstract class, abstract method and virtual method
- can I have overloaded constructors?
- 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
Here’s some sample code