FAQ

2014-04-28 15:05 GMT+08:00 杨博 <pop.atry@gmail.com>:

class Adder[A <: Plusable] extends Computer[A] {
def compute(x: A, y: A): A = x + y //此行出错

}
type mismatch;
found : A\$A3.this.Plusable
required: A
def compute(x: A, y: A): A = x + y
^

error: type mismatch;
found : Any
required: A
def compute(x:A, y:A): A = x + y
^

--
Nicholas Ren (任晓君)

*Thought*Works | Xi'an | GMT+8

On Monday, April 28, 2014 at 1:58 PM, 杨博 wrote:

def compute[A](x:A, y:A)(implicit numeric: Numeric[A]):A = {
import numeric._
x + y
}
assert(compute(1, 2) == 3)
assert(compute(1.2, 3.4) == 4.6)

trait Plusable {
def +(x: Plusable): Plusable
}

trait Computer[A <: Plusable] {
def compute(x:A, y:A):A
}

class Adder[A <: Plusable] extends Computer[A] {
def compute(x:A, y:A): A = {
x + y // 无法编译，提示类型不正确
}
}

type Plusable = {
def +(x: Any): Any
}

trait Computer[A <: Plusable] {
def compute(x:A, y:A):A
}

class Adder[A <: Plusable] extends Computer[A] {
def compute(x:A, y:A): A = {
x + y // 无法编译，提示类型不正确
}
}

2014-04-24 11:10 GMT+08:00 Xiang Zhang <solom...@gmail.com>:

2014-04-24 9:31 GMT+08:00 Liu Sam <world...@gmail.com>:

object Hello extends App {

println("Hello World!")
}

trait Computer[A] {
def compute(x:A, y:A):A = {
x
}
}

class Adder[A] extends Computer[A] {
def compute(x:Int, y:Int):Int = {
x + y
}
def compute(x:String, y:String):String = {
x + y
}
}

Best regards!

Sam Liu
world...@gmail.com

trait Computer[A] {
def compute(x:A, y:A):A
}

class Adder[A] extends Computer[A] {
def compute(x:Int, y:Int):Int = {
x + y
}
def compute(x:String, y:String):String = {
x + y
}
}

Best regards!

Sam Liu
world...@gmail.com

Best regards!

Sam Liu
world...@gmail.com

trait Computer[A] {
def compute(x:A, y:A):A
}

class Adder[A] extends Computer[A] {
def compute(x:A, y:A): A = {
x + y // 无法编译，提示类型不正确
}
}

--

--

--

--

--

--

--

--

## Related Discussions

 view thread | post posts ‹ prev | 10 of 11 | next ›
Discussion Overview
 group scalacn posted Apr 24, '14 at 12:05a active Apr 28, '14 at 10:02a posts 11 users 6

### 6 users in discussion

Content

People

Support

Translate

site design / logo © 2019 Grokbase