fork download
  1. trait CanQuantify[Q, T] {
  2. def apply(value: T): Q
  3. }
  4.  
  5. trait Quantity[T] {
  6. type Self <: Quantity[T]
  7.  
  8. def value: T
  9.  
  10. def +(that: Quantity[T])(implicit q: CanQuantify[Self, T], num: Numeric[T]) = {
  11. import num._
  12. q(value + that.value)
  13. }
  14.  
  15. def /[R <: Quantity[T]](that: R)(implicit num: Numeric[T]) = {
  16. import num._
  17. Per[T, Self, R](value * that.value)
  18. }
  19. }
  20.  
  21. case class Per[T, Q <: Quantity[T], R <: Quantity[T]](value: T) extends Quantity[T] {
  22. override type Self = Per[T, Q, R]
  23. }
  24.  
  25. case class ProductCount(value: Int) extends Quantity[Int] {
  26. override type Self = ProductCount
  27. }
  28.  
  29. case class BoxCount(value: Int) extends Quantity[Int] {
  30. override type Self = BoxCount
  31. }
  32.  
  33. package object canQuantifies {
  34. implicit val productCountCanQuantify = new CanQuantify[ProductCount, Int] {
  35. override def apply(value: Int) = ProductCount(value)
  36. }
  37.  
  38. implicit val boxCountCanQuantify = new CanQuantify[BoxCount, Int] {
  39. override def apply(value: Int) = BoxCount(value)
  40. }
  41. }
  42. import canQuantifies._
  43.  
  44. object Main extends App {
  45. val products = ProductCount(10)
  46. val boxes = BoxCount(5)
  47. var n = products / boxes
  48. n = boxes / products
  49. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
/opt/scala/bin/scalac: line 50: /dev/null: Permission denied
Main.scala:48: error: type mismatch;
 found   : Per[Int,Main.boxes.Self,ProductCount]
 required: Per[Int,Main.products.Self,BoxCount]
  n = boxes / products
            ^
one error found
spoj: The program compiled successfully, but Main.class was not found.
      Class Main should contain method: def main(args: Array[String]).
stdout
Standard output is empty