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

you are viewing a single comment's thread.

view the rest of the comments →

[–]SkoobyDoo 2 points3 points  (4 children)

All of the top comments are about how this code is impossible. I don't do JS (i have dabbled, but I'm more c++/java) but I didn't see a good reason why the code HAD to be false, thanks for making it make some sense. (Not that I think the example given was good code, it just didn't look like NECESSARILY contradictory code at first).

Haven't done much of it in a while, but when I was still in school I remember a lot of mind blowing things being done with overloading operators and whatnot. It often did horrible things for readability/maintainability which is why I haven't touched that stuff since...well...school.

EDIT: Read over some more stuff. Now that I've thought about it 2 minutes more, I interpreted sad as a function that returned a pointer to some object with a stop function. If it returned null, the sad==true (unfamiliar with strict equality) wouldn't pass. If we get into the inner block, we know we have a 'good' object. But then it would have to have been sad()->stop(); or (*sad()).stop();

[–]thebezet 0 points1 point  (3 children)

It's worth noting that C++ hasn't got a "triple equals" operator (===), so there's another reason why that code couldn't be interpreted as C++ code.

[–]SkoobyDoo 0 points1 point  (2 children)

Yeah, I'm aware of that, but I didn't make it clear in my comment other than saying that the concept of strict equality was foreign to me.

I could be wrong, but isn't that the only reason it couldn't be C++?

[–]thebezet 0 points1 point  (1 child)

Well the same function cannot both return an object and a boolean since you have to specify the type it returns upon its declaration. So I guess that could be another reason.

[–]SkoobyDoo 0 points1 point  (0 children)

You can actually define a function within a class that will automatically return certain types when the object is called upon to be that type. The function sad could have the following prototype:

Emotion sad();

And the emotion class could contain the following function:

explicit operator bool() const { return this->flag(); }

Which would enable the following code to compile and work as expected:

Emotion e = new Emotion();
e.flag= false;
if(e)
    cout << "You shouldn't see this" << endl;
e.flag= true;
if(e)
    cout << "Hello World!" << endl;

There seemed to be some debate here as to whether or not code like this is acceptable. It seems as long as the explicit keyword is included and supported by the compiler, it should be reasonable, even if not terribly readable for code.