Skip to content

Instantly share code, notes, and snippets.

@njlr
Created October 18, 2021 21:04
Show Gist options
  • Save njlr/b6c641535f5070f7ff3103c40e829d74 to your computer and use it in GitHub Desktop.
Save njlr/b6c641535f5070f7ff3103c40e829d74 to your computer and use it in GitHub Desktop.
type Circle<'t when 't : (static member Zero : 't)
and 't : (static member One : 't)
and 't : (static member (+) : 't * 't -> 't)
and 't : (static member (-) : 't * 't -> 't)
and 't : (static member (*) : 't * 't -> 't)
and 't : (static member (/) : 't * 't -> 't)
and 't : (static member (~-) : 't -> 't)
and 't : (static member op_Explicit : float -> 't) > =
{
Radius : 't
}
#nowarn "77" // op_Explicit
module Circle =
open System
let inline area (circle : Circle< ^t>) =
let castFromFloat (x : float) = (^t : (static member op_Explicit : float -> ^t) (x) )
castFromFloat Math.PI * circle.Radius * circle.Radius
printfn "%A" <| Circle.area { Radius = 1 }
printfn "%A" <| Circle.area { Radius = 1.0 }
printfn "%A" <| Circle.area { Radius = 1.0f }
(*
3
3.141592654
3.141592741f
*)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment