gpt4 book ai didi

inheritance - 从 F# 生成类型提供程序提供的类型继承

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

我有这个基本的生成 F# 类型提供程序

[<TypeProvider>]
type MyTypeProvider(config : TypeProviderConfig) as this =
inherit TypeProviderForNamespaces(config)

let ns = "MyNamespace"
let asm = Assembly.LoadFrom(config.RuntimeAssembly)

let buildTypes (typeName:string) (args:obj[]) =
let asm = ProvidedAssembly()
let srvName = args.[0] :?> string
... omitted
let provided = ProvidedTypeDefinition(asm, ns, typeName, Some typeof<MyRuntimeType>, hideObjectMethods = true, nonNullable = true, isErased = false)
let ctor = ProvidedConstructor([], (fun _ -> <@@ MyRuntimeType() @@>))
provided.AddMember(ctor)
provided
let parameters =
[ ProvidedStaticParameter("Host", typeof<string>, "") ]

let provider = ProvidedTypeDefinition(asm, ns, "MyProvider", Some typeof<obj>, hideObjectMethods = true, nonNullable = true, isErased = false)
do provider.DefineStaticParameters(parameters, buildTypes)
do this.AddNamespace(ns, [provider])

[<assembly:TypeProviderAssembly()>]
do ()

在另一个项目中,我想不直接使用提供的类型,而是通过继承它:
type Provided = MyNamespace.MyProvider<"Host123">

type Derived() =
inherit Provided() //Cannot inherit a sealed type

但是我收到一条错误消息,指出所提供的类型是密封类,因此无法从中继承。

这是设计使然还是我错过了什么?

最佳答案

这是 F# 类型提供程序 SDK 中的默认行为。您可以在 ProvidedTypes.fs 中看到用于提供的类型定义的属性。在 ProvidedTypeDefinition类(围绕第 1241-1252 行):

    static let defaultAttributes isErased = 
TypeAttributes.Public |||
TypeAttributes.Class |||
TypeAttributes.Sealed |||
enum (if isErased then int32 TypeProviderTypeAttributes.IsErased else 0)

您可以通过显式传递 TypeAttributes 来覆盖它。对于构造函数的第五个参数(您必须使用接受所有参数的构造函数)。接您后 ... omitted部分,看起来像这样:
let derivableClassAttributes = TypeAttributes.Public ||| TypeAttributes.Class

let provided =
ProvidedTypeDefinition(false,
TypeContainer.Namespace (K asm,ns),
typeName,
K (Some typeof<MyRuntimeType>),
derivableClassAttributes,
K None,
[],
None,
None,
K [||],
true,
true)

关于inheritance - 从 F# 生成类型提供程序提供的类型继承,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51033847/

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