r/ProgrammerHumor • • Jan 05 '19

You know it's true

Post image
60.6k Upvotes

1.1k comments sorted by

View all comments

Show parent comments

13

u/theXpanther Jan 05 '19

Also, what is the value of x ? x = 5/2.0 : ';'

70

u/adenosine-5 Jan 05 '19

what monstrosity of a language allows that syntax?

43

u/Thalanator Jan 05 '19

my bet is on some spawn of javascript

36

u/Cobaltjedi117 Jan 05 '19

Or maybe just JavaScript

2

u/mypetocean Jan 05 '19

No, you can't check the value of x if it hasn't been declared (either lexically earlier or by hoisting), and you can't perform an assignment like that within a ternary without wrapping the assignment in parentheses.

The only place that line as a whole would not throw a runtime error in JavaScript would be in a try block — and of course even then it wouldn't do anything but pass the error to catch.

2

u/_Lady_Deadpool_ Jan 06 '19

Python would let you, albeit with its backwards format

x = 5/2.0 if x else pass

3

u/chozabu Jan 05 '19 edited Jan 05 '19

x ? x = 5/2.0 : ';'

in JS - you get:

>>x ? x = 5/2.0 : ';'
ReferenceError: x is not defined[Learn More] debugger eval code:1:1
>>x=0
0
>>x ? x = 5/2.0 : ';'
";"
>>x
0
>>x=1
1
>>x ? x = 5/2.0 : ';'
2.5
>>x
2.5

worth noting, it assigns x to 2,5 only if it currently evals to true

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator

TLDR `TEST?TRUE:FALSE`

15

u/theXpanther Jan 05 '19

It will work in Java, c#, or any other c based language as well

7

u/adenosine-5 Jan 05 '19

Are you sure?

Because x is not defined, there is no semicolon at the end and the ternary operator uses mixed variables - int, float and string/char...

So what is it supposed to do?

7

u/OmarRIP Jan 05 '19

He’s asking the value of the expression — never said it’s a statement. It’s an analogous question to “what’s the value of [the expression] 5 + 3.”

5

u/adenosine-5 Jan 05 '19

If x is not int then you try to assign 2 into it and that is not ok...

If x is int and not 0 then the expression is true, otherwise its ';'... and that is also not ok...

What I'm trying to say is that C# (and others) will just throw half a dozen compile errors at you and won't do anything

7

u/OmarRIP Jan 05 '19

Get closer to the machine level and consider how a computer actually stores values (i.e. all variables are numbers) and it’ll make sense.

It certainly works in C; claiming a syntax error isn’t really in the spirit of the question. I wrote an answer in response to the original problem comment.

2

u/ConspicuousPineapple Jan 06 '19

No it won't. The types involved aren't compatible in any of these languages.

1

u/theXpanther Jan 06 '19

this works in C#:

int y = 0;
object z = y ? (y / 2.0) : ';';

It might need some small changes to work in java to deal with primitives not being objects, but the basic idea will be the same without actually changing the types

2

u/Iron_Maiden_666 Jan 06 '19

It doesn't work in Java or C#, x needs to be a boolean and you can't assign floats to bools.

1

u/theXpanther Jan 06 '19

Assume X is declared object

2

u/OmarRIP Jan 05 '19 edited Jan 06 '19

The one that your operating system is written in; the same one that’s the foundation for practically every higher-level language.

Honestly, the C code above is pretty clean compared to the monstrosities you’ll find in C++. And the implicit type conversion are infinitely more consistent and comprehensible than what you find in JS.

1

u/nonamee9455 Jan 06 '19

Isn’t the ternary operator valid in Java?

10

u/OmarRIP Jan 05 '19 edited Jan 06 '19

This one is fun.

I’ll assume weak typing/C-syntax and rules; also going to treat this an expression rather than any sort of statement.

My answer:

If x initially had a non-zero (i.e. true) value, then the expression evaluates to 2 or 2.5 depending on if x is an integral or floating point, respectively. Also x has been assigned that same value.

Otherwise, x == 0 and the expression simply evaluates to ;.

Edits: Fixed spoiler formatting; Corrected switched cases.

4

u/Skitz-Scarekrow Jan 06 '19

C is my main focus and I was looking at that shit like "is this a trick? Isn't it 2.5 float?" Thanks for reassuring me that I can math

2

u/Zetice Jan 06 '19

Actually your answer should be the opposite. If x is 0 originally, then the expression evaluates to false. And gets set to ;

1

u/OmarRIP Jan 06 '19

Thank you. Don’t know how I managed to switch those.

0

u/[deleted] Jan 06 '19

[deleted]

→ More replies (5)

0

u/dontFart_InSpaceSuit Jan 06 '19

Not enough info given. Need current value of x.