This page is no longer maintained — Please continue to the home page at www.scala-lang.org

Adding Numbers together

No replies
Andres Taylor 2
Joined: 2011-06-18,
User offline. Last seen 42 years 45 weeks ago.
Hello,
I have a List[Number]. It contains values of different subclasses of Number - Int, Double, and so on. I want to sum all the values together. If the list only contains Int, I'd like the result to be an Int. If there are mixes, I'd like to follow the normal Scala rules: Int + Double = Double ( and so on ).
list.reduceLeft(_ + _) fails. Number has no + method defined that takes another Number.
How can I do this?
Andrés

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland