Are Infinite Types (aka Recursive Types) not possible in F#?

前端 未结 3 1145
南笙
南笙 2020-12-30 06:51

I was chatting with Sadek Drobi on twitter when be brought up that F# didn\'t seem to support Infinite Types. It turns out that in C# you can do something along these lines

相关标签:
3条回答
  • 2020-12-30 07:32

    Recursive record types should work as well.

    type A = { A : A }
    let rec a : A = { A = a }
    

    I'd be interested in a practical application. Or even an impractical one :)

    0 讨论(0)
  • 2020-12-30 07:43
    type d<'T> = delegate of 'T -> d<'T>  //'
    let del : d<int> = null
    let anotherDel = del.Invoke(1).Invoke(2).Invoke(3)
    

    I think you need a named type that is representable directly in CLI to break the recursion, so in F# this means you need an actual delegate type as well.

    0 讨论(0)
  • 2020-12-30 07:51

    You can also do something like

    type 'a RecType = RecType of ('a -> 'a RecType)
    

    to create a named type through which to perform the recursion. Now this works:

    let rec specialF = RecType (fun _ -> specialF)
    
    0 讨论(0)
提交回复
热议问题