With union cases without arguments like this, one thing you can do is use the Constant attribute:

1
2
3
4
type EScmd =
    | [<Constant "SETF">] SETF
    | [<Constant "NEW">] NEW
    | [<Constant "MOO">] MOO

You can then call it from JavaScript like this:

1
var x = foo("MOO");
By on 10/22/2015 2:39 PM ()

Yeah, my workaround is to use Constant 1,2,...

But this is fragile. If I change the F# code, I'd have to manually change my JS code (I know, already a problem) and my typescript code (a new problem).

BTW: the typescript d.ts file is generated nicely.

By on 10/22/2015 3:24 PM ()

Not sure I get what's fragile with the way unions are treated. If I had code that depends on my F# code, I would WANT that code to tell me what needs to change when my F# code changes. TypeScript will do that to a good extent because the declarations will change. What's fragile here is using JS. No?

By on 10/23/2015 1:37 PM ()

Yes, that's what I meant by "already a problem" for JS.

But it doesn't work at all for Typescript. Even though the d.ts file is generated, W# doesn't generate any js code to support it. For example, given the following TS snippet:

1
2
3
4
    // export var EScmd = WebSh.EScmd.Tags; // W# enum tags don't work :(
    export enum EScmd { // FRAGILE: must keep in sync with W# Union
        SETF, NEW, MOO
    }

The typescript compiler will generate the following JS:

1
2
3
4
5
    (function (EScmd) {
        EScmd[EScmd["SETF"] = 0] = "SETF";
        EScmd[EScmd["NEW"] = 1] = "NEW";
        EScmd[EScmd["MOO"] = 2] = "MOO";
    })(MyApp.EScmd || (MyApp.EScmd = {}));

whereas W# produces nothing to support Escmd.Tags

By on 10/26/2015 2:30 PM ()

Also, if W# did generate something like

1
2
3
4
5
    (function (EScmd) {
        EScmd[EScmd["SETF"] = {$:0}] = "SETF";
        EScmd[EScmd["NEW"] = {$:1}] = "NEW";
        EScmd[EScmd["MOO"] = {$:2}] = "MOO";
    })(MyApp.EScmd || (MyApp.EScmd = {}));

Then the JS code that used it like

1
	var x = foo(EScmd.MOO);

would be less(ish) fragile

By on 10/26/2015 2:58 PM ()
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us | Terms of Use | Privacy Policy | Cookie Policy
Built with WebSharper