Are there any tricks for hard coding blocks in the write area without losing pretty printable stuff?

I am looking for hard code units in my record fields. I currently have a ToString () method overriden and use the [StructuredFormatDisplay ("{AsString}"]] attribute. This worked, except that I am very sorry that I lost the wide format FSharp print (mostly offset) by the types this entry is nested in. With that said, I'm wondering if anyone knows of any tricks to achieve this.

Essentially I have this type:

type SteelMaterial =
    {Fy : float<ksi>;
     Fu : float<ksi>;
     E : float<ksi>;}

      

and want it to print like this:

SteelMaterial = {Fy = 50.0 <ksi>;
                 Fu = 60.0 <ksi>;
                 E = 29000.0 <ksi>;}

      

and the like when nested:

Section = {Section = "Section 1";
           Material = {Fy = 50.0 <ksi>;
                       Fu = 60.0 <ksi>;
                       E = 29000.0 <ksi>;};
           Transformations = null;}

      

The reason I want to do this is to document the units when doing calculations using F # formatting (via (*** include-value: mySection **)), Ifsharp, or Azure Notebooks.

UPDATE

I initially didn't include my implementation as I didn't think I was adding clarity to the question. Here, in case anyone is wondering.

[<StructuredFormatDisplay("{AsString}")>]
type SteelMaterial =
    {
    Fy : float<ksi>
    Fu : float<ksi>
    E : float<ksi>
    }   

    static member create (Fy, Fu, E) =
        {Fy = Fy; Fu = Fu; E = E}

    override this.ToString() = 
        sprintf "Steel Material =\r\n{Fy = %f <ksi>;\r\nFu = %f <ksi>;\r\nE = %f <ksi>;}"
            this.Fy this.Fu this.E

    member this.AsString = this.ToString()

      

+3


source to share


1 answer


It looks like I can get what I'm looking for by wrapping the block in DU and overriding this ToString () method.

[<Measure>] type ksi

[<StructuredFormatDisplay("{AsString}")>]
type Ksi =
    | Ksi of float<ksi>

    override this.ToString() =
        match this with
        | Ksi value -> System.String.Format("{0:0.0####} <ksi>", value)

    member this.AsString = this.ToString()

    member this.ksi =
        match this with
        | Ksi value -> value


type SteelMaterial =
    {Fy : Ksi;
     Fu : Ksi;
     E : Ksi;}

let mySteelMaterial =
    {Fy = Ksi(50.0<ksi>);
     Fu = Ksi(60.0<ksi>);
     E = Ksi(29000.0<ksi>)}

      



This has some advantages, including the ability to match the Ksi type and print the units I was looking for. The downside is the downside that comes with wrapping the values ​​in DU, which involves the user having to type Ksi(10.0<ksi>)

, which isn't too short, and then having to access the value via ".ksi".

0


source







All Articles