F#:[<StructuredFormatDisplay>] 与覆盖 __.ToString()。 ¿怎么了?

标签 f# attributes record tostring

语境:
使用 dotnet 2.2.203 在容器化环境中运行 F#
在 Ubuntu 18.04 台式机上

问题:StructuredFormatDisplay在组合记录中不起作用。
错了吗?
这些是代码

[<StructuredFormatDisplay("{SizeGb}GB")>]
type Disk = 
    { SizeGb : int }
    override __.ToString() = sprintf "<%dGB>" __.SizeGb

[<StructuredFormatDisplay("Computer #{Id}: {Manufacturer}/{DiskCount}:{Disks}")>]
type Computer =
    { Id: int
      mutable Manufacturer: string
      mutable Disks: Disk list }
      override __.ToString() = sprintf "#%d<%s>%O" __.Id __.Manufacturer __.Disks


[<EntryPoint>]
let main argv =

    let myPc =
        { Id = 0
          Manufacturer = "Computers Inc."
          Disks =
            [ { SizeGb = 100 }
              { SizeGb = 250 }
              { SizeGb = 500 } ] }

    printfn "%%O = %O" myPc 
    printfn "%%A = %A" myPc   
    0

和输出
%O = #0<Computers Inc.>[<100GB>; <250GB>; <500GB>]
%A = Computer #0: Computers Inc./3:[...GB; ...GB; ...GB]

计算机记录中磁盘记录的 %A 模式只是打印一些...点!

但是 %O 打印得很好。

最佳答案

我确认这个问题也发生在我的上下文中。

当您直接在磁盘列表上打印 %A 时,输出是可以的:

printfn "%A" [{SizeGb = 10}] // output: [10GB]

但是当磁盘列表在您的代码中被间接打印时:
[<StructuredFormatDisplay("Computer #{Id}: {Manufacturer}/{DiskCount}:{Disks}")>]

我们收到点。

我认为这是 F# 核心库的一个错误。一种解决方法可能是添加一个新的字符串属性来保存磁盘列表的格式化字符串,并改用该属性:
[<StructuredFormatDisplay("Computer #{Id}: {Manufacturer}/{DiskCount}:{DisksStr}")>]
type Computer =
{ Id: int
  mutable Manufacturer: string
  mutable Disks: Disk list }
  member this.DisksStr = sprintf "%A" this.Disks

关于F#:[<StructuredFormatDisplay>] 与覆盖 __.ToString()。 ¿怎么了?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55985835/

相关文章:

c# - 是否可以使用属性在属性更改时自动引发事件

record - 如何为 VLC 启用实验性 AAC 编码器并从麦克风录制 AAC 声音?

asp.net-mvc - F# 中具有可选参数的 MVC Controller

arrays - 如何分配 RAM 容纳不下的单个数组

python - 当子元素具有特定属性值时使用Python中的elementree xpath选择父元素

Delphi:记录构造函数与工厂函数

python - pyaudio-OSError : [Errno -9999] Unanticipated host error

.net - F# 实际上将私有(private)成员编译为 IL 中的内部成员。漏洞?

f# - 当它是类型的一部分时,函数签名是不同的(不再是通用的)

objective-c - 使用 NSDictionary 还是创建一个具有属性的类?