본문 바로가기

프로그래밍/Scala

Scala - Type polymorphism

 

 

 

 

/**
 * Problem 1: Structural subtype (5 points)
 *
 * Find the **least** (i.e. most specific) common supertype of Ty1 and Ty2.
 *
 *   `CommonTy >: Ty1 && CommonTy >: Ty2`
 *
 * We will check your answer by comparing it with our answer as follows:
 *
 * `checkType(Ty1 <: CommonTy && Ty2 <: CommonTy && CommonTy <: Answer)`
 *
 * DO NOT USE "Any" in anywhere in your code
 */
object Problem1 {
  class MyClass[A, B, C, D, E, F]() {
    type Func1 = {val a: B} => {val b: A}
    type Func2 = {val b: D} => {val a: A}
    type Func3 = {val c: C} => {val a: B}
    type Func4 = {val f: E} => {val d: F}

    type Ty1 = {
      def apply: {val func: Func1; val c: C} => {val b: B; val c: C; val f: F}
      def function1: {val func: Func3} => {val a: A; val func: Func2}
      val a: A
      val b: B
      val f: F
    }

    type Ty2 = {
      def apply: {val func: Func2; val e: E} => {val b: B; val e: E }
      def function1: {val func: Func4} => {val c: C; val func: Func1}
      val a: A
      val c: C
      val d: D
    }

    /**
     * WRITE YOUR ANSWER
     */
    type CommonTy = {
      def apply= {def func: ;val c:C; val e:E } => {val b:B}
      def function1 = {val func:} => {val c:C; val a:A; }
      val a: A

    }
  }
}

 

 

 

 

 

 

 

 

 

 

 

 

 

잘 이해가 안가는 예제

def foo(s:{val a:Int; val b:Int}) : {val x:Int; val y:Iny} = {
	object tmp {
    	val x = s.b
        val y = s.a
    }
    tmp
}

val gee: {val a:Int; val b:Int; val c:Int} => {val x:Int} = foo_

 

'프로그래밍 > Scala' 카테고리의 다른 글

Scala Array 문법  (0) 2022.11.24
백준 2738번 행렬의 덧셈  (0) 2022.11.23
Scala Array 문법  (0) 2022.11.22
Scala String 문법  (0) 2022.11.22
1103 Mixin, Stacking with Traits  (0) 2022.11.03