dotnet / dotnet/fsharp

F#: [<StructuredFormatDisplay>] in composed records just print dots. (...)

Open
#6,753 8 comments 4 reactions 0 assignees View on GitHub
Area-Library Feature Improvement
Dominant language
F#
Stars
4.3k
Forks
876
Avg merge
4d 11h
Merged PRs (30d)
131

Description

Context:
Running F# in a containerized environment with **`dotnet 2.2.203`**
F# Version: $ fsharpc
Microsoft (R) F# Compiler version 10.2.3 for **F# 4.5**

**Question: The `StructuredFormatDisplay` in a Composed Record doesn't work.**

#### Repro steps
These is the code

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

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


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

printfn "%%O = %O" myPc
printfn "%%A = %A" myPc
```
#### Expected behavior
The expected behavior just print the Diks size correctly on both lines %O and %A
```
%O = #0[<100GB>; <250GB>; <500GB>]
%A = Computer #0: Computers Inc./3:[100GB; 250GB; 500GB]
```
#### Actual behavior
On the %A line apears some dots (...) insted of the real value
```
%O = #0[<100GB>; <250GB>; <500GB>]
%A = Computer #0: Computers Inc./3:[...GB; ...GB; ...GB]
```
#### Known workarounds

One workaround could be adding a new string property holding the formatted string of the disk list, and use that property instead: ([stackoverflow](https://stackoverflow.com/questions/55985835/f-structuredformatdisplay-vs-override-tostring-what-its-wrong))
```
[]
type Computer =
{ Id: int
mutable Manufacturer: string
mutable Disks: Disk list }
member this.DisksStr = sprintf "%A" this.Disks
```
#### Related information
Running F# in a containerized environment with **`dotnet 2.2.203`**
F# Version: $ fsharpc
Microsoft (R) F# Compiler version 10.2.3 for **F# 4.5**

#### Extra issue
Additionally, in a record, if an `option field` without value, i.e. with `None` value, does not use the **StructuredFormatDisplay** attribute, it instead prints the contents of the record without showing error. and the option field shows it as **None**.

Couldn't you use the attribute and print None too?
**Example code:** take care of the mutable field Boss, is a string option.
```
[]
[]
type Company=
{ Id : int
mutable Name : string
mutable Boss : string option }
with
static member New( name: string ) = { Id = 0; Name = name; Boss = None }
....
let myCompany = Company.New("Koséi Kantà")
printfn "%A" myCompany

myCompany.Boss <- Some "String"
printfn "%A" myCompany
```
**Result:**
```
{Id = 0;
Name = "Koséi Kantà";
Boss = None;}

Company #0: Some "String"/Koséi Kantà
```
**The desired behavior would be:**
```
Company #0: None/Koséi Kantà
```

Thanks.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.