Last active
March 12, 2023 18:29
-
-
Save matarillo/b1d9ec8bb16627288b2c33917f3e5130 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type Polynominal<'a>(x : 'a) = | |
static member op_Implicit (x : 'a) = Polynominal x | |
type Rational<'a>(x : 'a) = | |
class end | |
let x : Rational<Polynominal<float>> = Rational 1.0 | |
printfn "%A" x |
というか、左辺に型を書いて右辺を少し楽するぐらいなら、全部右辺に書けばいいというつまんない結論にしかならないな。。。
type Polynominal<'a>(x : 'a) =
class end
type Rational<'a>(x : 'a) =
class end
let x = Rational (Polynominal 1.0)
printfn "%A" x
Generic Mathにからめて、こういうコードの方が良かっただろうか
type Polynominal<'a>(x : 'a list) =
static member inline One with get () = Polynominal [LanguagePrimitives.GenericOne]
type Rational<'a>(x : 'a, y : 'a) =
static member inline from x = Rational (x, LanguagePrimitives.GenericOne)
let x = Rational (3, 5) // 3 / 5
let y = Rational<_>.from 2.5 // 2.5 / 1.0
let z = Rational<_>.from (Polynominal [4.2]) // Polynominal [4.2] / Polynominal [1.0]
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
暗黙の型変換をようやくサポートしたので(Don Symeが嫌がってたんだと思う)Polynominalのほうは暗黙の型変換でいけるけど、さすがに2段以上はふつうに無理。コンストラクタのジェネリック型引数が省略できるので、
Rational
ってだけ書けばあとは左辺から補ってくれるから少し楽ではある。