5

Github WIP: Prototyping Erased Union types by Swoorup · Pull Request #10566 · do...

 3 years ago
source link: https://github.com/dotnet/fsharp/pull/10566
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Contributor

Swoorup commented on Nov 29, 2020

edited by dsyme

RFC https://github.com/fsharp/fslang-design/blob/master/RFCs/FS-1092-anonymous-type-tagged-unions.md

I am currently experimenting hacking the compiler to support union types. I'll admit I don't have much knowledge of the F# compiler and this is a merely an exploration to see if the features would fit well within F#.

What currently works:

  • Type Declaration (Commutativity and Associativity)
    type A = (int | (int64 | int16) is same as ((int | int64) | int16)
    type C = (int | string) same as (string | int)
  • Subtyping rules:
type I = interface end
type IA = interface end
type IB = interface end

type AA = AA of int interface IA
type BB = BB of int interface IB
let aa = AA(10)
let aabb: (AA|BB) = aa
aabb :> (IA|IB) // should pass
aabb :> (IA|IB|string) // should pass
aabb :> I // should fail
aabb :> IA // should fail
aabb :> (IA|int) // should fail

What doesn't:

  • Some issues appear to exist when interacting with anonymous records.
  • No exhaustive match checks, no union types within match cases.
  • Would be nice not to have :> _ every time to convert to union type.
  • Naked Generics, following doesn't work (Should likely be disallowed)
     let formUnion<'a, 'b when 'a :> ('a |'b) and 'b :> ('a | 'b)> (a: 'a) (b: 'b) = a :> ('a| 'b)
     let formUnion2<'a, 'b> (a: 'a) (b: 'b) = a :> ('a| 'b) // yields constraint issue

Links:


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK