Is this an example of a functional subtype relationship?

57 views Asked by At

From this video by Functional Programming Principles in Scala "4.5 Variance" you can see a slide that says,

Translated:

Say you have two function types:

type A = IntSet => NonEmpty
type B = NonEmtpy => IntSet

According to the Liskov Substitution Principle, which of the following should be true?

  • A <: B (checked)
  • B <: A
  • A and B are unrelated.

The video claims type A is a subtype of type B; but, it does so saying,

Type A satisfies the same contract as Type B if you give it a NonEmpty set it will give you back an IntSet, but it will actually satisfy more than B.

But that's not true, B includes the NonEmpty set which includes more than an IntSet. Is this video just straight up incorrect and confused?

1

There are 1 answers

1
John Freeman On BEST ANSWER

I think it's implied (perhaps mentioned in a previous lesson?) that NonEmpty is a subtype of IntSet. Based on the way Odersky was speaking, it sounds like NonEmpty is the subset of IntSets that are not empty. So A accepts more argument values than B does, and returns a narrower subset of values than B does, a subset with additional properties.