Live data from Hacker News

JavaScript Pattern Matching Proposal

github.com

221–230 of 254 posts

Re: JavaScript Pattern Matching Proposal

#221
Surprised nobody has mentioned zkat's "fork", which contains a lot more detail:

https://github.com/zkat/proposal-pattern-matching

zkat (who is also an npm employee) is a leading committer to the tc39 version, so I think there's some chance that many aspects their version will be adopted into the proposal.

Re: JavaScript Pattern Matching Proposal

#222

Earlier quoted context omitted.

I think match cases should be functions, since you have built those anyway. So instead of match (x) { expr... } you would have something like match(x, [ fn... ]) or match(x) { name: fn, name2: fn2 } where fn is like (objectToMatch) => { expr }

It could look like this: var x = match (response) { case { status:200 }: true; case { status: 404 }: false; case Number: 0; case SomeClass: 1; default: -1; };

This would introduce a weird inconsistency with cases for match and cases for switch regarding fallthrough, though. If it's going to look like cases in a switch statement, it should act like them, for better or worse.

Re: JavaScript Pattern Matching Proposal

#223

Earlier quoted context omitted.

If you mean by method-based pattern matching that you evaluate a function for each 'case'. My lib can do that do: const match = require('pmatch-js') const _ = require('lodash') const fizzbuzz = x => match(x) .when(a => a % 3 == 0 && a % 5 == 0, 'fizzbuzz') .when(a => a % 5 == 0, 'buzz') .when(a => a % 3 == 0, 'fizz') .otherwise(a => a) console.log( _.range(1, 101).map(fizzbuzz).join(' ') ) But maybe you mean somethin…

Wow, importing the whole lodash to use just one function? There’s es6 way of getting an array with a range of numbers: [...Array(100).keys()].map(v=>v+1) A little bit longer but no dependencies.

Pretty cool trick mate. Didn’t know

Re: JavaScript Pattern Matching Proposal

#225
post #213
post #110

Earlier quoted context omitted.

Even Stage 4 isn't granted it will end up on the standard, some proposals have died on Stage 4.

Any example? Since stage 3 is roughly "request for implementers", and Stage 4 requires 2 real world implementations and essentially mean "will be part of the standard in the next wave". The closest thing I can think of is ES6 modules, because the module loader spec wasn't finished, but the syntax still lived on. Now, stuff getting kicked down from Stage 3, that has happened.

I was wrong, on my mind I was thinking SIMD has been dropped during stage 4.

Re: JavaScript Pattern Matching Proposal

#226
post #148

You know that quip about C++ being an octopus made by nailing extra legs onto a dog? Is the same phenomenon somehow tasteful when it comes to ECMAScript {({..., sy: nt => ax, ...}({,})} ?

I feel like JavaScript is already a sort of functional language, so going in this direction is ok. It's the object/class additions where I think it's all going a bit C++. Start with prototypical inheritance, but with a Java-style `new` keyword that makes everything confusing. Next bolt on classes on the one hand, while correcting your prototype system `Object.create` on the other. R has 3 different object systems and…

I think that one of the reasons that classes were added to the language was that people kept implementing them anyway, so it was better to standardise. This may be a good thing: JavaScript arguably has to be multi-paradigm, more than other languages, because the user-base is so diverse.

Re: JavaScript Pattern Matching Proposal

#227
post #218

If I do the following: let visFilter = 'foo'; const newState = match (action) { {type: 'set-visibility-filter', filter: visFilter} => console.log(visFilter) } What happens? Is it checking if action.type === 'set-visibility-filter' && action.filter === 'foo' or is it destructuring the value of action.filter into visFilter?

Every example in on the readme demonstrates that using an identifier in that position becomes destructured assignment.

found the answer in the full proposal. Variables will always be assigned to, never checked for equality.

To me this is pretty average and seems like it will cause magic numbers/strings/etc if I can't use constants where it makes sense. The proposed solution is to do something like

  { status: 200 } if (x === foo) => // do something
which just seems (for this type of use case, maybe not all) like more boilerplate code for no real benefit over just doing something like

  if (res.status === 200 && res.x === foo) //do something
I would much prefer if there was some sort of special operator used to either check equality or to destructure when matching so it is obvious what the code is doing.

Re: JavaScript Pattern Matching Proposal

#228

This proposal will increase code coupleing by fixing the object structure, this will lead to refactoring problems and so on. A better approach would allow to match objects by their fileds while leaving the structure of the object unspecified.

Can you elaborate? What do you mean "match objects by their fields"? How does the proposal not already do that?

Maybe I did not express my self correctly, what I meant was - in it's current state the proposal rquires, when matching against an object, to specify all it's fileds, say you have an object with keys a, b, c then even if you match soley against the values of a , b you are required to specify c and by doing so notify it's existence. This poses a problem , since if you now remove c or add a filed d then the match will break failing to the default case , this means failing silently the worst that can happen. The described situation will be more then common since objects are ubiquitous to modern day js. Sadly nobody noted the existence of such a design flaw , which in my opinion is rather big since the proposal in it's current state implicitly turns objects into some kind of types , which they are not, such transformation will result only in increased coe coupleing , since anyone who matches against an object basically specifies it's type and as a consequence only doom and despare will follow to anyone involved.

Re: JavaScript Pattern Matching Proposal

#229
I just want to have ? Functionality in js like Ruby...

So I can do

let name = obj?.data[i]?.name;

And not get error can't read property data of undefined, when there is something wrong, just have undefined as value.

To fix this I've to write redudant checks for each level of nested property. Or do stuff like (obj || {}).prop....

Re: JavaScript Pattern Matching Proposal

#230
post #211

Earlier quoted context omitted.

Apparently they're overloading the meaning of the => "fat arrow" in the proposed syntax. I agree it's confusing because it's already used with lambdas in case of JS, but many languages with pattern matching do use it (eg. Scala, Rust). Other languages like Haskell and F# use the thin arrow -> instead. Pattern matching can be exhaustive or non-exhaustive . Basically, if a match construct is going to be an expression i…

I believe all these languages, if they use some arrow in lambda syntax (so, except Rust), use the same kind of arrow for pattern matching. It's an obvious syntax choice when lambda arguments also use the same destructuring syntax: why would anyone want to remember which of those is `[x,y]->x+y` and which is `[x,y]=>x+y`?

Fair point, it does make sense.
Post reply on HN