Proposal: enhanced case clause

Sm In sbshw1 at gmail.com
Sun Jan 20 08:58:48 UTC 2019


Thank you for answer, I've checked thart proposal before.
As I know, proposal-pattern-matching contains guards(which checkes value's
property)

Yes, this proposal's new sementic seems duplicate pattern match's guard.
But, As I know, the guard can be associcated with only one pattern.
here is an example from proposal-pattern-matching:

```js

const res = await fetch(jsonService)case (res) {
  when {status: 200, headers: {'Content-Length': s}} -> {
    console.log(`size is ${s}`)
  }
  when {status: 404} -> {
    console.log('JSON not found')
  }
  when {status} if (status >= 400) -> {
    throw new RequestError(res)
  }
}

```

Okay, looks fine, but how about when we want to send different error when
error code is 5xx?
do I have to do this?

```js

const res = await fetch(jsonService)case (res) {
  when {status: 200, headers: {'Content-Length': s}} -> {
    console.log(`size is ${s}`)
  }
  when {status: 404} -> {
    console.log('JSON not found')
  }
  when {status} if (status >= 400 && status < 500) -> {
    throw new RequestError(res)
  }
  when {status} if (status >= 500) -> {
    throw new InternalServerError(res)
  }
}

```

Humm, this looks odd, we duplicate `when {status}` twice!
so, I can say: "pattern matching is good at checking value's structure.
checking it's property is small option."

I don't know which one is better choice, pushing this proposal or add this
problem as issue to proposal-pattern-matching(they have [some plans](
https://github.com/tc39/proposal-pattern-matching/blob/latest/TO_INFINITY_AND_BEYOND.md)
for future, and this can be merged into there).
and creating genenral syntax or creating specific syntax in order to solve
problem.

please help me to figure out in this problem :))
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20190120/c4632fdd/attachment.html>


More information about the es-discuss mailing list