I started learning Scala yesterday, so I'm pretty new to this. One thing I like to do when learning a new language is to create a micro-TDD library.
This is what I got so far:
def assert(condition: Boolean, message: String) { if(!condition){ throw new AssertionError(message) } } def assertThrows[E](f: => Unit) { try { f } catch { case e: E => { return } case _: Exception => { } } throw new AssertionError("Expected error of type " + classOf[E] ) }
The code for assert works fine, but I have two problems with assertThrows .
- It seems that I can not use
E in the last line. No matter what I do, I get class type expected but E found error . - If I remove E from the last line (for example, replacing it with
throw new AssertionError("error expected") ), I get the following: warning: abstract type E in type pattern is unchecked since it is eliminated by erasure
I think the two problems that I have are related to how Scala (and probably java) deals with abstract types and how they are executed.
How can I fix my assertThrows?
Bonus points: is this the way I set the "block type" ( f: => Unit ) correctly?
source share