JavaScript
-
Look! I bought this for free on capybaras website, there's a glitch!
capybara: at least it didn't throw an error.
/ jk
Use typescript if you're paranoid about this
-
I'd rather have my website shit itself than have silent difficult to find errors.
Use typescript
-
This is exactly why it should throw an error, to make it incredibly obvious something isn't working correctly so it can be fixed. Otherwise you have wrong logic leading to hard to notice and hard to debug problems in your code
Use typescript
-
In practice runtime errors are a bitch to find and fix.
Fair enough. This is why people prefer typescript
-
The interpreter knows that this is not something anyone will ever do on purpose, so it should not silently handle it.
You basically defied the whole NaN thing. I may even agree that it should always throw an error instead, but... Found a good explanation by someone:
NaN is the number which results from math operations which make no sense
And the above example fits that.
"hello" - 1
makes no sense at all.Yeah but actually there can be many interpretations of what someone would mean by that. Increase the bytecode of the last symbol, or search for "1" and wipe it from string. The important thing is that it's not obvious what a person who wrote that wants really, without additional input.
Anyway, your original suggestion was about discrepancy between + and - functionality. I only pointed out that it's natural when dealing with various data types.
Maybe it is one of the reasons why some languages use . instead of + for strings.
You basically defied the whole NaN thing. I may even agree that it should always throw an error instead, but⦠Found a good explanation by someone:
NaN is the number which results from math operations which make no sense
Well, technically this is the explanation, it really isn't a good one.
x + 1
with x not being defined also doesn't result in aNaN
but instead it throws a reference error, even though that undefined variable isn't a number either. Andx = 1;x.toUpperCase();
also doesn't silently do anything, even though in this case it could totally return"1"
by coercing x to a string first. Instead it throws a TypeError.It's really only around number handling where JS gets so weird.
Yeah but actually there can be many interpretations of what someone would mean by that. Increase the bytecode of the last symbol, or search for β1β and wipe it from string. The important thing is that itβs not obvious what a person who wrote that wants really, without additional input.
That's exactly the thing. It's not obvious what the person wants and a
NaN
is most likely not what the person wants at either. So what's the point in defaulting to something they certainly didn't want instead of making it obvious that the input made no sense?A similarly ambiguous situation would be something like
x = 2 y
. For someone with a mathematical background this clearly looks likex = 2 * y
with an implicit multiplication sign. But it's not in the JS standard to interpret implicit multiplication signs. If you want multiplication, it needs to explicitly use the sign. And thus JS dutifully throws a Syntax Error instead of just guessing what the programmer maybe wanted.Anyway, your original suggestion was about discrepancy between + and - functionality. I only pointed out that itβs natural when dealing with various data types.
My main point here was that if you have mathematical symbols for string operations, all of the acceptable operations using mathematical symbols need to be string operations. Like e.g.
"ab" * 2 => "abab"
, which many languages provide. That's consistent. I didn't mean that all of these operators need to be implemented, but if they aren't they should throw an error (I stated that in my original comment).What's an issue here is that "1" + 1 does a string concatenation, while "1" - 1 converts to int and does a math operation. That's inconsistent. Because even you want to use that feature, you will stumble over
+
not performing a math operation like-
.So it should either be that +/- always to math operations and you have a separate operator (e.g.
.
or..
) for concatenation, or if you overload+
with string operations, all of the operators that don't throw an exception need to be strictly string-operations-only. -
You are entitled to your opinion. implicit conversion to string is not a feature in most languages for good reasons.
Sure. And you're entitled to yours. But words have meaning and this isn't MY OPINION, it's objective reality. It follows strict rules for predictable output, it is not nonsensical.
You're entitled to think it's nonsense, and you'd be wrong. You don't have to like implicit type coercion, but it's popular and in many languages for good reason...
Language Implicit Coercion Example JavaScript '5' - 1 β 4
PHP '5' + 1 β 6
Perl '5' + 1 β 6
Bash $(( '5' + 1 )) β 6
Lua "5" + 1 β 6
R "5" + 1 β 6
MATLAB '5' + 1 β 54
(ASCII math)SQL (MySQL) '5' + 1 β 6
Visual Basic '5' + 1 β 6
TypeScript '5' - 1 β 4
Tcl "5" + 1 β 6
Awk '5' + 1 β 6
PowerShell '5' + 1 β 6
ColdFusion '5' + 1 β 6
VBScript '5' + 1 β 6
ActionScript '5' - 1 β 4
Objective-J '5' - 1 β 4
Excel Formula "5" + 1 β 6
PostScript (5) 1 add β 6
I think JavaScript is filthy, I'm at home with C#, but I understand and don't fear ITC.
-
Sure. And you're entitled to yours. But words have meaning and this isn't MY OPINION, it's objective reality. It follows strict rules for predictable output, it is not nonsensical.
You're entitled to think it's nonsense, and you'd be wrong. You don't have to like implicit type coercion, but it's popular and in many languages for good reason...
Language Implicit Coercion Example JavaScript '5' - 1 β 4
PHP '5' + 1 β 6
Perl '5' + 1 β 6
Bash $(( '5' + 1 )) β 6
Lua "5" + 1 β 6
R "5" + 1 β 6
MATLAB '5' + 1 β 54
(ASCII math)SQL (MySQL) '5' + 1 β 6
Visual Basic '5' + 1 β 6
TypeScript '5' - 1 β 4
Tcl "5" + 1 β 6
Awk '5' + 1 β 6
PowerShell '5' + 1 β 6
ColdFusion '5' + 1 β 6
VBScript '5' + 1 β 6
ActionScript '5' - 1 β 4
Objective-J '5' - 1 β 4
Excel Formula "5" + 1 β 6
PostScript (5) 1 add β 6
I think JavaScript is filthy, I'm at home with C#, but I understand and don't fear ITC.
C# is filthy. But it explains where you got your warped idea of righteousness.
-
Sure. And you're entitled to yours. But words have meaning and this isn't MY OPINION, it's objective reality. It follows strict rules for predictable output, it is not nonsensical.
You're entitled to think it's nonsense, and you'd be wrong. You don't have to like implicit type coercion, but it's popular and in many languages for good reason...
Language Implicit Coercion Example JavaScript '5' - 1 β 4
PHP '5' + 1 β 6
Perl '5' + 1 β 6
Bash $(( '5' + 1 )) β 6
Lua "5" + 1 β 6
R "5" + 1 β 6
MATLAB '5' + 1 β 54
(ASCII math)SQL (MySQL) '5' + 1 β 6
Visual Basic '5' + 1 β 6
TypeScript '5' - 1 β 4
Tcl "5" + 1 β 6
Awk '5' + 1 β 6
PowerShell '5' + 1 β 6
ColdFusion '5' + 1 β 6
VBScript '5' + 1 β 6
ActionScript '5' - 1 β 4
Objective-J '5' - 1 β 4
Excel Formula "5" + 1 β 6
PostScript (5) 1 add β 6
I think JavaScript is filthy, I'm at home with C#, but I understand and don't fear ITC.
Also, you contradicted yourself just then and there. Not a single of your examples does string concatenation for these types. It's only JS
-
Use typescript
wrote on last edited by [email protected]No. I don't want to transpile. I don't want a bundle. I want a simple site that works in the browser. I want to serve it as a static site. I don't want a build step. I don't want node_modules. I want to code using the language targeted for the platform without any other nonsense.
Javascript is cancer. Fucking left pad?! How the fuck did we let that happen? What is this insane fucking compulsion to have libraries for two lines of code? To need configuration after configuration just to run fucking hello world with types and linting?
No, fuck Typescript. Microsoft owns enough. They own where you store your code. They own your IDE. They might own your operating system. Too much in one place. They don't need to own the language I use, too.
"Let's use a proprietary improvement to fix the standard that should have not sucked in the first place" is why we can't have nice things.
No.
-
This post did not contain any content.
Imagine doing math with strings and then blaming the language not yourself
-
Imagine doing math with strings and then blaming the language not yourself
The risk is when it happens unintentionally. The language is bad for hiding such errors by being overly 'helpful' in assuming intent.
-
The risk is when it happens unintentionally. The language is bad for hiding such errors by being overly 'helpful' in assuming intent.
Sure, but at this point it's your own fault if you don't use Typescript to keep these issues from happening.
-
Sure, but at this point it's your own fault if you don't use Typescript to keep these issues from happening.
So, just donβt use JavaScript?
-
It makes perfect sense if the Lang objective is to fail as little as possible. It picks the left side object, checks if the operand is a valid operand of the type. If it is, it casts the right variable into that type and perform the operand. If it isn't, it reverses operand positions and tries again.
The issue here is more the fact that + is used both as addition and as concatenation with different data types. Well, not an issue, just some people will complain.
Computing a nonsensical result is itself a failure. Continuing to run while avoiding giving an error in that case accomplishes nothing but to make the program harder to debug.
-
So, just donβt use JavaScript?
I wouldn't use raw JS for anything new, yes. Typescript however is an excellent language.
-
I don't even know Haskell but it seems like (" ( , ) ") would be an instance of boob.
(.)
is a valid expression in Haskell. Normally it is the prefix form of the infix operator.
that does function
composition.(.) (2*) (1+) 3
=((2*) . (1+)) 3
=2 * (1 + 3)
=8
.But, the most common use of the word "boob" in my experience in Haskell is the "boobs operator":
(.)(.)
. It's usage in Haskell is limited (tho valid), but it's appearance in racy ASCII art predates even the first versions on Haskell. -
That is just the tip of the iceberg:
Haha thatβs a great site. But I think the C example is actually reasonable behaviour.
-
Instead of trying to make it work, javascript could just say "error." Being untyped doesn't mean you can't have error messages.
I think it's less about type system, and more about lack of a separate compilation step.
With a compilation step, you can have error messages that developers see, but users don't. (Hopefully, these errors enable the developers to reduce the errors that users see, and just generally improve the UX, but that's NOT guaranteed.)
Without a compilation step, you have to assign some semantics to whatever random source string your interpreter gets. And, while you can certainly make that an error, that would rarely be helpful for the user. JS instead made the choice to, as much as possible, avoid error semantics in favor of silent coercions, conversions, and conflations in order to make every attempt to not "error-out" on the user.
It would be a very painful decade indeed to now change the semantics for some JS source text.
Purescript is a great option. Typescript is okay. You could also introduce a JS-to-JS "compilation" step that DID reject (or at least warn the developer) for source text that "should" be given an error semantic, but I don't know an "off-the-shelf" approach for that -- other than JSLint.
-
Also, you contradicted yourself just then and there. Not a single of your examples does string concatenation for these types. It's only JS
- In https://lemm.ee/comment/20947041 they claimed "implicit type coercion" and showed many examples; they did NOT claim "string concatenation".
- However, that was in reply to https://lemmy.world/comment/17473361 which was talking about "implicit conversion to string" which is a specific type of "implicit type coercion"; NONE of the examples given involved a conversion to string.
- But also, that was in reply to https://lemm.ee/comment/20939144 which only mentions "implicit type coercion" in general.
So, I think probably everyone in the thread is "correct", but you are actually talking past one another.
I think the JS behavior is a bad design choice, but it is well documented and consistent across implementations.
-
Sure, but at this point it's your own fault if you don't use Typescript to keep these issues from happening.
"Use a different language" is a common defense of javascript, but kind of a weird one.