question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Formatter does not work on generic type

See original GitHub issue

Describe the bug

I tried to format a record with generic type 'T. See below the type definition and register function.

type TestType<'T> =
  {
    Name: string
    Value: 'T
  }

let v1:TestType<float> = { Name = "Test"; Value = 1.0 }
let v2:TestType<string> = { Name = "Test"; Value = "String" }

let registerTestType (s:TestType<_>) (writer:TextWriter) =
    let headers = new ResizeArray<IHtmlContent> ()
    headers.Add(th.innerHTML(i.innerHTML("TestType")))
    let rows = ResizeArray<ResizeArray<IHtmlContent>>()
    let cells = ResizeArray<IHtmlContent>()
    cells.Add(td.innerHTML(s.Name + ": " + (s.Value.ToString())))
    rows.Add(cells)
    let t =
        table.innerHTML(
            [|
              thead.innerHTML(headers)
              tbody.innerHTML(rows.Select(fun r -> tr.innerHTML(r)))
            |])    
    writer.Write(t)

See the screenshot below for the Jupyter output. The following code does not register the format of the generic type

Formatter<TestType<_>>.Register( (fun x writer -> registerTestType x writer), mimeType = "text/html")

I had to register types explicitly to get it working

Formatter<TestType<float>>.Register( (fun x writer -> registerTestType x writer), mimeType = "text/html")
Formatter<TestType<string>>.Register( (fun x writer -> registerTestType x writer), mimeType = "text/html")

Did this error occur while using dotnet try or online?

  • dotnet-try
  • online

What kind of error was it?

  • User Interface (UI): For example the output never displayed
  • Service Error: For example “The service is temporarily unavailable. We are working on it”
  • Other:

Screenshots

image

Please complete the following:

  • OS
    • Windows 10
    • macOS
    • Linux (Please specify distro)
    • iOS
    • Android
  • Browser
    • Chrome
    • Edge
    • Safari

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
jonsequiturcommented, Nov 8, 2019

We didn’t anticipate this usage but it makes sense. @brettfo and I were discussing how this should be improved.

In C#, this doesn’t compile:

Formatter<TestType<>>.Register((x, writer) => { } );

We do have an API that looks like this:

Formatter.Register(typeof(TestType<>), (x, writer) => { } );

BUT this API doesn’t work yet for non-instantiable types (e.g. interfaces and open generic types).

I’m working on changing that in dotnet/try#611.

1reaction
jonsequiturcommented, Nov 21, 2019

Work on the extensibility model is in progress and we’d appreciate feedback. You can see an example here: https://github.com/eerhardt/DotNetInteractiveExtension

Read more comments on GitHub >

github_iconTop Results From Across the Web

Problem with using a defined TypeScript Generic Type in ...
First, let's fix that Column<T> definition so it properly types the formatter property. It may be just a typo, but I wanted to...
Read more >
Ugly formatting for Typescript generic type type assertion
Heads up I submitted PR #4219 that will fix this if accepted.
Read more >
[Code Formatter] lines with declarations with generic types ...
Created attachment 236685 [details] nested classes/methods with generic type parameters that won't wrap The lines in the attached text do not wrap to...
Read more >
Generic Formatter Output - Win32 apps
The following table lists the generic format output for PROP_TYPE_BYTE data type properties. In the example output column, the value of the data ......
Read more >
Generic Types - Learning the Java Language
A generic type is a generic class or interface that is parameterized over types. The following Box class will be modified to demonstrate...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found