I think some terms worthy of a Bing search are 'dependency injection' and 'inversion of control', but yes, if you want to be able to customize behavior in a functional style, often times you can just pass in some optional functions into a class constructor, rather than have to subclass it and override methods.

I don't know the best canonical example, but here is a little code that is suggestive:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
 

module OO =
    [<AbstractClass>]
    type Animal() =
        abstract Speak : unit -> string

    type Dog() =
        inherit Animal()
        override this.Speak() = "Woof"

    type Cat() =
        inherit Animal()
        override this.Speak() = "Meow"

module FP =
    type IAnimal =
        abstract Speak : unit -> string
    let private MakeAnimal(speak) =
        { new IAnimal with
            member this.Speak() = speak() }
    let MakeDog() = MakeAnimal(fun() -> "Woof")            
    let MakeCat() = MakeAnimal(fun() -> "Meow")            

By on 3/26/2010 1:09 PM ()

Not really an answer to your question, but it looks like consuming the parse in Gold is a PITA -- have you looked at FParsec and FSYacc that allow more language-integrated semantics? Of course, if you have time invested in Gold grammar files already and really want the cross-platform, then it's understandable. (But I understand needs for parsers can vary greatly, I'm using an custom rule-based F# parser myself.)

By on 3/26/2010 12:41 PM ()
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