it would be prudent to allow βobstructingβ to bind within discriminated unions. I think the reason this is not possible is because discriminatory unions are still based on the OCaml design, while objects come from the .NET world. F # is trying to integrate the two as much as possible, but perhaps this can go further.
In any case, it seems to me that you use the discriminatory union only to implement some internal behavior like AI_Choose . In this case, you can declare the discriminated union separately and use it to implement the type of object.
I think you could write something like this:
type AiChooseOptions = | AI_Priority of list<AI> * Condition | AI_Weighted_Priority of list<AI * float> * Condition type AiChoose(aiOptions) = let mutable chosen = Option<AI>.None member this.Choose() = match aiOptions with | AI_Priority(aiList, condition) -> (...) | AI_Weighted_Priority(aiList, condition) -> (...) member this.Chosen (...) interface AI with (...)
The key difference between class hierarchies and discriminatory associations is when it comes to extensibility. Classes make it easy to add new types, while discriminatory associations make it easy to add new functions that work with the type (in your case, AiChooseOptions), so this is probably the first thing to consider when developing an application.
Tomas petricek
source share