Live data from Hacker News

Fear, trust and JavaScript: When types and functional programming fail

reaktor.com

1–10 of 210 posts

Re: Fear, trust and JavaScript: When types and functional programming fail

#3
> In a dynamic language like JavaScript, it can be hard to know what the shape of your data is.

Being dynamic doesn't preclude variables from being type checked by the runtime. Reading this just sounds like "javascript has no built in support for type checking, so lets pretend it's not possible"

All the time people spent making weird unreadable 'fat functions', but you still can't define the types of arguments you want to receive?

Re: Fear, trust and JavaScript: When types and functional programming fail

#4
post #3

> In a dynamic language like JavaScript, it can be hard to know what the shape of your data is. Being dynamic doesn't preclude variables from being type checked by the runtime. Reading this just sounds like "javascript has no built in support for type checking, so lets pretend it's not possible" All the time people spent making weird unreadable 'fat functions', but you still can't define the types of arguments you wa…

I guess he means ”knows [at compile time]”

Re: Fear, trust and JavaScript: When types and functional programming fail

#5
One of the strengths of JS is how the runtime environment can be dynamically interrogated and polyfilled, and how it can often gracefully degrade if there's a mismatch between static assumptions and runtime behavior.

Is there a vision for how PureScript/ReasonML/Elm etc. can accomplish the same thing?

The brute force approach is to place a boundary between everything external and handle it via an FFI. This adds friction: apps built under this approach are less capable and evolve less gracefully. I hope there's a better solution.

Re: Fear, trust and JavaScript: When types and functional programming fail

#6
Broken promises in Javascript? ... unsurprising ... Here is an imaginary article that I would read: "New web platform runs entirely in webassembly generated by well-typed languages". Subtitle: "When hand-written Javascript is detected, continuous integration activates subdermal shock-collar that each of the developers is contractually obliged to have".

Re: Fear, trust and JavaScript: When types and functional programming fail

#7
post #4
post #3

> In a dynamic language like JavaScript, it can be hard to know what the shape of your data is. Being dynamic doesn't preclude variables from being type checked by the runtime. Reading this just sounds like "javascript has no built in support for type checking, so lets pretend it's not possible" All the time people spent making weird unreadable 'fat functions', but you still can't define the types of arguments you wa…

I guess he means ”knows [at compile time]”

if someone invented a typed superset of JS life could be so much easier Oh wait ...

Re: Fear, trust and JavaScript: When types and functional programming fail

#8

One of the strengths of JS is how the runtime environment can be dynamically interrogated and polyfilled, and how it can often gracefully degrade if there's a mismatch between static assumptions and runtime behavior. Is there a vision for how PureScript/ReasonML/Elm etc. can accomplish the same thing? The brute force approach is to place a boundary between everything external and handle it via an FFI. This adds frict…

Look like this is one of the idea? Build on Crystal programming language with less to code and reduce clutter.

https://www.mint-lang.com/

Mint to Javascript is what any languages to web assembly.

I’m not sure who is comfortable to evaluate Real World code example: https://github.com/mint-lang/mint-realworld

Along with some Crystal shards (LLVM, assembly, C librares) to validate your data or other structure formats is a breeze to work in.

Re: Fear, trust and JavaScript: When types and functional programming fail

#9
The article builds a strawman.

It's not the types that fail, but the type system and its misuse.

> This is like pretending really hard

No, that's not pretending — static typing is literally about theorem proving, type theory being equivalent with math logic. This is the famous Curry-Howard isomorphism, types corresponding to propositions.

Of course, you can have situations in which your type system cannot describe the propositions that you need, or situations in which holes are left for pragmatism. Such holes include for example the ability to use Object and then downcast in Java and have the implicit contract that the people using them know what they are doing, being at the same time a symptom of the type system not being expressive enough.

In TypeScript in particular you can just use the "any" type, which is there for pragmatism and because JavaScript developers would have had a hard time adopting it otherwise. It doesn't help that TypeScript's generics are unsound either.

Of course, you can point at "the outside world" and say that you can't trust it to deliver the types that you expect. But with a good type system, you only need to do that runtime validation only once, at the edge where you receive the data and afterwards the statically typed compiler can take over that responsibility. And indeed, in my experience libraries for JSON parsing in Scala (e.g. Circe) or Haskell (e.g. Aeson) are very well behaved and due to automatic deriving, they make that validation painless in 90% of cases, because the runtime validation is derived from the static types automatically, no work required, something I haven't seen in TypeScript.

The whole notion of "optional types" is my opinion flawed.

You either work with static typing, or with dynamic typing and choosing a side will change the way you work, it will change your mentality, your approach to problem solving.

Working with optional types, which is what people tend to do in languages like TypeScript, brings you the very worst of both worlds.

Optional static typing doesn't work. A contracts system, like Clojure's Spec, works much better for dynamic typing and yes, there are fundamental differences, basically the difference between "for all" and "exists", or between compile time and runtime.

---

> Convention: Pretend immutability

N.B. in an actual FP language, immutability is not a convention, but something being enforced by how data structures are built (e.g. persistent data structures), via the type system or the runtime system — even in Java, you'll have a really hard time to change the definition of a class or of a "final", or to mutate a persistent collection (see vavr.io).

---

I like TypeScript overall, I think it's an improvement over JavaScript and it certainly has pretty cool features — but don't use it to judge either static typing or dynamic typing or functional programming for that matter.

If you want to see actual dynamic typing and how it can shine, use ClojureScript. If you want to see actual static typing and how it can kick ass, use PureScript.

And both are good for giving you a real taste of functional programming. JavaScript and JavaScript++ languages, are really poor at FP, mostly due to the ecosystem's culture, and the very few FP libraries that exist for JavaScript are very unpopular, with only a few gems here and there, like Rx.JS. So if you're doing JavaScript or TypeScript, chances are your codebase doesn't do much FP.

Re: Fear, trust and JavaScript: When types and functional programming fail

#10
But in JavaScript, the fear is always with you.

That's why we have both tests and code reviews.

Aside from that I'm surprised the author didn't mention checking constructors. It's arguably the most dependable way of checking if something is of a certain type.

But I agree that it's a shame that TS/Flow lose their ability to check types whenever partial application or currying is involved - it's not like it's impossible in principle.

Post reply on HN