dotnet / interactive

.NET Interactive combines the power of .NET with many other languages to create notebooks, REPLs, and embedded coding experiences. Share code, explore data, write, and learn across your apps in ways you couldn't before.
MIT License
2.89k stars 385 forks source link

Sharing List of F# anonymous record data to C# leads to CompilationErrorException #531

Open cartermp opened 4 years ago

cartermp commented 4 years ago

Using version 1.0.131001+ff9b940c966abf0fc4c341f432c45dc2e116b42b

I have a System.Collection.Generic.List<T> defined in F# (via the ResizeArray type abbreviation). I'm filling it with F# anonymous record data. When sharing it with C#, it blows up.

image

The same happens with an F# record type as well.

This is less important for records, but matters more for anonymous records. Anonymous records are the F# equivalent of anonymous types, and unblock the "talk to JavaScript" scenario.

The .dib file:

#!fsharp

let xs = ResizeArray<_>()

for x in 1..10 do
    xs.Add({| X = x |})

xs

#!csharp

#!share --from fsharp xs
display(xs);

#!fsharp

type R = { Value: int }
let ys = ResizeArray<_>()

for x in 1..10 do
    ys.Add({ Value = x })

ys

#!csharp

#!share --from fsharp ys
display(ys);
jonsequitur commented 4 years ago

If the destination kernel can't compile the type, we don't handle that for you:

https://github.com/dotnet/interactive/blob/master/docs/variable-sharing.md#limitations

cartermp commented 4 years ago

Okay, gotcha. There's a quirk related to anonymous records here.

#!fsharp

let xs = System.Collections.Generic.List<_>()

for x in 1..10 do
    xs.Add({| X = x |})

xs.GetType()

#!csharp
#!share --from fsharp xs

using System;
using System.Collections.Generic;

display(xs);

This fails with the same error.

However, as a compiled .NET app and within Visual Studio everything is fine.

image

image

cartermp commented 4 years ago

This is accomplished via a P2P reference so maybe that's the key thing that's missing.

But a major reason behind why anonymous records exist is that they can be used to group related data trivially to aid in ad-hoc sharing/serialization/etc. scenarios.

jonsequitur commented 4 years ago

I think the scenario makes sense but it will require some sort of translation. We would either have to load the needed references into the target kernel, or translate to new types that make idiomatic sense.

brettfo commented 1 year ago

Related to #2679