简体   繁体   中英

Is this an example of a functional subtype relationship?

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?

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 IntSet s 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM