user3564573 user3564573 - 1 year ago 76
Scala Question

How to find supertype in scala

If A<:B, I understand that means A is a subtype and B a supertype, I thought I use A in place of B anywhere B is needed because it has inherited all of its properties from B. Now here is my problem

type One
type Two
type Three
type Four
type Five
type Six
type Seven
type Eight

type Fun1 = { val a: One } => { val b: Two }
type Fun2 = { val b: Two } => { val a: One }

type SuperType = {

type TypeOne = {
def apply: { val func: Fun1 ; val c: Three } => { val b: Two ; val d: Four }
val g: Seven

type TypeTwo = {
def apply: { val func: Fun2 ; val e: Five } => { val b: Two ; val f: Six }
val h: Eight

How can I make SuperType which is the supertype of TypeOne and TypeTwo.
I could only come up with the keyword 'Any' nothing else works. I also tried

def apply: {val func: Fun1}=>{val b: Two}

because I don't see and relationship between all the other values

Answer Source

The only possible relationship I see between Fun1 and Fun2 is that they're both functions, so I guess SuperType is a function with generic arguments. I tried to stick as much as possible to the style you exposed in your snipped (although Scala offers much more idiomatic ways to define type hierarchies).

type SuperType = {
  type A
  type B
  def apply: { val a: A } => { val b: B }

type Fun1 <: SuperType {
  type A = One
  type B = Two
type Fun2 <: SuperType {
  type A = Two
  type B = One
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download