Hacker News new | ask | show | jobs
by pavlov 260 days ago
Further proof that JavaScript accidentally became the new C++.

“Aren’t you surprised that this syntax works?” is not praise for a language design.

6 comments

> “Aren’t you surprised that this syntax works?” is not praise for a language design.

It's a clever hack. This is Hacker News. Let's try to appreciate clever hacks while here.

It is not a surprise that overriding the implementation of an operator’s type coercion works and overrides the behavior of the operator’s type coercion.
Do you really think that most JavaScript users are aware that “overriding the implementation of an operator’s type coercion” is a language feature?

Sure, you can claim that everyone should know this obscure feature when they don’t. But that’s how this language enters C++ territory.

I actually don't think you are wrong, but I'm not backing that up with any actual data.

I happened to know it because of how the hyperHTML micro-library works; the author went into great detail about it and a ton of other topics. But my gut would say that the average js dev doesn't know about it.

But then... it's useful for creating component frameworks which... most js devs use. Which doesn't mean they know how they work under the hood. But... a lot of devs I've met specifically choose a framework because of how it works under the hood.

... so... I really have no idea how many people know this. I'm still betting it's less than average.

Well, Proxy objects do allow you to override the behavior of any property, including Symbol properties. Symbol.iterator is pretty widely used to create custom iterable objects, so I would expect curious devs to have taken a look at what else can be done through the use of Symbol properties.
Is there a language that can’t be contorted in surprising ways that I’m unaware of?
LISP — the contortions are expected, not surprises
Non-Lisp: gratuitous contortions are expected.
I think that depends on the person, the language, and how familiar they are with the language. Someone's "what the fuck" is another's "obviously it can do that".
Swift, IMHO. Grew up on ObjC and the absolutely crazy things you could pull off dynamically at runtime. You can definitely feel they did not want that in Swift. There's operator overriding but idk if I'd count that as contorting in surprising ways shrugs
A language where there's only one way to do things, maybe a very early version of CSS. The things is all languages end up bloated with new features.
Probably not, but there are definitely languages that don't do automatic type coercion - so at least one fewer contortion available.
Nothing new. This was 15 years ago:

https://github.com/tbtlr/def.js

No way dude, this does a disservice to the insanity that is C++'s syntax. Wake me up when you have 6 different initialization syntaxes or fun things like 4[array]
Agreed!!!!

Serious q: but how does this sentiment change with LLMs? They can pickup new syntax pretty fast, then use fewer tokens...

It sounds like using less tokens (or, less output due to a more compact syntax) is like a micro-optimization; code should be written for readability, not for compactness. That said, there are some really compact programming languages out there if this is what you need to optimize for.
I imagine the error messages must be terrible to read, since this hack is based on reusing syntax that was meant for something entirely different.
IMO it's more likely to get confused because there are less unique tokens to differentiate between syntax (e.x. pipe when we want bitwise-or or vice-versa)
I’ve heard it said before on HN that this is not true in general because more tokens in familiar patterns helps the model understand what it’s doing (vs. very terse and novel syntax).

Otherwise LLMs would excel at writing APL and similar languages, but seems like that’s not the case.

probably because there arent enough apl examples to imbue the rare weird apl tokens with sufficient semantic meaning to be useful.