Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

> Thinking || allows for set operations: states.includes('VALID' || 'IN_PROGRESS')

Genuinely wondering what language that would have been valid in.



The type system in TypeScript can compose types from string literals using a similar syntax.

Obviously, this is runtime code, not a type declaration, but you can see how a new or tired/busy developer’s brain might spit this out in the wrong place.


English.

Have a brain fart one day and you'll write something like this and be confused why it doesn't work


You can do this in C#, which is similar if you squint.

    states.Any(s => s is "VALID" or "IN_PROGRESS")
I'm sure there are languages where a match-able pattern is an expression, allowing the original code basically unchanged.


You can do it in JS too:

    states.find(state => state === 'VALID' || state === 'IN_PROGRESS')
Or

    states.find(state => ['VALID', 'IN_PROGRESS'].includes(state))


This is a little further away IMO, as it doesn't have the `v1 or v2` construction.


> Genuinely wondering what language that would have been valid in.

Raku, but using “junctive or” | (which creates an any junction of its arguments, which “autothreads” on method calls producing a Junction of the results) instead of “tight or” ||.


One could abuse Kotlin to make almost this syntax valid. Something like: states.includes { +”VALID” || +”IN_PROGRESS” }

Where statestype function ‘includes’ takes a lambda that’s run in states context and overrides plus operator for string to evaluate to states.contains(that string). One could, but should they?


I was amazed to discover COBOL has something like this:

IF X = 0 OR = 1 OR = 2


Almost in the Raku Programming Language, bit with a single |

    states.includes('VALID' | 'IN_PROGRESS')
is effectively the same as:

    states.includes('VALID') || states.includes('IN_PROGRESS')
See https://docs.raku.org/type/Junction


> states.intersection({'VALID'} | {'IN_PROGRESS'})

Does this Python code count? Haha.


No, that’s the explicit conjunction of two explicit sets.

    ‘VALID’ or ‘IN_PROGRESS’ in states
Does not work and I’ve seen it around or SO. Or more commonly it’s variation

    a == b or c


datadog allows exactly this syntax modulo JS:

   attribute:(value OR another_value)




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: