bonjour,
je voudrais utiliser un type structurel pour utiliser indifféremment un Int par exemple et une classe que j'ai créé:
MonInt est bien reconnue comme un type AdditionneAvecInt, mais pas Int (la ligne commentée ne compile pas), alors qu'elle a bien une méthode +(Int): Int : http://www.scala-lang.org/api/curren...html#scala.IntCode:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 object Main { type AdditionneAvecInt = { def +(v: Int): Int } class MonInt(val i: Int) { def +(v: Int): Int = i + v } def additionner(a: AdditionneAvecInt, b: Int) = a + b def main(args: Array[String]) = { println(additionner(new MonInt(1), 1)) //println(additionner(1, 1)) } }
le compilateur retourne ça:
c'est possible d'utiliser un Int?Citation:
Test.scala:16: error: type mismatch;
found : Int(1)
required: Main.AdditionneAvecInt
println(additionner(1, 1))
^
one error found
merci