f# - Impossible to make object with a recursive type constraint? -


this purely academic question, riffing off of this question type constraints. questioner gave example:

type something<'a, 'b when 'b :> seq<'b>>() =     member __.x = 42 

which f# happily compiles. problem how make object??

let z = new something<???, ???>() 

here's 1 way:

open system.collections.generic  type node<'a> () =     let getemptyenumerator () = seq.empty<node<'a>>.getenumerator ()     interface ienumerable<node<'a>>         member this.getenumerator () = getemptyenumerator ()         member this.getenumerator () =             getemptyenumerator () :> system.collections.ienumerator 

instead of returning empty sequence, implement class return sequence of child nodes. called type node<'a>, because it's idiomatic way model tree (or graph) in c#.

use:

> let smth = something<string, node<int>> ();;     val smth : something<string,node<int>>  > smth.x;; val : int = 42 

Popular posts from this blog

php - How should I create my API for mobile applications (Needs Authentication) -

5 Reasons to Blog Anonymously (and 5 Reasons Not To)

Google AdWords and AdSense - A Dynamic Small Business Marketing Duo