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.85k stars 381 forks source link

variable sharing between C# & F#? #3062

Open smoothdeveloper opened 1 year ago

smoothdeveloper commented 1 year ago

I wonder if the engineering work to make it happen implicitly is a lot, and if it was already considered and dismissed.

I find it unnatural to "share variables" when it is not about crossing runtimes (such as dotnet to javascript, etc.).

I should be able to define a cell with a function and a variable in F#, and use it in next cell in C#, no ceremony involved, it should work with additional CLI languages so long the infrastructure to make it happen is extensible.

I overall think it could work with netmodules, but I don't know how the dotnet backend works.

jonsequitur commented 1 year ago

We've had some discussions around this kind of implicit variable sharing. It can certainly work and we already have the APIs to do it, so it would even be possible to implement this using a .NET Interactive extension. Here's a quick naive example that shows the basic flow:

image

There are a few caveats:

These can probably be solved with enough bookkeeping and some thinking about the right user experience.

Related: #2873

@sharoncxu

smoothdeveloper commented 1 year ago

@jonsequitur, thanks this gives a nice peek at how things are wired 🙂.

I'm wondering if it would make sense to de-correlate the 1 to 1 relationship between kernel and language, and basically have a kernel per runtime, and have each "kernel as runtime" potentially support several languages?

I can see similar considerations: using javascript, typescript, and other language that target the javascript runtime together?

For example, F# in context of polyglot notebooks is in special spot, F# code can be valid in CLI, javascript, python and some other runtimes in the future.

I assume it could turn to be the same with C#.

I'm sorry this seems to challenge what lays behind the current implementation of kernels, and bring some UX challenges, if this would be considered a good route for this project.

jonsequitur commented 1 year ago

Kernel and language are uncorrelated in the sense that the .NET Interactive architecture isn't opinionated about them. It's incidental that the dotnet-interactive tool loads C#, F#, and PowerShell kernels in the same process. You can connect additional instances out-of-process using proxy kernels. You might want, for example, a C# 10 kernel and a C# 11 kernel in the same notebook. This can be accomplished by running one out-of-process and proxying it into the other. This allows them to appear as separate "cell languages" in the same notebook. Another example is the CSharpProjectKernel (i.e. the backend for the next version of Try .NET) which is a C# kernel that compiles code in one process and emits assemblies to be run using WASM in another.

I can see similar considerations: using javascript, typescript, and other language that target the javascript runtime together?

JavaScript in Polyglot Notebooks is executed in the web view context. We also have experimental Playwright-powered browser kernels where the JavaScript runs in an external browser:

image

For example, F# in context of polyglot notebooks is in special spot, F# code can be valid in CLI, javascript, python and some other runtimes in the future.

Yes! Paweł Stadnicki's work, which you might be referring to, is a beautiful example of this: https://twitter.com/florence_dev/status/1671150700259315717?s=20