News for hackers. Stuff that matters.
Re:Imagining a Language Without Booleans by jbreckmckye on Tuesday September 23, 2025 @01:40PM (#45350350)
Now that we have defined a language without booleans, we need some way to coalesce these optional values

We ideally want an infix function that can reduce the "truthiness" of two values.

Let us imagine this language is a Haskell-type-thing, and we can define pseudo-operators with pattern matching

    infixr 3 &&
    (&&) :: Optional -> Optional -> Optional
    Empty && _       = Empty
    _ && Empty       = Empty
    Some A && Some B = Some A
    _ && _           = Empty

    infixr 2 ||
    Some || _     = Some
    None || Some  = Some
    None || None  = None
    _    || _     = None 
Hmm, let's see how that looks

    a = b && c
    d = e || f
The good news is that we are free from the tyranny of booleans. The bad news is that we just reinvented JavaScript :-)
Re:Imagining a Language Without Booleans by kmill on Tuesday September 23, 2025 @02:48PM (#45351192)
That second operator is the <|> operator, from the Alternative typeclass.

The first one has some arbitrariness (do you take the left or right value if both are Just). But, thankfully the Applicative typeclass gives both <* and *>, which lets you choose which value you want:

  Just A <* Just B = Just A
  Just A *> Just B = Just B
(There's the possibility to merge values too, with f <$> Just A <*> Just B, which evaluates to Just (f A B). I feel like this is a "don't try to understand it, just get used to it" sort of syntax. It can be pretty convenient though.)
Re:Imagining a Language Without Booleans by BriggyDwiggs42 on Tuesday September 23, 2025 @02:00PM (#45350617)
I honestly really like those two operators in js.
Re:Imagining a Language Without Booleans by melncat on Tuesday September 23, 2025 @02:04PM (#45350676)
Don't forget about ?? as well
Re:Imagining a Language Without Booleans by jbreckmckye on Tuesday September 23, 2025 @02:33PM (#45351017)
I'm a big fan of the Elvis operator myself (.?)
Re:Imagining a Language Without Booleans by chuckadams on Tuesday September 23, 2025 @02:57PM (#45351279)
.? is null-safe property access. Elvis is ?:
Re:Imagining a Language Without Booleans by jbreckmckye on Tuesday September 23, 2025 @03:11PM (#45351428)
Oh, that makes more sense, because ?: has eyes

https://en.m.wikipedia.org/wiki/Elvis_operator

?:-)

Re:Imagining a Language Without Booleans by mock-possum on Tuesday September 23, 2025 @09:08PM (#45355009)
Mmm nullish coalescing
Re:Imagining a Language Without Booleans by zahlman on Tuesday September 23, 2025 @04:42PM (#45352480)
> The bad news is that we just reinvented JavaScript :-)

There's a whole lot more to JavaScript typing that makes it JavaScript.

After all, Python does this too (but the spellings are "and" and "or").

Re:Imagining a Language Without Booleans by Twey on Wednesday September 24, 2025 @05:48AM (#45358150)
The big difference being that ‘truthiness’ is explicitly encoded next to the value rather than being an inherent property of certain values. That's a win in my book!
Re:Imagining a Language Without Booleans by throwawayqqq11 on Saturday September 27, 2025 @03:40AM (#45393878)
> invented JavaScript

Good Job! Now do the "no scalar values" challenge. FP fanbois gonna love the unwrapping.