Skip to content

Instantly share code, notes, and snippets.

@polymorphic
Created May 4, 2015 16:44
Show Gist options
  • Save polymorphic/1a9df44dd11949055fd7 to your computer and use it in GitHub Desktop.
Save polymorphic/1a9df44dd11949055fd7 to your computer and use it in GitHub Desktop.
Snippet illustrating how the object-protected modifier could cause the Scala compiler to generate code that throws ClassFormatError.
/*
Code for illustrative purposes only, to demonstrate a particular behavior.
I already know that it doesn't run (throws ClassFormatError), and I wouldn't
write it this way to begin with :)
*/
package eg
class Base() {
def foo = "bar"
}
trait Behavior {
val config = "base"
}
class Derived extends Base with Behavior {
/*
With the object-protected modifier the compiler generates code that causes a ClassFormatError to be thrown at
run time.
*/
private[this] val config = "derived"
/*
Changing the modifier to private yields a compile error.
*/
// private val config = "derived"
}
object Main extends App {
val instance = new Derived
instance.foo
println("done")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment