Skip to content
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

Consider options for Symbol instead of strings for match keys #38

Closed
uniphil opened this issue Oct 20, 2015 · 1 comment
Closed

Consider options for Symbol instead of strings for match keys #38

uniphil opened this issue Oct 20, 2015 · 1 comment
Milestone

Comments

@uniphil
Copy link
Owner

uniphil commented Oct 20, 2015

the problem:

const U1 = Union({
  A: {},
  B: {},
});
const U2 = Union({
  A: {},
  Z: {},
});

const u1thing = U1.A(2, 3);
const u2thing = U2.A();

function takesAU1(thing) {
  return thing.match({
    A: (x, y) => x * y,
    [_]: () => 0,
  });
}

takesAU1(u1thing);  // U1's `.match` check validates, and we're good
takesAU1(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

const U1 = Union({
  A: {},
  B: {},
});

const u1thing = 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

const U1 = Union({
  A: {},
  B: {},
});

const u1thing = U1.A(2, 4);

u1thing.match([
  [U1.A, (x, y) => x * y],
  [_, () => 0],
]);

// 2A (thanks Alexei)

u1thing.match(
  U1.A, (x, y) => x * y,
  _, () => 0
);

3. make union members match-aware

const U1 = Union({
  A: {},
  B: {},
});

const u1thing = U1.A(2, 4);

u1thing.match([
  U1.A.matchThen((x, y) => x * y),
  U1[_].matchThen(() => 0),
]);

4. Put match on the union instead of its members

const U1 = Union({
  A: {},
  B: {},
});

const u1thing = U1.A(2, 4);

U1.match(u1thing, {
  A: (x, y) => x * y,
  [_]: () => 0,
});

// or like,

U1(u1thing).match({
  A: (x, y) => x * y,
  [_]: () =. 0,
});

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

@uniphil uniphil added this to the v0.8 milestone Oct 20, 2015
@uniphil uniphil mentioned this issue Oct 20, 2015
@uniphil
Copy link
Owner Author

uniphil commented Oct 21, 2015

Going with

Result.match(None(), {
  Some: n => n * n,
  None: () => 0
});

implementing at #41

@uniphil uniphil closed this as completed Oct 21, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant