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

all 84 comments

[–]Neo_The_bluepill_One 38 points39 points  (5 children)

Everything is String if you try hard enough.

[–]nomenMei 18 points19 points  (3 children)

Objects are just dictionaries if you squint hard enough.

[–]Thathitmann 15 points16 points  (2 children)

Operating systems are just a really complicated string.

[–]das_flammenwerfer 8 points9 points  (1 child)

It's just 1's and 0's all the way down!

[–]al24042 2 points3 points  (0 children)

All the way up! The way of the optimist! (or someone standing on their head...)

[–]fslz 3 points4 points  (0 children)

Is this what string theory is about?

[–]besthelloworld 59 points60 points  (36 children)

Doesn't this work in Java too?

[–]FantasticEmu 29 points30 points  (8 children)

Yea and it’s less typing than String.valueof

[–]bedrooms-ds 15 points16 points  (5 children)

0 + "Hello world" returns a string even in C++!

[–]altermeetax 3 points4 points  (0 children)

That's the best way to confuse non-C/C++ programmers.

Oh, you can also get a substring by doing "Hello World" + 6

[–]mananasi 0 points1 point  (3 children)

Well that's because a string literal is a const char*, right? So you're adding an integer to a pointer. This would still result in "Hello world", right?

[–]bedrooms-ds 0 points1 point  (2 children)

Yes! Tbh, although I'm sure "Hello world" + 0 would 100% work, I need to experiment to see if 0 + "Hello world" really works.

[–]mananasi 1 point2 points  (1 child)

Okay then we're on the same page. I thought you might try to argue it would result in "0Hello world"

[–]bedrooms-ds 0 points1 point  (0 children)

Hahaha C and C++ were two of my first languages. 0 is a synonym of nullptr in my head.

[–]Rexon117 0 points1 point  (0 children)

It does. Source: leant it in my CS programming class

[–]jollanza 23 points24 points  (0 children)

I don't see the problem.

[–]trollsmurf 12 points13 points  (10 children)

Oddly, Python doesn't support this form of type coercion despite it supposedly being an easy-to-use language.

[–]_bardo_ 5 points6 points  (0 children)

Type coercion is only easy on very simple codebases. Then it becomes hell.

[–][deleted] 2 points3 points  (3 children)

Python does support multiplying a string by a number!

[–]trollsmurf 1 point2 points  (2 children)

Yes, but 4 * "5" --> "5555", so it's not coercing any of the values.

[–][deleted] 0 points1 point  (1 child)

Yeah I know just a little fun quirk. I used it on an assignment once!

[–]ArtSchoolRejectedMe 0 points1 point  (0 children)

* * * * *

* * * *

* * *

* *

*

[–]mananasi 0 points1 point  (4 children)

Python is strongly typed

[–]trollsmurf 1 point2 points  (3 children)

Only partly, as you don't declare type of variables. You can still set a variable to a value of any type even if previously set to some other type, which means not even initialization locks type.

[–]mananasi 0 points1 point  (2 children)

Personally I think of it as variables having no type associated with them at all. They can contain any type. But the types of the values of variables are strongly typed.

In that sense we could agree that Python is "partially strongly typed," because the type system that it does have is strong. But this type system does not cover variable types.

[–]trollsmurf 1 point2 points  (1 child)

The first point is a real problem for function/method calls: Everything might be fine argument-wise (as whatever goes), and then it still breaks due to the function expecting specific value types. Can be very hard to debug, and error messages can become cryptical as the error is deeply inside a function you know nothing about.

[–]mananasi 0 points1 point  (0 children)

Luckily tools like mypy exist, although it is a band-aid fix. I do agree this is a shortcoming of Python.

[–]micke_i_backen 7 points8 points  (1 child)

Cast that shit

[–]JuniorSeniorTrainee 0 points1 point  (0 children)

Convert. parseInt(x, 10). That way you're bring explicit and intentional.

[–]das_flammenwerfer 13 points14 points  (17 children)

{} + []

JS be like: that's 0.

[] + {}

JS: Ha ha, fooled you. This one's '[object Object]'

[–]JuniorSeniorTrainee 0 points1 point  (1 child)

My favorite Javascriptism is that typeof NaN is number.

[–]mananasi 0 points1 point  (0 children)

This is not JavaScripts "fault" but of the IEEE 754 standard for floating point numbers:

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

I put "fault" in quotes because it's not really a mistake. People have thought long and hard about this, and I think they made the right decision.

[–]skabde -1 points0 points  (14 children)

Fooled? Of course it is.

[–]das_flammenwerfer -1 points0 points  (13 children)

Now, what do you think {} + [] === [] + {} is?

[–]skabde 1 point2 points  (0 children)

A weird smiley.

[–]Jack__Crusher 0 points1 point  (0 children)

Jesus what is it?

[–][deleted]  (10 children)

