-
Notifications
You must be signed in to change notification settings - Fork 37
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use classes for the bindings? #77
Comments
TBH, I prefer the breaking changes as it would mean writing "standard" F# code instead of having to explain why there is 2 ways of doing it.
Less namespace magic seems good to me. |
Today there has been a question on F# Slack about how to support multiple constructor in F#
Is this a Fable limitation when using constructor or did it wrote it the incorrect way? Ok after trying the code into the REPL, this is not a Fable limitation but F# limitation:
|
Yes, this is tricky. First of all, there are no JS classes with multiple constructors, that's just a Typescript mechanism like the fake overloads. But the fake overloads are correctly called by Fable for classes decorated with open Fable.Core
[<Import("Color","three")>]
type Color(r: float, g: float, b: float) =
new(hex: int) = Color(0.,0.,0.)
new(str: string) = Color(0.,0.,0.)
let c = Color("foo") Becomes: import { Color } from "three";
export const c = new Color("foo"); |
Thank you for the explanations @alfonsogarciacaro |
@alfonsogarciacaro I looked at the DOM bindings of Bridge.NET and they also have proper types and values of Do you have any examples of multiple inheritance in the DOM APIs? @MangelMaxime this is what I do lately: // Private parameterless constructor, can be public if necessary
type T private () =
/// <summary>XML docs for the first constructor.</summary>
/// <param name="arg">What is arg for?</param>
new (arg: obj) =
// Call the primary constructor. No need to pass any arguments
T ()
/// XML docs for the second constructor
new (arg0: obj, arg1: obj) = T ()
// "Minimal" binding for T.Prop. This syntax is not allowed without a primary constructor
member val Prop: obj = jsNative with get, set |
@inosik For example, fable-browser/src/Dom/Browser.Dom.fs Lines 424 to 428 in 36faa2e
I think in this case the "actual" parent is Unfortunately I don't have much time to work on this atm, but if someone wants to give it a try with a small package we can see how the ergonomics work and have a feeling of how much work would a full conversion entail. |
According to MDN, I'll see if I can find a nice, machine-readable definition of the DOM/Browser APIs and see if we can go from there. |
So far we (I) have been recommending interfaces to write bindings, but in recent discussions we are considering the possibility of using classes instead. Some links:
fsharp/fslang-suggestions#1054
fable-compiler/Fable#2353
fable-compiler/Fable#2492
So I'm toying now with the idea of writing a script to automatically convert some of the bindings in this repo to classes to see how far we can go, and if the ergonomics is good enough to start recommending writing classes instead of interfaces (and whether we should change ts2fable).
Merits:
Create
method to avoid breaking changes)Demerits:
Browser.Types
andBrowser
for the values exposing the changes. But in many cases we wouldn't needThoughts? cc @MangelMaxime @Zaid-Ajaj @inosik @Booksbaum
The text was updated successfully, but these errors were encountered: