Is there some standard way in Scala of specifying a function that does nothing, for example when implementing a trait? This works:
trait Doer {
def doit
}
val nothingDoer = new Doer {
def doit = { }
}
But perhaps there is some more congenial way of formulating nothingDoer?
Edit Some interesting answers have appeared, and I add a little to the question in response. First, it turns out that I could have used a default implementation in Doer. Good tip, but you don’t always want that. Second, apparently a more idiomatic way of writing is:
val nothingDoer = new Doer {
def doit { }
}
Third, although nobody suggested exactly that, I found that this also seems to work:
val nothingDoer = new Doer {
def doit = Unit
}
Is this a good alternative?
(The “:Unit” that a few people suggested does not really add anything, I think.)
Since your return type is Unit, it’s conventional not to use the
=