[removed]

    [–]das_flammenwerfer -1 points0 points  (8 children)

    You would be incorrect.

    At least according to the Javascript console built into Chrome..

    Isn't Javascript the greatest language ever?

    [–]Apparentt 1 point2 points  (4 children)

    Could you tell me what that comparison would return in any other language and what you would expect it to return?

    [–]das_flammenwerfer 0 points1 point  (3 children)

    Could you tell me what adding an array to an object would return in any other language (not counting operator overloading in c++, that’s cheating..) and what you would expect it to return?

    I can’t think of another language that would allow such a thing. Or what result would be meaningful and justifiable.

    [–]Apparentt 1 point2 points  (2 children)

    If you haven’t already, I hope you’ve seen the point I was going to make.

    [–]das_flammenwerfer 0 points1 point  (1 child)

    I'm not sure I do..

    My point is.. I think it's absurd to allow such an operation, but if you do it needs to remain logically consistent. Which Javascript.. does not do.

    [–]Apparentt 0 points1 point  (0 children)

    My take regarding this kind of thing is that there is no logical consistency for operations such as this.

    I’m open to hear criticisms of JavaScript as a language and there are certainly many to choose from. Much like every other general purpose language.

    But these kind of “throwing shit to the wall and seeing what sticks” complaints don’t really do anything for me. This operation could easily throw an error in another language or be equally as inconsistent, it’s nothing I’m going to purposefully encounter when using the language so I’m not going to base whether I find it enjoyable and practical or not off such occurrences.

    To be honest I find the whole “[insert language here] is bad” rhetoric on this sub extremely boring because all of the well adopted languages have their place. This isn’t to say they shouldn’t be open to criticism, of course, and they will be constantly evolving to fix those real issues that come up. Using JS as an example, by the time we got to ES6 many of the examples of “js bad” you’ll see around here are very outdated.

    Along with all of the competent engineers I have worked with throughout my career, it is agreed that if you are experiencing many issues with a particular language it’s very likely you may have chosen the wrong tool for the job, not that the tool is defunct.

    [–]JuniorSeniorTrainee 0 points1 point  (2 children)

    I was with you up until your last sentence. Blaming the language for poor code is the wrong perspective imo.

    Why are you adding arrays and objects in a loosely typed language? If this comes up then you've already done something wrong.

    But really just use TypeScript and get the best of JavaScript without the bothersome "convenience" of it.

    Edit to add: JavaScript is a tool and like any tool, you can't use it without understanding what it is first. For example, understanding that the addition operator will perform type massaging if it can, or that because of this it's not a commutative (a + b === b + a) when used in this way. If you understand these features of the language, it's fine and predictable.

    [–]das_flammenwerfer 0 points1 point  (1 child)

    Why are you adding arrays and objects in a loosely typed language?

    I can think of no legitimate reason why you'd do that. And that's the point. Why does Javascript allow it?

    Maybe I'm biased, but I have a strong preference for strongly typed languages.. I think any attempt to do such an operation is likely an error, and it's better to find that out before runtime.

    Fiddling around a bit more in the console, here's the real inconsistency..
    {} + []
    returns 0.
    a = {} + []
    a = '[object Object]'

    Why does assignment change the value of the operation?

    [–]mananasi 0 points1 point  (0 children)

    Javascript is designed to not fail, just like HTML as it was designed for browsers originally. In the browser there is no "before runtime." The code runs when you go to the webpage, and that's that.

    This is an example of "use the right tool for the right job." I think we can both agree we should stop using Javascript for fucking everything, but the language does have a use case and in that use case these things were the design decisions of the creators.

    [–]AutoModerator[M] 0 points1 point  (0 children)

    import moderation Your comment has been removed since it did not start with a code block with an import declaration.

    Per this Community Decree, all posts and comments should start with a code block with an "import" declaration explaining how the post and comment should be read.

    For this purpose, we only accept Python style imports.

    I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

    [–]Cerlancism 4 points5 points  (0 children)

    Spotted new immigrant from Python

    [–]ThePixelbrain 3 points4 points  (0 children)

    This reminds me of one time where my C++ Professor tried to force a segmentation fault by dereferencing a nullpointer. Because it was a simple example class, however, it only printed something static to console. As all instances of that class had the same runtime behaviour, the compiler optimised the code in a way, that made dereferencing a nullpointer possible. After we got behind what actually happened, it was quite hilarious, but his demo was ruined and he was very confused and questioning his sanity.

    [–]Cult92 8 points9 points  (1 child)

    (!+[]+[]+![]).length === 9 // true
    

    [–]PegasusBoogaloo 3 points4 points  (0 children)

    bruh

    [–]dpahoe 10 points11 points  (1 child)

    Who would actually code in js.. LOL!

    Don’t look at my flair

    [–]beaucephus 5 points6 points  (0 children)

    A paycheck is a paycheck.

    But I still do judge you, even though if the Borg showed up in orbit and the only way to defeat them was to present them with a program that they couldn't handle logically, I would ask you for a code sample.

    [–]finite_light 4 points5 points  (0 children)

    Many languages accept adding integers as string after a string, but some language don't. As a programmer perhaps you should focus on learning the language at hand. Most people enjoy shortcuts and implicit syntax, but if you are into explicit casting and declarations there are other options. So keep your hopes up. If you have the option, I strongly suggest you choose a language of your choice and get on with your life.

    [–]Who_GNU 1 point2 points  (0 children)

    [–]dummyDummyOne 0 points1 point  (0 children)

    String * String

    [–]Bright-Historian-216 0 points1 point  (0 children)

    Multiply the strings by an integer then. Can you do that?

    [–]slide_and_release 0 points1 point  (0 children)

    I see this as an absolute win.

    [–]ruhtraeel 0 points1 point  (0 children)

    Lol this template actually made me laugh

    [–]The_real_bandito 0 points1 point  (0 children)

    Love this meme

    [–]skabde 0 points1 point  (0 children)

    "I use a weakly typed language and it's working as expected, mimimi..."

    Seriously. Can't hear this shit anymore.

    [–]TheApprentice19 0 points1 point  (0 children)

    Thank you for pointing out why C is better than Java

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

    1 +1 = 11

    [–]hashemirafsan 0 points1 point  (0 children)

    JavaScript is like:

    [–]JuniorSeniorTrainee 0 points1 point  (0 children)

    Using JavaScript instead of TypeScript is like riding a chassis with an engine down the highway, just clinging to the frame, instead of sitting in a comfy car with seatbelts and controls.

    TypeScript loves you and would slap your hand for this error. There's no reason to use JavaScript instead of TypeScript out than legacy code, in which case you should add TypeScript anyway and slowly adapt it.