You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
constU1=Union({A: {},B: {},});constU2=Union({A: {},Z: {},});constu1thing=U1.A(2,3);constu2thing=U2.A();functiontakesAU1(thing){returnthing.match({A: (x,y)=>x*y,[_]: ()=>0,});}takesAU1(u1thing);// U1's `.match` check validates, and we're goodtakesAU1(u2thing);// U1's `.match` check validates, but takesAU1 is not getting the thing it's expecting! this could be a hard bug.
The problem is that they only validation that match is doing is comparing strings. For safety, users have to find ways to try and make member names unique (like with namespacing: Union({U1A: {}, U1B: {}})) so that match can properly validate the options.
It would be better to compare based on a reference that comes from the union.
Here are some possible fixes:
1. with a Symbol() attached the the union
constU1=Union({A: {},B: {},});constu1thing=U1.A(2,4);u1thing.match({[U1.A.symbol]: (x,y)=>x*y,// maybe with a better prop name than 'symbol'[_]=>0,});
2. make union members classes and use an instanceof check
the problem:
The problem is that they only validation that
match
is doing is comparing strings. For safety, users have to find ways to try and make member names unique (like with namespacing:Union({U1A: {}, U1B: {}})
) so thatmatch
can properly validate the options.It would be better to compare based on a reference that comes from the union.
Here are some possible fixes:
1. with a
Symbol()
attached the the union2. make union members classes and use an
instanceof
check3. make union members match-aware
4. Put match on the union instead of its members
This last one can work because it's possible for U1 to know if any object is an instance of one of its members thanks to #31
The text was updated successfully, but these errors were encountered: