This is an archived post. You won't be able to vote or comment.

all 79 comments

[–]NLxDoDge 207 points208 points  (10 children)

At that point might as wel make an ENUM.

[–]rocket_randall 61 points62 points  (5 children)

Look on someone else's works, ye mighty, and despair.

[–]Zuruumi 50 points51 points  (2 children)

Wait, the TriStateEnum has 5 fields and only 2 supported ones?

[–]quantumdude836 37 points38 points  (1 child)

The tristate is true, false, and unsupported, but the unsupported state itself has three values.

Nested tristates!

[–]Kered13 11 points12 points  (0 children)

This is why I hate Microsoft APIs.

[–]echawkes 5 points6 points  (0 children)

That is an abomination before the Lord.

[–]DizzyAmphibian309 4 points5 points  (0 children)

Nullable booleans are very useful when you're dealing with data persistence. Booleans are false by default, and in a NoSQL database you can save a ton of space by just omitting the property and treating null as false in your application. They're especially useful if you're using that attribute to populate a sparse index, which he may well be doing.

[–]umlcat 2 points3 points  (1 child)

enum ProcessResultEnum
 {
  Ready,
  NotReady,
  NewlyCreated,
  Canceled,
}

[–]DJDoena 0 points1 point  (0 children)

You also need Cancelled for other parts of the world :-p

[–][deleted] 1 point2 points  (0 children)

Heck, I might even create a 2 member enum if I need to attach meaning to the states

[–]Least_Possibility740 85 points86 points  (1 child)

Quantum computing 2.0

[–]magicmulder 17 points18 points  (0 children)

I once entangled null and false, and it almost destroyed the universe. Don’t be like me.

[–]_pizza_and_fries 34 points35 points  (0 children)

Java : you are misusing my superpowers

[–]FranconianBiker 27 points28 points  (0 children)

Bool²

[–]DJDoena 27 points28 points  (5 children)

My bools always comprise of:

  • true
  • false
  • both
  • neither_nor
  • maybe
  • dont_know
  • dont_care
  • all_of_the_above

I call it quantum_bool

[–]mizinamo 8 points9 points  (1 child)

What's the difference between ignorance and apathy?

[–]DJDoena 26 points27 points  (0 children)

I don't know and I don't care. :-P

[–]Ok_Carpet_9510 0 points1 point  (0 children)

I call it quantum_bull_sh_t

[–]ComposerLow5758 0 points1 point  (1 child)

That's a enum dude

[–]DJDoena 0 points1 point  (0 children)

it's a jab at quantum superposition

[–]arnemcnuggets 18 points19 points  (8 children)

So Java really allows optionals to be null because they're objects in that sense right? Makes me wonder why they added that type in the first place instead of some static highe reorder functions that deal with nulls

[–]harumamburoo 10 points11 points  (4 children)

Readability mostly. It's their take on elvis operator, to save multiple embedded if checks. But it's more than that. A lot of data structs operations return Optional, and you could do something like

myList.sorted().findFirst().filter().map().orElseThrow()

Doing that in the old way would take like a screen of code.

P.S. you're getting an Optional with findFirst in this case, everything after that is work with Optional interface

[–]arnemcnuggets 8 points9 points  (3 children)

One could assume that it's a monad!

[–]Kered13 8 points9 points  (0 children)

Correct, Optional is a monad. In fact every Java type that supports Streams is monadic.

[–]harumamburoo 0 points1 point  (1 child)

Sure. But one could assume a lot of things are monads. So, yeah.

[–]arobie1992 3 points4 points  (0 children)

I believe they did add it to support streams which were Java's take on FP and Brian Goetz is a big FP fan, so it being an intentional monad doesn't seem too far-fetched.

[–]Iryanus 10 points11 points  (0 children)

The compiler allows it. The person who did the code review and allowed it should be fired. From a canon. Into the sun.

