JsLIGO improvements
Type system
-
remove need of writing
as foo
notations (@alistair.obrien + @SanderSpies for syntax) - add support for iterating over maps (@lesenechal.remi, !1949 (merged))
- remove need for adding annotations for recursive functions (@alistair.obrien, !1960 (merged))
- allow for global mutable variables (@alistair.obrien)
-
simplify
let z : list <int> = list([2, ...y]);
intolet z : list <int> = [2, ...y]
;
Syntax
-
Add support for match expression (see: https://github.com/tc39/proposal-pattern-matching) + deprecate match functionExtend switch statement with pattern matching capability.Add support for discriminatory unions. (@SanderSpies, !1973 (merged)) - Destructuring in function parameters (@SanderSpies, !2002 (merged) + !2011 (merged))
- Ternary operator (@SanderSpies, !1993 (merged))
- Improve if else (#1456 (comment 1098253838)) (@SanderSpies, !1991 (merged))
-
Check possibility to use
return
inswitch
statements. (@SanderSpies, !1995 (merged))
Modules
- Modules + syntax: replace import directives with proper JS like imports (@pewulfman + @SanderSpies for syntax: !2009 (merged))
Stdlib
- Add List.find_opt (@alistair.obrien , !1953 (merged))
Designs
- Show closed items
Activity
-
Newest first Oldest first
-
Show all activity Show comments only Show history only
- Sander assigned to @SanderSpies
assigned to @SanderSpies
- Rémi marked the checklist item add support for iterating over maps (@lesenechal.remi, !1949 (merged)) as completed
marked the checklist item add support for iterating over maps (@lesenechal.remi, !1949 (merged)) as completed
- Sander marked the checklist item add support for iterating over maps (@lesenechal.remi, !1949 (merged)) as incomplete
marked the checklist item add support for iterating over maps (@lesenechal.remi, !1949 (merged)) as incomplete
- Sander marked the checklist item add support for iterating over maps (@lesenechal.remi, !1949 (merged)) as completed
marked the checklist item add support for iterating over maps (@lesenechal.remi, !1949 (merged)) as completed
- Sander changed the description
Compare with previous version changed the description
- Sander changed the description
Compare with previous version changed the description
- Contributor
//missing find_opt function
let find_opt : (f : (x : _a) => bool, xs : list<_a>) => option<_a> = (f : (x : _a) => bool, xs : list<_a>) : option<_a> => match(xs ,list([ ([] : list<_a>) => None(), ([x,... xs] : list<_a>) => { if(f(x)) { return Some(x); } else {return find_opt(f, xs); } } ]));
Edited by Benjamin Fuentes Collapse replies - Author Contributor
I've added this one to the list.
- Author Contributor
Implemented.
- Contributor
- Contributor
Same comments apply for
Set
- Contributor
How can we simplify this code that actually works but is almost unreadable ?
const _test_mutation = () : bool => { let log = (mutations : list<[bool,mutation]>) : bool => { if(List.size(mutations) == (1 as nat)) {Test.log("Mutation issue found"); Test.log(Option.unopt(List.head_opt(mutations))); return false;} else return log(Option.unopt(List.tail_opt(mutations))); }; return match(Test.mutation_test_all(PokeGame.main,_tests) , list([ ([] : list<[bool,mutation]>) => {Test.log("No mutation errors"); return true;}, ([head,...tail] : list<[bool,mutation]>) => { if(List.size(tail) == (0 as nat)) { Test.log(head); return false;} else return log(tail); } ])); }
Edited by Benjamin Fuentes - Owner
Ideally we should be able to remove almost all of those annotations with the new type system
- Sander changed the description
Compare with previous version changed the description
- Contributor
replace
match
byswitch
Collapse replies This goes in favor of
deprecate match function Extend switch statement with pattern matching capability
instead ofAdd support for match expression
Ideally i would be very happy to have jsligo that is valide TS. This would be a valide pattern matching in both with this feature
type a = ["a", string]; type b = ["b", string]; type c = ["c"]; type t = a | b | c; const snd = (tuple : t) : string => { switch (tuple[0]){ case "a": { return tuple[1]; } case "b": { return tuple[1]; } case "c": { return ""; } } };
I support it but doesn't seem that simple.
Whatever I'm not in favor to implement a Stage1 proposal.
If we cannot have pattern matching exhaustiveness with switch, i would recommand to keep the situation as is, and reconsider this feature when we either have time to implement switch or when match expression is at least stage3 of TC39
Edited by Thomas Haessle 1- Author Contributor
Discriminated unions have been implemented, and although they are a bit of a hack and have some limitations, I believe this is good enough for the audience.
- Contributor
Destructuring in function parameters <= yes, because we need to pass always a table of fields as unique parameter of a function, would be better to have several parameters
i.e :
func([arg1,arg2,arg3])
to befunc(arg1,arg2,arg3)
on func definition, this allows to haveconst func = (arg1 : type1,arg2 : type2,arg3 : type3) : typeReturn => {...}
Collapse replies I think it is about destructuring records & lists, destructuring on tuple is already ok.
//works in 0.50.0 const tuple_params = ([a,b] : [int, string]) : string => b; const params = (a: int, b:string) : string => b; // would work with param destructuring const record_params = ({a,b} : {a: int, b: string}) : string => b; const list_params = ([a,b, ...rest] : list<string>) : string => b;
- Contributor
Context is while defining a function with several arguments, we are obliged to use
func([arg1,arg2,arg3])
and would be nice to allowfunc(arg1,arg2,arg3)
- Author Contributor
Destructuring of objects in function parameters have been implemented.
Tuples was already possible.Correction: tuples was not possible, but have now been implemented.
Edited by Sander
- Contributor
is it possible to have implicit casting sometimes with no compiler error ? Like this example :
let b: int = (5 as nat) * 5;
failwith("Address"+Tezos.get_source()+" is not an admin")
Edited by Benjamin Fuentes 1 - Contributor
is it possible to use the spread syntax without list keyword ?
let larger_list: list<int> = list([5, ...my_list]); // [5,1,2,2]
replaced by
let larger_list: list<int> = [5, ...my_list]; // [5,1,2,2]
Does this work also with 2 lists as argument ? OR does exist a List.concat() function to do this?
ps : same questiosn applies for "Set"
Edited by Benjamin Fuentes 1 - Contributor
This is a bit heavy :
let my_set: set<int> = Set.literal(list([3, 2, 2, 1]));
why not
let my_set: set<int> = [3, 2, 2, 1]
? <= btw, here the compiler could detect that there is an issue because one value is duplicated 1 Collapse replies ligo would not allow implicit casting
let my_set: Set<number> = [3, 2, 2, 1]
is not valid TS
- Contributor
ok
- Contributor
General question about Exceptions :
- when failwith is reached, there is no way of catching it , right ? Is is possible to introduce try/catch mechanism ?
- can we "classify" Exception ? meaning a function can throw different type of Exception. type failwith = ["UserNotFoundException"] | ["InvalidAddressException",address] ?
Collapse replies It would be interesting but I don't think there is a way to interecpt runtime errors in Michelson?
- Contributor
Would be one day ? or it is designed to never have it ?
Probably not. There is only 2 possibilities for an exception in MICHELSON:
- Explicit FAILWITH
- CREATE_CONTRACT that fail.
Which means except for functional error management you can mostly be managed by if/else
- Contributor
in typescript we can have
UNION
of typesIs it something similar to jsligo Variant ?
jsligo : type coin = ["Head"] | ["Tail"];
typescript : type coin = Head | Tail;
If variant is more a
enum
than an union type, is typescript syntax for variant possible to do and still work?enum coin { Head= 'Head', Tail= 'Tail' }
Collapse replies The jsligo syntax in your exemple is a union of nuplets (here with 1 element) . It is also valid TS so that's fine to me.
Enum doesn't give more. It's a variant literal string or number. They are not very useful even in TS.
In TS you have discriminated union that give a more elegant way to emulate variants with parameters, but this require to have literal string types. I'm not sure ligo want / can manage this type?
Edited by Thomas Haessle