I have a use case for treating a constructor for a derived type as a delegate and I can't figure out if it's impossible or I'm just incapable of working it out.
type SomeJobEvent(jobId : int, otherThing : string) =
member this.JobId = jobId
member this.OtherThing = otherThing
type SomeJobStarted(jobId : int, otherThing : string) =
inherit SomeJobEvent(jobId, otherThing)
type SomeJobComplete(jobId : int, otherThing : string) =
inherit SomeJobEvent(jobId, otherThing)
type SomeJobError(jobId : int, otherThing : string) =
inherit SomeJobEvent(jobId, otherThing)
Lets imagine that this is the model, in real life the model happens to be in C# and my code is in F#, but for brevity it's much easier to type out in F#.
And what I want to do is...
let raise eventObject =
// This is just a helper, the raise event takes obj.
eventRaiser.Raise(eventObject)
let raise jobId otherThing eventConstructor =
// Lets treat the 'event' constructor as a function
raise(eventConstructor(jobId, otherThing))
[<EntryPoint>]
let main args =
// Lets curry this function up so I don't have to pass around 1234
let raiseEventForJob1234 = raise 1234 "other thing"
raiseEventForJob1234 SomeJobStarted
raiseEventForJob1234 SomeJobComplete
Now as the constructors passed into the raiseEventForJob1234
have the same signature and are part of the same inheritance chain, it feels possible. It's just I am not sure how to make it work, it's not even that they all quack, they are actually ducks!
Edit: There is a great answer here from @tomas, but also a really useful extension from Piaste in the comments make sure you check out both.