gpt4 book ai didi

attributes - F# 属性中的位置参数

转载 作者:行者123 更新时间:2023-12-02 01:20:31 25 4
gpt4 key购买 nike

我尝试在 F# 中使用位置参数创建属性,但始终失败。

type ColumnAttribute(?index:int,?name:string) =
inherit Attribute()
let mutable index = index
let mutable name = name
member x.Index
with get() = index
and set value = index <- value
member x.Name
with get() = name
and set value = name <- value

type Substance = {
[<Column(Index=1)>] Name : string
[<Column(Index=0)>] Id : int
[<Column(Name="sequence")>] Sequence : string
}

我尝试了几种不同的方法,但这是我最终选择的最接近的方法。

最佳答案

我发现您的代码存在 2 个问题

  1. 为了使其正常工作,您需要为 ColumnAttribute 定义一个无参构造函数。
  2. IndexName 的类型分别是 int optionstring option,但您需要它是intstring

尝试以下操作

type ColumnAttribute(index:int option,name:string option) =     
inherit Attribute()
let mutable index = index
let mutable name = name
new () = ColumnAttribute (None, None)
member x.Index
with get() = match index with | Some i -> i | None -> 0
and set value = index <- Some value
member x.Name
with get() = match name with | Some n -> n | None -> ""
and set value = name <- Some value

type Substance = {
[<Column(Index=1)>] Name : string
[<Column(Index=0)>] Id : int
[<Column(Name="sequence")>] Sequence : string }

关于attributes - F# 属性中的位置参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5328457/

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