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:
在我看过这个PDC会话之后,我想尝试学习一些F#。所以我认为最好的方法是用F#重写我已经在C#中完成的东西,但我的大脑只是拒绝在功能模式中思考。我有这个抽象类,它有一些抽象方法和一些虚方法。我想覆盖一些虚拟方法。这个类用C#编写并编译,我不打算用F#重写它。所以我的问题是:
- 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.
有没有人有一个如何实现抽象类,抽象方法和虚方法的简短示例
我可以重载构造函数吗?
如果我想在dll中编译它并使其可用于我的基于C#的程序,是否有任何约束。
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#?
更新:我真的很感激Brian的答案,但我仍然不清楚,所以我想澄清一下。让我们假装这是我用C#编写的抽象类,并在dll中编译。我如何在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.
一些文档适合任何人寻找一些F#资源: - 如果任何其他F#有兴趣获得我在Don Syme(F#的创建者)的博客免费章节F#Expert中找到的一些文档。您可以下载doc格式的那些。
- Real World Functional Programming by Tomas Petricek has free chapter here
Tomas Petricek的真实世界功能编程在这里有免费章节
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
Scott Hanselman的博客文章
达斯汀坎贝尔的博客
Tomas Petricek的博客
F#的地方 - hubFS
1 个解决方案
#1
14
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)
#1
14
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)