https://github.com/inosik/fable-csharp-import-sample
A sample that shows how to create bindings for Fable projects with C# or any other .NET language.
https://github.com/inosik/fable-csharp-import-sample
csharp fable fsharp
Last synced: 10 months ago
JSON representation
A sample that shows how to create bindings for Fable projects with C# or any other .NET language.
- Host: GitHub
- URL: https://github.com/inosik/fable-csharp-import-sample
- Owner: inosik
- License: unlicense
- Created: 2017-11-04T15:36:23.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2022-11-28T17:56:33.000Z (over 3 years ago)
- Last Synced: 2024-07-30T18:21:53.084Z (almost 2 years ago)
- Topics: csharp, fable, fsharp
- Language: C#
- Homepage:
- Size: 91.8 KB
- Stars: 8
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Fable Bindings With C\#
This is a sample which shows that we can create bindings for Fable projects with C# or any other .NET language.
## Background
As we only want to provide metadata from our assembly, we can do it with any .NET language. As long as we decorate our classes and methods with the `Import` attribute from the Fable.Core package, Fable knows what `import` statements it needs to generate. However, if we wanted to provide more than simple typed bindings, this approach won't work anymore, because Fable then would need the source code of the extra functionality, which had to be F#.
This might be useful if we wanted to reuse [Retyped][retyped] bindings with Fable.
[retyped]: https://retyped.com
## Why C\#?
I noticed that with the `extern` keyword, we can build really nice bindings for JavaScript packages. However, `extern` is somewhat limited and hard to use in F#. It can only be used with functions, and the syntax switches to a C-like style when creating such functions. In C# however, `extern` can be put on pretty much everything: instance methods, static methods, properties and even constructors!
Bindings for JavaScript classes, for example, become really straight forward to write for authors:
``` csharp
[Fable.Core.Global]
public class XMLHttpRequest
{
public extern XMLHttpRequest();
public extern void send();
}
```
And easy to use for users:
``` fsharp
let req = XMLHttpRequest ()
req.send ()
```
## Instructions
Follow these steps:
``` shell
# Restore the packages
dotnet tool restore
dotnet restore
npm install
# Prepare the bindings
# Unfortunately, project references don't work here
dotnet build -o bindings Fable.Import.LeftPad/
dotnet build -o bindings Fable.Import.Node/
# Compile to JavaScript
dotnet fable Sample/ --outDir js
# Run the sample application
node .
```