Reputation: 198228
I defined a trait RequireLogin
with a constant:
trait RequireLogin {
val message = "you should login"
if(sessionUser.isEmpty) {
Global.error(message)
throw new RedirectException("/login", message);
}
}
Now in my test, I created an object with this trait, and test if the Global.error()
is equal to RequireLogin.message
.
try {
new Object with RequireLogin
} catch {
case _: RedirectException =>
}
Global.error should be === RequireLogin.message // !!! can't be compiled
But it can't be compiled.
Is there any way to get the constant from a trait? Or how to refactor my code to make it better?
Upvotes: 3
Views: 3241
Reputation: 25781
What comes to mind is to define a companion object holding the constant:
object RequireLogin {
val message = "you should login"
}
Not sure if this is what you're looking for, but I don't think you'll be able to get the value out of the trait itself without using reflection.
Upvotes: 6