What does “abstract over” mean?

后端 未结 6 2095
醉话见心
醉话见心 2021-01-29 17:58

Often in the Scala literature, I encounter the phrase \"abstract over\", but I don\'t understand the intent. For example, Martin Odersky writes

You can p

6条回答
  •  忘掉有多难
    2021-01-29 18:34

    Here is my narrow show and tell interpretation. It's self-explanatory and runs in the REPL.

    class Parameterized[T] { // type as a parameter
      def call(func: (Int) => Int) = func(1)  // function as a parameter
      def use(l: Long) { println(l) } // value as a parameter
    }
    
    val p = new Parameterized[String] // pass type String as a parameter
    p.call((i:Int) => i + 1) // pass function increment as a parameter
    p.use(1L) // pass value 1L as a parameter
    
    
    abstract class Abstracted { 
      type T // abstract over a type
      def call(i: Int): Int // abstract over a function
      val l: Long // abstract over value
      def use() { println(l) }
    }
    
    class Concrete extends Abstracted { 
      type T = String // specialize type as String
      def call(i:Int): Int = i + 1 // specialize function as increment function
      val l = 1L // specialize value as 1L
    }
    
    val a: Abstracted = new Concrete
    a.call(1)
    a.use()
    

提交回复
热议问题