z-pattern-matching / z

Pattern Matching for Javascript
https://z-pattern-matching.github.io/
Apache License 2.0
1.72k stars 50 forks source link

Matching Consts #40

Closed EthanRBrown closed 6 years ago

EthanRBrown commented 6 years ago

Using z in a Redux project, I was very surprised to find that matches doesn't work with atomic consts:

const { matches } = require('z')

const FOO = 'foo'

matches(FOO)(
  (x = FOO) => console.log('foo is foo!'),
  (x)       => console.log('foo is not foo???')
)

This code results in "foo is not foo???", which is (ahem) not really what I was expecting.

I haven't dug into the source code to see how z works its magic, but it seems like this would be a really common use case.

leonardiwagner commented 6 years ago

Hi @EthanRBrown , this looks like the same problem at #37

please use version z@1.0.4 , and .call method with the context variables, example:

matches(FOO).call({ FOO },
  (x = FOO) => console.log('foo is foo!'),
  (x)       => console.log('foo is not foo???')
)

The problem is, the library can't have your context (with your decelerated const) unless you explicitly pass as arguments. We need to update the docs with this .call method, but honestly, I'm looking for an easier, or more elegant way to solve this problem.

I hope it works now!

EthanRBrown commented 6 years ago

Ah, thank you for the reply...I was remiss in that I searched the issues, but I didn't look through the closed issues. I agree a more elegant solution would be nice...might dig in & see if I can figure something out myself, will keep you posted if I can find the time. Thanks for all your hard work!