gpt4 book ai didi

f# - 如何在泛型约束类型上调用静态方法

转载 作者:行者123 更新时间:2023-12-02 09:12:10 24 4
gpt4 key购买 nike

给出这个例子:

type Base() =
static member Function() = ""

type Derived() =
inherit Base()

let get<'T when 'T :> Base> segment =
return ('T).Function()

let invoked = get<Derived>()

有没有办法从get调用Function

最佳答案

SRTP 可以帮助您:

type Base() =
static member Function() = ""

type Derived() =
inherit Base()

let inline get< ^T
when ^T :> Base
and ^T : (static member Function: unit -> string)> =
(^T : (static member Function: unit -> string) ())

let invoked1 = get<Derived> //ok
let invoked2 = get<string> //error: string not derived from Base

关于f# - 如何在泛型约束类型上调用静态方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50907288/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com