News for hackers. Stuff that matters.
Re:Nil in Go is typed in theory and sort of untyped in practice by mirekrusin on Wednesday March 31, 2021 @03:51PM (#26652202)
Can you please give more concrete example that is valid js but is absurd logically?
Re:Nil in Go is typed in theory and sort of untyped in practice by curryst on Thursday April 01, 2021 @01:58PM (#26663929)
This talk lists a bunch of them https://www.destroyallsoftware.com/talks/wat

The first one he talks about is that [] + [] gives you "" (an empty string). I don't know what universe that makes sense in. [] + {} gives you {} back. Meanwhile {} + [] gives you 0. Yes, the order in which you add types that can't logically be added ends up giving you different incoherent answers. {} + {} gives you NaN.

One of the other sibling comments said that it was mostly implicit conversions, which I think is the underlying cause of most of these.

That's not to totally hate on Javascript, but the language could probably use fewer implicit conversions and more type errors. I struggle to think of a situation in which those outcomes are desirable, and it can cause extremely hard to follow stack traces. A JSON API returns a list of objects instead of a list of strings and now your types are screwed up in hard-to-follow ways.

Re:Nil in Go is typed in theory and sort of untyped in practice by esrauch on Friday April 02, 2021 @08:45PM (#26677433)
So FYI most of those are misleading. When he's typing into a terminal and it does {}+{} it's interpreting it as and empty code block, and then an unary plus on an empty object, similar to:

{ }; +{}

If you put parentheses around it, or assign it to a value and then print that value then {}+{} isn't empty string.

Re:Nil in Go is typed in theory and sort of untyped in practice by esrauch on Wednesday March 31, 2021 @11:59PM (#26656552)
I don't actually subscribe to the "JS is a bad language" meme (and maybe the meme has died off a bit, partially thanks to typescript), but implicit coercion is one thing that people cite, with behavior exactly like that nontransitive Golang case.

Also see this minitalk (which actually gets some things wrong because of confusion on how the terminal interpreter behaves but is still amusing): https://www.destroyallsoftware.com/talks/wat

Re:Nil in Go is typed in theory and sort of untyped in practice by camjw on Wednesday March 31, 2021 @04:02PM (#26652354)
I think the classic example is [] == ![] evaluates to true. It’s not absurd logically, just the operators act weirdly/non-standardly.
Re:Nil in Go is typed in theory and sort of untyped in practice by qsort on Wednesday March 31, 2021 @04:31PM (#26652745)
Honestly that's more of a weirness of the '==' operator, which is kind of deprecated in modern JS. On the other hand I fully agree that non-strict definitions of equality are bound to generate weird cases around nullish values.
Re:Nil in Go is typed in theory and sort of untyped in practice by mirekrusin on Wednesday March 31, 2021 @04:28PM (#26652707)
Yeah, that one is funny

  > [] == []
  false
  > [] == ![]
  true
Re:Nil in Go is typed in theory and sort of untyped in practice by mirekrusin on Wednesday March 31, 2021 @04:31PM (#26652746)
However typescript says:

  This condition will always return 'false' since the types 'never[]' and 'boolean' have no overlap.
...for `[] == ![]`. Which is ironic because it does return true, not false. However at least it flags an error!

I wonder how much of weirdness is wiped out by linters/ts/etc.