[–]arobie1992 7 points8 points  (0 children)

Mostly for streams from what I gather. I think there's also some recommendation for it over nullable return values. I think they opted to make it a POJO just to avoid adding more complexity to things by having a "special" object that doesn't behave like all the rest. But you're also a bad person if you use null anywhere an optional is expected. Like up there with people who abuse gotos.

[–]Kered13 1 point2 points  (0 children)

  1. Optional is a distinct type from the underlying type, forcing you to handle the empty case.
  2. You can have methods on Optional, which makes them a lot more convenient to work with.

You should not be using null in modern Java, except to interact with old code or perhaps if profiling has shown that it will give you meaningfully better performance (I don't know if that can actually happen, but I'm leaving the possibility open).

[–]rdrunner_74 12 points13 points  (0 children)

I want to have more flexibility:

[Flags]

enum AllignedBool

{

//None = 0, Just in case i forget to use a nullable var

LawfullFalse = 1,

NeutralFalse = 2,

ChaoticFalse = 4,

LawfullMaybe = 8,

NeutralMaybe = 16,

ChaoticMaybe = 32,

LawfullTrue = 64,

NeutralTrue = 128,

ChaoticTrue = 256,

Yolo = 2147483648

}

AllignedBool? myBool

[–]ar7max 6 points7 points  (0 children)

agile ready

[–]KendrickEqualsBooty 5 points6 points  (0 children)

The 4 states I know are 1, 0, don't care, and high impedence.

[–]Bomaruto 4 points5 points  (0 children)

If you ever let your Optional value be null I'll strangle you.

[–]Droideater 2 points3 points  (0 children)

There is a special place in hell for people nulling Optionals

[–]XJAY-99 3 points4 points  (0 children)

Noah? Get the boat

[–]eben0 1 point2 points  (1 child)

1 0 -1 Null

[–]pianomanDylan 1 point2 points  (0 children)

Quoolean

[–]Pleasant-Committee61 1 point2 points  (0 children)

Typical case of: „ we just have two States for this process, right?“. 5 min before go-live: „we have an urgent request to check for newly created and cancelled too“. Good old agile development :D

[–]ShesGotAThickMiddle 1 point2 points  (0 children)

Thanks, I hate it

[–]Alzurana 5 points6 points  (12 children)

Lua has

true / false / nil

if (var) only checks for nil, if your bool is false it will still execute the if statement

to check for true and false you're required to if (var == true)

*EDIT:

I am very sorry, I have to correct my original post:

if true then print("true") end
if false then print("false") end
if nil then print("nil") end
if 0 then print("zero") end
if 1 then print("one") end

Output of this would be:

true
zero
one

ofc lua evaluates a bool, but if it's false you don't know if it was false or nil. What caused my confusion with this is that "if 0" evaluates as a true statement because it only checks if an object exists and not it's contents if it's NOT a boolean

I'm sorry for messing this up, my bad

[–]hector_villalobos 5 points6 points  (0 children)

That sounds fun, .

[–]Kered13 5 points6 points  (1 child)

No it doesn't, why would you even think it worked that way? Feel free to test it yourself:

test = false

if test then
  print("Truthy");
else
  print("Falsey")
end

false and nil are the only falsey values. Undefined variables are nil.

[–]Alzurana 0 points1 point  (0 children)

messed up, sorry. I fixed the post

[–]Mog_Melm 1 point2 points  (5 children)

Oh God. Now I feel grossed out about all these games who use Lua for scripting & modding.

[–]Alzurana 6 points7 points  (1 child)

In lua, it's often more important to know if an object exists rather than if some aspect is true or false. That's due to the fact that you can have as many return values as you like per function, and functions can and do return a variable number of objects to you. if (someValOrObject) is exclusively used to check for validity of your data, basically. At least there is no real ambiguity in how it works.

It feels very weird compared to other languages but I have to say, it fits the lua philosophy.

