News for hackers. Stuff that matters.
Re:So you want to write a type checker (2014) by Dylan16807 on Monday October 30, 2017 @10:39AM (#15585258)
Covariant: A variable needs to have an Animal. You can put a Cat in the variable.

Contravariant: A variable needs to have a function that accepts Animals. You can't put a function that accepts only Cats, or it will crash on other kinds of animal. But you can put a function that accepts all LivingThings.

So when something is covariant you can use a more specific type, and when it's contravariant you can use a more generic type.

When it comes to function parameters, typescript lets you use either. You can replace any type with any related type, even if you're going in the wrong direction. They did this to make certain use cases simpler, at the cost of weaker typing.

Re:So you want to write a type checker (2014) by tytytytytytytyt on Monday October 30, 2017 @11:39AM (#15585813)
Which use cases are those?? Are any of them actually real life, practical use cases?
Re:So you want to write a type checker (2014) by tangent128 on Monday October 30, 2017 @12:50PM (#15586576)
If a function takes an array of Animal as an argument, the "intuitive" assumption is that you can pass in a Cat[] to it since it's probably processing the array items and a Cat is an Animal.

In usual Javascript patterns, this intuition works, but it's not sound, since arrays are mutable; the function could write to the array, appending a Dog or a Potoo or a Jellyfish- valid for an Animal[] argument, but not sound for a Cat[].

In practice, in most JS code I've seen, arrays are constructed once and then are passed around as immutable collections, so Typescript's unsoundness saves a lot more casting than it introduces errors.

Re:So you want to write a type checker (2014) by hinkley on Monday October 30, 2017 @06:12PM (#15589415)
The real problem here is that mutable data structures aren’t entirely type sound. The solution here is to split collections into read only and mutable types, but people balk at this.

At one point long ago I did some experiments with this, and found that the most of the unique method signatures necessary to represent the data structures we are familiar with are concentrated in the mutators.

That is, the interface differences between read only data structures are pretty small. So you don’t double the surface area by splitting read from write. It’s more like 25%, and this might be partially offset by simplifications in code that has to scan multiple types of collections.

Re:So you want to write a type checker (2014) by jonsterling on Friday November 03, 2017 @11:05PM (#15623583)
There's nothing wrong with mutable data structures from a type soundness perspective. We know how to do it properly. But you need to get the type system right in order to include mutable data structures; often people get this wrong, and it leads to all sorts of frustration...
Re:So you want to write a type checker (2014) by tangent128 on Wednesday November 01, 2017 @10:54AM (#15601656)
Do you have any examples lying around of what the interface differences would look like?
Re:So you want to write a type checker (2014) by Dylan16807 on Monday October 30, 2017 @11:54AM (#15585966)
https://github.com/Microsoft/TypeScript-Handbook/blob/master...

The example seems like a valid case. The type of event is expressed in a separate parameter, so being strict here requires pointless casting and doesn't actually make things safer.

Whether you think this type weakness is worth the downsides is up to you.

Re:So you want to write a type checker (2014) by Filligree on Monday October 30, 2017 @11:26AM (#15585661)
That helps, thanks.

Can you explain the naming? What sort of variance is the second case contra?

Re:So you want to write a type checker (2014) by Dylan16807 on Monday October 30, 2017 @11:51AM (#15585926)
The first one is the more natural one, and so the opposite gets to be "contra", I think.