Crell,
@Crell@phpc.social avatar

Thought experiment:

We have languages without null (eg, ), and it's been a good thing.

What would a language without booleans look like? Force things to be context-specific enums or similar.

Something to think about this weekend while avoiding your inlaws...

cebe,
@cebe@phpc.social avatar

@Crell
How does if/else work in a language without booleans?

If you can not write expressions that evaluate to true/false that would be a very big limitation.

Or maybe you disallow assigning boolean expression to variables. But still that would feel very weird to write code in that language.

syntaxseed,
@syntaxseed@phpc.social avatar

@Crell The beautiful thing about booleans is they force things to be distilled down to yes or no questions. Take that away and you have a language full of 'maybe'. 😆

I find that the language forms the thinking we do about the solutions. And the more clear the language the more clear the thoughts...

Holy shit I think I just talked myself out of using nulls... 🤣

Crell,
@Crell@phpc.social avatar

@syntaxseed Score! 🙂

But are they really all the same yes/no question? Success/Failure and Found/NotFound are not the same space, semantically. They get serialized to true/false for storage.

Just like a name and an ID are both "strings", but only as a representation. They're semantically different things.

maxtappenden,

@Crell @syntaxseed Is the only argument for this code readability? Because my IDE shows me argument names already…

Crell,
@Crell@phpc.social avatar

@maxtappenden @syntaxseed No, I think it's more semantic accuracy. Just like null is problematic because it means "out of bounds", for any bounds, which is sometimes a meaningful value, but it's hard to distinguish. Hence why I recommend replacing null returns with purpose-built enums.

But if we can do that for the unary type, can we for the binary type? Maybe? What are the implications?

It's just me musing aloud at the moment to cause trouble. 🙂

maxtappenden,

@Crell @syntaxseed I’m struggling with this because I feel like you have a point and it’s the kind of thing I’d usually be a stickler for. But I’m struggling to think of any practical value beyond, “this totally works for my particular brand of crazy and if I let myself / skip my ADHD meds, I could get completely neurotic about this.”

Crell,
@Crell@phpc.social avatar

@maxtappenden @syntaxseed "What are the practical advantages?" is what I asked in the first place. 😃

I'd speculate that it would become more natural to pass and return more domain-specific symbols. Like, return AccessDenied rather than "false", which more naturally extends to PartialAccess, or whatever. And those can be objects with methods, for polymorphism.

ramsey,
@ramsey@phpc.social avatar

@Crell @maxtappenden @syntaxseed I’ve tended to throw exceptions for this, however there are many folks these days on the anti-exception bandwagon, and I can see some of their points (i.e., giving up control on one part of your application to another, which the caller might not be aware of). So, returning an enum would be helpful in that the caller can make decisions about what to do, based on different cases of the enum, something that gets unwieldy with lots of exceptions.

Crell,
@Crell@phpc.social avatar

@ramsey @maxtappenden @syntaxseed I've written extensively elsewhere on why exceptions are a bad choice for such flow control, especially in PHP where they're so expensive.

maxtappenden,

@Crell @ramsey @syntaxseed Where do you write?

Crell,
@Crell@phpc.social avatar

@maxtappenden @ramsey @syntaxseed These days mainly on https://peakd.com/@crell, though I'm planning to migrate off that back to my own domain at some point.

I don't write often, but when I do, I think it's pretty good. 😃

maccath,
@maccath@phpc.social avatar

@ramsey @Crell @maxtappenden @syntaxseed I don't know if other languages do things differently but it greatly annoys me when exceptions are not included in a throws annotation. For all intents and purposes they are a potential return type. I don't like to use null because it lacks context, but the proper use of exceptions can't be relied upon either. Using enums makes a weird kind of sense. A bit like http status codes.

Crell,
@Crell@phpc.social avatar

@maccath @ramsey @maxtappenden @syntaxseed May I direct you toward:

https://peakd.com/hive-168588/@crell/much-ado-about-null

And its predecessor:

https://joeduffyblog.com/2016/02/07/the-error-model/

It's a lot to take in, but there's a lot of deep thought in this area.

maccath,
@maccath@phpc.social avatar

@Crell @ramsey thank you - I love this kind of thing. Although I do use exceptions I am a stickler for being explicit about the kind of exceptions that are used. There is a biiiig difference between say a runtime exception and a logic exception and a difference still between a domain exception and a systems one. I'm constantly reevaluating what this all means, though, from a practical standpoint.

dgoosens,
@dgoosens@phpc.social avatar

@Crell @maxtappenden @syntaxseed

TBH, I see no reason why Rust’s Result that you phpized already (I’m using it!) would not be able to deal with binary

All you’d need is multiple types of Success or an additional property to it to make them more specific

— For this interested, Larry wrote about null here:

https://peakd.com/hive-168588/@crell/much-ado-about-null

maxtappenden,

@Crell @syntaxseed Eh. Is this intended to be serious or just for fun?

syntaxseed,
@syntaxseed@phpc.social avatar

@maxtappenden @Crell Programming is fun!

nielsdos,
@nielsdos@phpc.social avatar

@Crell I've seen this used to describe arguments. e.g. enum CaseSensitive {Yes, No}. match_string("foo", "bar", CaseSensitive::Yes);
In this case we understand what is going on without having to look at argument names, which we can't do with: match_string("foo", "bar", true); // what does "true" mean in this context?

Crell,
@Crell@phpc.social avatar

@nielsdos Exactly where I started from. How far can we push that?

Crell,
@Crell@phpc.social avatar

@nielsdos Eg, Success/Failure, Found/NotFound, Contained/NotContained.

Those are not the same type (true/false) any more than name, address, id, and description are all "strings." And newer languages tend to make it easy to not just do "everything is a string, hope you know what it is."

nielsdos,
@nielsdos@phpc.social avatar

@Crell I guess for some things it could be annoying. e.g. has_foo() calls which are by convention yes / no questions. You'd have to find a word that works in all contexts to describe this. e.g. enum Exists may be good for one context, but not exactly the right term for another. Not sure though...

heiglandreas,
@heiglandreas@phpc.social avatar

@Crell Enum Bool {
case true;
case false;
}

If you want to be mean you make it a string or int backed enum and assing true or 1 to false and false or 0 to true 🤣

Crell,
@Crell@phpc.social avatar

@heiglandreas Sure, just like you can make an enum Null { case null; } 🙂

But what are the larger implications of encouraging people to think differently, in more context-aware ways? eg, Success | Failure, which suggests "wait, why did it fail", etc.

kboyd,
@kboyd@phpc.social avatar

@heiglandreas @Crell Here, have an eldritch horror:

enum FlexiBool {
case true;
case false;

public static function fromWeb(mixed $val): FlexiBool {
return match (true) {
FlexiBool::true == $val,
true === $val,
1 === $val,
is_string($val) && strtolower($val) == 'true',
is_string($val) && strtolower($val) == 'yes',
=> FlexiBool::true,
default => FlexiBool::false,
};
}
}

  • All
  • Subscribed
  • Moderated
  • Favorites
  • rust
  • ngwrru68w68
  • rosin
  • GTA5RPClips
  • osvaldo12
  • love
  • Youngstown
  • slotface
  • khanakhh
  • everett
  • kavyap
  • mdbf
  • DreamBathrooms
  • thenastyranch
  • magazineikmin
  • megavids
  • InstantRegret
  • normalnudes
  • tacticalgear
  • cubers
  • ethstaker
  • modclub
  • cisconetworking
  • Durango
  • anitta
  • Leos
  • tester
  • provamag3
  • JUstTest
  • All magazines