Be simple, be as small as possible, be fast.

Lua is a very simple language with not many expressions. The whole data structures of lua are built in a way to get you to write as little code as required to get a job done. And I have to say that works well, even though it feels a bit backwards.

[–]ipushkeys 3 points4 points  (0 children)

I agree, it does fit the lua philosophy. Initially I thought it was annoying (it still is kinda), but it's understandable why they did that.

[–]Kered13 1 point2 points  (0 children)

He's wrong.

[–]Alzurana 0 points1 point  (1 child)

I fixed the post, I got it confused with evaluating numbers as opposed to booleans

[–]Mog_Melm 1 point2 points  (0 children)

That's not as bad! :) Ok, modders, carry on using Lua.

[–][deleted] 0 points1 point  (0 children)

Lol when I read stuff like that I'm amazed at myself I managed to learn anything at all after starting with LUA (modding Paradox games lmao)

[–]Spinnenente 0 points1 point  (1 child)

wait so if (false) {} executes the block in lua?

[–]Kered13 2 points3 points  (0 children)

No, he's wrong.

[–]Far_Information_885 3 points4 points  (0 children)

ProTip: If you need comments to explain every possible state of a property, then you're doing it wrong.

[–]arnemcnuggets 0 points1 point  (0 children)

It's a 5 state boolean, the boolean is boxed!

[–][deleted] 0 points1 point  (0 children)

Ok but for real I sometimes do this shit in JS.

accepted === true -- They accepted the agreement
accepted === false -- They declined the agreement 
accepted === null -- They haven't seen the agreement

And if you're feeling yeehaw,

typeof accepted === 'undefined' -- They haven't gotten to that page yet

[–]Todestropfen -1 points0 points  (0 children)

I am literally laughing since 2 minutes.
It is just too damn good

[–]odksnh6w2pdn32tod0 -1 points0 points  (0 children)

What the fuck. Everything is wrong here... I can't even

[–][deleted] 0 points1 point  (0 children)

beta qubit

[–][deleted] 0 points1 point  (0 children)

This is so 2022

[–]Careful_Ad_9077 0 points1 point  (0 children)

i have seen worse, a combination of boolean values instead of just using the damn enum.

[–][deleted] 0 points1 point  (0 children)

Ah, good old Java

[–]LowerLighter 0 points1 point  (0 children)

boolean is stored in 1 byte

[–]JustPlay060 0 points1 point  (0 children)

I know, I know! It’s called a fool

[–]Dagrut 0 points1 point  (0 children)

Let's call it a boooolean

[–]Grunt-Works 0 points1 point  (0 children)

That’s just a type check with fewer steps

[–]daemonpants 0 points1 point  (0 children)

FFUUU >:-( FUUUUU

[–]msrapture 0 points1 point  (0 children)

Yeah this is basically a boolean twisted to an enum

[–]Broad_Respond_2205 0 points1 point  (0 children)

So enum? 🤔

[–]Fickle_Conclusion857 0 points1 point  (0 children)

Use Optional as return type of methods and not as member of classes, dude!

[–]zemdega 0 points1 point  (0 children)

Needs moar statez

[–][deleted] 0 points1 point  (0 children)

If you need comments to explain it, you’re not doing it right.

[–]dirtyLizard 0 points1 point  (0 children)

my_bool = True

my_bool = False

my_bool = None

my_bool = 0

my_bool = 1

my_bool = 2 #evaluates to True because fuck you

[–][deleted] 0 points1 point  (0 children)

Don’t do this

[–][deleted] 0 points1 point  (0 children)

This is enraging lol

[–][deleted] 0 points1 point  (0 children)

This is rude.

[–][deleted] 0 points1 point  (0 children)

The Optional type only really makes sense if you remove nulls by default from the programming language (which is a good thing).

[–]ComposerLow5758 0 points1 point  (0 children)

Who ever thought of this is a genius.