[Feature Request] Helper functions for transpiled Discriminate Union cases
Closed this issue · 2 comments
HLWeil commented
growth.AddColumn(new CompositeHeader(11, [new IOType(0, [])]), [ new CompositeCell(1, ["free text"]) ]);
=
pain
Maybe we can find a better solution
HLWeil commented
F#
[<AttachMembers>]
[<RequireQualifiedAccess>]
type IOType =
| Source
| FreeText of string
static member source = IOType.Source
static member freeText(ft) = IOType.FreeText(ft)
let myVal = IOType.Source
let myVal2 = IOType.FreeText "hello"
let h1 = IOType.source
let h2 = IOType.freeText("lol")
->
JavaScript
export class IOType extends Union {
constructor(tag, fields) {
super();
this.tag = tag;
this.fields = fields;
}
cases() {
return ["Source", "FreeText"];
}
static get source() {
return new IOType(0, []);
}
static freeText(ft) {
return new IOType(1, [ft]);
}
}
export function IOType_$reflection() {
return union_type("Test.IOType", [], IOType, () => [[], [["Item", string_type]]]);
}
export const myVal = new IOType(0, []);
export const myVal2 = new IOType(1, ["hello"]);
export const h1 = IOType.source;
export const h2 = IOType.freeText("lol");
HLWeil commented
Python
def _expr11() -> TypeInfo:
return union_type("Test.IOType", [], IOType, lambda: [[], [("Item", string_type)]])
class IOType(Union):
def __init__(self, tag: int, *fields: Any) -> None:
super().__init__()
self.tag: int = tag or 0.0
self.fields: Array[Any] = list(fields)
@staticmethod
def cases() -> list[str]:
return ["Source", "FreeText"]
@staticmethod
def source() -> IOType:
return IOType(0.0)
@staticmethod
def free_text(ft: str) -> IOType:
return IOType(1.0, ft)
IOType_reflection = _expr11
my_val: IOType = IOType(0.0)
my_val2: IOType = IOType(1.0, "hello")
h1: IOType = IOType.source()
h2: IOType = IOType.free_text("lol")