This is a purely academic question, but riffing off of this question about type constraints. The questioner gave this as an example:
type Something<'a, 'b when 'b :> seq<'b>>() =
member __.x = 42
which f# happily compiles. Now the problem is how do you make this object??
let z = new Something<???, ???>()
type T() =
interface seq<T> with
member this.GetEnumerator() = ([] :> seq<T>).GetEnumerator()
member this.GetEnumerator() = ([] :> seq<T>).GetEnumerator() :> System.Collections.IEnumerator
let z = new Something<string, T>()
Here's one way:
open System.Collections.Generic
type Node<'a> () =
let getEmptyEnumerator () = Seq.empty<Node<'a>>.GetEnumerator ()
interface IEnumerable<Node<'a>> with
member this.GetEnumerator () = getEmptyEnumerator ()
member this.GetEnumerator () =
getEmptyEnumerator () :> System.Collections.IEnumerator
Instead of returning the empty sequence, you could implement this class to return a sequence of child nodes. I called this type Node<'a>
, because it's a fairly idiomatic way to model a tree (or a graph) in C#.
Use:
> let smth = Something<string, Node<int>> ();;
val smth : Something<string,Node<int>>
> smth.x;;
val it : int = 42
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With