-
Notifications
You must be signed in to change notification settings - Fork 21
DelayedInit fails for top derived class has empty constructor #4680
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Imported From: https://issues.scala-lang.org/browse/SI-4680?orig=1 |
@paulp said: |
@odersky said: |
Matt Hicks (darkfrog) said: |
@paulp said: |
Matt Hicks (darkfrog) said: |
@paulp said: https://raw.github.com/scala/scala/c8f4316b3/test/files/run/bug4680.scala Here is what it produced at the time of its writing and presumably at present: https://raw.github.com/scala/scala/c8f4316b3/test/files/run/bug4680.check |
@paulp said: https://groups.google.com/forum/#!topic/scala-debate/5nbzkiv6lFI |
Matt Hicks (darkfrog) said: I'm all for your alternative if we could get it introduced, but as it is DelayedInit exists now but requires bug-fixes...a new feature to the language I find doubtful at least in 2.10. I think it's reasonable for DelayedInit to be fixed before you can consider 2.10 to be out of RC considering this is a glaring bug that needs to be fixed. |
@paulp said: |
@adriaanm said: DelayedInit is unfortunately plagued with issues -- it seems to be common theme with these main-method replacements. In the meantime, yes, DelayedInit bugs should be fixed in 2.10.x |
Matt Hicks (darkfrog) said: |
@paulp said: Nope. I have another unpublished branch which comes at it from another angle; you can define postInit in the companion of any class, and after instantiation it traverses all the base classes calling any companion-defined postInit methods with the newly created instance as the argument. This makes it possible to inherit from a class, write custom postInit code, yet still have the base class's postInit code called, without being required to know about and dispatch to the existing postInits. The major weakness is that it of necessity depends on the participation of the compiler; it would all break down if any such class were instantiated from java. This might not matter to most people, but it would be an issue nonetheless. (You could make the constructors private, but then you lose the part where you can still use inheritance.) |
@adriaanm said: |
Matt Hicks (darkfrog) said: |
@paulp said: |
Matt Hicks (darkfrog) said: |
@adriaanm said: |
Matt Hicks (darkfrog) said: |
@adriaanm said: Fixing DelayedInit would likely require deprecating it and replacing it by a new feature, possibly the one proposed in #4330. Scala is an open source project. We do our best to facilitate and support community contributions, but we can't do everything ourselves as our resources are limited. |
@paulp said: For instance: https://groups.google.com/forum/#!topic/scala-debate/M8s8FmASL8Y Trust me when I say DelayedInit is way, way, way down the list of things which are known to be broken and which will not be fixed. |
Matt Hicks (darkfrog) said: Paul, though I agree with your premise, I don't believe it is a sufficient problem on its own to abandon the language. I refuse to go back to Java and I don't see any other languages offering the capabilities that Scala does without the headaches caused by things like this. |
@adriaanm said: |
Matt Hicks (darkfrog) said: Is there something I can do to help? I would happily contribute code (and perhaps Paul might be persuaded to do so as well) if there were a high likelihood that it would find itself into a next release. I'm also happy to move to Switzerland if it will help. :-p |
@paulp said: |
@paulp said:
Sigh. |
@paulp said: |
@paulp said: "One post by one author." I'm glad to be reminded of this. There's so much which went into why I quit, I forget most of it at any given time. People really have no idea. |
Matt Hicks (darkfrog) said: |
@paulp said: |
Markus Marvell (Marvell) said (edited on Feb 21, 2016 10:00:34 AM UTC): trait Worker {
def use(): Unit
}
trait SafeWorker extends DelayedInit with Worker{
private[this] var ok = true
override final def delayedInit(body: ⇒ Unit): Unit = {
if (ok) try body catch {case e: Throwable ⇒ ok = false}
}
def isOk: Boolean = ok
def use(): Unit = if (isOk) println(s"Working...") else println(s"Oops...")
}
class MediaSafeWorker extends SafeWorker {
throw new Exception
}
class MySafeWorker extends MediaSafeWorker { () }
class UnsafeWorker extends Worker{
throw new Exception
def use(): Unit = println(s"Working...")
} Running this code prints: "Oops..." val safeObj = new MySafeWorker
safeObj.use() But running this code breaks execution with exception. val unsafeObj = new UnsafeWorker
unsafeObj.use() (i) SUGGESTED OPTION DelayedInit trait needs some improvements.
def delayedInit (body: => Unit, bodyClass: Class[_]) . Or even version with argument for explicit mark indicating leaf class body. def delayedInit (body: => Unit, bodyClass: Class[_], isLeafClass: Boolean) Or add def onCreate() when object construction is complete. Current workaround is to use the solution suggested by @erik Westra for detection of leaf class. And slightly raw solution to make asynchronous delayed cancellable invokation of onCreate method in base superclass and in the end of each body of delayedInit method. trait Obj extends DelayedInit {
postOnCreate()
override final def delayedInit(body: ⇒ Unit): Unit = {
cancelPostOnCreate()// pseudocode
body
if ((body _).getClass.getDeclaringClass == getClass) onCreate()
else postOnCreate()
}
def postOnCreate(): Unit = post(id= "onCreate", delay=10 ms, onCreate())// pseudocode
def onCreate(): Unit
} If the body of leaf class is empty onCreate will be called 10 milliseconds after instance is constructed. Not cool but at least. (!) NOTE for Android developers. -keep class scala.DelayedInit |
DelayedInit is deprecated. the discussion here could be revived on https://contributors.scala-lang.org |
Consider the code:
This produces the output:
But when the constructed "C" has a non-empty constructor, such as:
Then the DelayedInit works and generates:
The text was updated successfully, but these errors were encountered: