Live data from Hacker News

TypeScript 2.4

blogs.msdn.microsoft.com

51–52 of 52 posts

Re: TypeScript 2.4

#51

Earlier quoted context omitted.

I would love for an ELI5 explanation of covariance/contravariance, but I've never seen one. I get it now, but only after reading and rereading explanations very carefully.

So, then how would you explain it to a five year old?

Explaining it to a five-year-old adds overhead that isn't really useful when you're in fact explaining to someone who knows about programming but has simply never heard about covariance and contravariance.

A function `X -> string` can be turned into a function `X -> object`: call it, take its string result and cast it to object. This is called covariant (latin co- for "together") because casting `string` to `object` also lets you cast `X -> string` to `X -> object`.

A function `object -> X` can turned into a function `string -> X`: take the string argument, cast it to object, and pass it to the function. This is called contravariant (latin contra- for "against") because casting `string` to `object` lets you cast `object -> X` to `string -> X`.

Re: TypeScript 2.4

#52

Earlier quoted context omitted.

So, then how would you explain it to a five year old?

Explaining it to a five-year-old adds overhead that isn't really useful when you're in fact explaining to someone who knows about programming but has simply never heard about covariance and contravariance. A function `X -> string` can be turned into a function `X -> object`: call it, take its string result and cast it to object. This is called covariant (latin co- for "together") because casting `string` to `object`…

Thanks, obviously your example is more concrete. GP's comment was good for introducing the idea and your comment is great for showing how the idea is useful.
Post reply on HN