f# - F#: [] vs 覆盖 __.ToString()。¿ 怎么了?
问题描述
dotnet 2.2.203
上下文:在 Ubuntu 18.04 台式机上的容器化环境中运行 F#
问题: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
推荐阅读
- docker - 访问 Docker 容器内的共享库
- javascript - 如何将 php 变量传递给 php 正在执行的 javascript
- nginx - 禁用特定位置(路径)的 nginx 日志
- jquery - 移动主页有两个滚动条,一大一小,无法正常工作
- reactjs - formik connect props 扩展了缺失的属性
- spring - Bean 属性“favLang”不可读或具有无效的 getter 方法:getter 的返回类型是否与 setter 的参数类型匹配?
- angular - 如何使用角度在firebase中存储多个待办事项列表?
- ios - 如何访问另一个视图控制器的方法?
- android - 如何在 React Native(iOS 和 Android)中获取设备的当前国家/地区?
- python-3.x - 编写一个程序来计算正整数输入 n 的所有除数之和。使用 Python