You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I ran across this problem when the compiler failed to process a @tailrec annotation on a method inside an anonymous Runnable class. I hadn't imported @tailrec and I got no compile-time warning about this at all - just a StackOverflowException at runtime.
The Runnable was created like so:
new Thread(new Runnable { ... @tailrec ... }).start()
However I eventually realised the problem and found that I get a proper compile error when I rearranged the code like so.
val t = new Thread(new Runnable { ... @tailrec ... })
t.start()
According to my investigation calling new Thread(...).start() means that annotations aren't processed. Note that the parens on the start() seem to be quite important for some reason.
Simplified test case below.
// CORRECT - complains about annotation
// "not found: type nosuchannotation"
// donothing has parens, returns unit, but not called
object Example1 {
class Wrapper(x: AnyRef) {
def donothing {}
}
new Wrapper(new AnyRef {
@nosuchannotation def method() {}
})
}
// CORRECT - complains about annotation
// "not found: type nosuchannotation"
// donothing doesn't have parens, returns unit, called without parens
object Example2 {
class Wrapper(x: AnyRef) {
def donothing {}
}
new Wrapper(new AnyRef {
@nosuchannotation def method() {}
}).donothing
}
// CORRECT - complains about annotation
// "not found: type nosuchannotation"
// donothing has parens, returns unit, called without parens
object Example3 {
class Wrapper(x: AnyRef) {
def donothing() {}
}
new Wrapper(new AnyRef {
@nosuchannotation def method() {}
}).donothing
}
// INCORRECT - compiles without complaint
// donothing has parens, returns unit, called with parens
object Example4 {
class Wrapper(x: AnyRef) {
def donothing() {}
}
new Wrapper(new AnyRef {
@nosuchannotation def method() {}
}).donothing()
}
// INCORRECT - compiles without complaint
// donothing has parens, returns int, called with parens
object Example5 {
class Wrapper(x: AnyRef) {
def donothing(): Int = 1
}
new Wrapper(new AnyRef {
@nosuchannotation def method() {}
}).donothing()
}
The text was updated successfully, but these errors were encountered:
I ran across this problem when the compiler failed to process a @tailrec annotation on a method inside an anonymous Runnable class. I hadn't imported @tailrec and I got no compile-time warning about this at all - just a StackOverflowException at runtime.
The Runnable was created like so:
However I eventually realised the problem and found that I get a proper compile error when I rearranged the code like so.
According to my investigation
calling new Thread(...).start()
means that annotations aren't processed. Note that the parens on thestart()
seem to be quite important for some reason.Simplified test case below.
The text was updated successfully, but these errors were encountered: