object-initialization

How to identify / get automated hints with cyclic object initialization causing deadlocks in Scala?

我的梦境 提交于 2020-07-31 04:17:33
问题 The following code runs into future timeouts (in Scala 2.x and Dotty, -Xcheckinit or -Ycheck-init does not help here) because of cyclic object initialization. In complex projects these cycles usually are hidden very well. Is there any possiblity of getting help from the compiler or at least at runtime? How do you prevent this from happening in a multithreaded environment? import scala.concurrent.Future import scala.concurrent._ import scala.concurrent.duration._ import scala.concurrent

Is initializer evaluated after memory allocation in new expression?

╄→尐↘猪︶ㄣ 提交于 2020-01-24 02:58:13
问题 Consider the code auto p = new T( U(std::move(v)) ); The initializer is then U(std::move(v)) . Let's assume that T( U(std::move(v)) ) does not throw. If the initializer is evaluated after the underlying memory allocation, the code is then strong-exception-safe. Otherwise, it is not. Had memory allocation thrown, v would have already been moved. I'm therefore interested in the relative order between memory allocation and initializer evaluation. Is it defined, unspecified, or what? 回答1: Yes,

Assertions in abstract superclass scala creating NPE

走远了吗. 提交于 2019-12-31 03:06:08
问题 The following code, when entered in REPL abstract class A { val aSet: Set[Int]; require(aSet.contains(3)) } class B extends A { val aSet = Set(4,5,6) } new B() gives a null point exception, rather than an invariant failure. What would be the best idiom to solve this problem? Similar questions: Code Contracts: Invariants in abstract class Private constructor in abstract class Scala? and also online commentary: https://gist.github.com/jkpl/4932e8730c1810261381851b13dfd29d 回答1: When you declare

Why is a POD in a struct zero-initialized by an implicit constructor when creating an object in the heap or a temporary object in the stack?

左心房为你撑大大i 提交于 2019-12-17 22:33:04
问题 The standard and the C++ book say that the default constructor for class type members is called by the implicit generated default constructor, but built-in types are not initialized. However, in this test program I get unexpected results when allocating an object in the heap or when using a temporary object: #include<iostream> struct Container { int n; }; int main() { Container c; std::cout << "[STACK] Num: " << c.n << std::endl; Container *pc = new Container(); std::cout << "[HEAP] Num: " <<

Java Generic object initialization

為{幸葍}努か 提交于 2019-12-13 04:48:31
问题 Please look at this snippet first : public MultiThreadManager( Class<T> c) { T[] allJobs = (T[]) Array.newInstance( c , MAX_THREAD_SIZE ) ; for ( int i = 0 ; i < MAX_THREAD_SIZE ; i ++ ) { allJobs[i] = (T) new Object(); service.submit( allJobs[i] ); getWaitingThreads().add( allJobs[i] ); } } Here is the exception : Exception in thread "main" java.lang.ClassCastException: java.lang.Object cannot be cast to slave.JobTemplate What I am trying to do : The Constructor of MultiThreadManager should

Python Module Initialization

橙三吉。 提交于 2019-12-10 17:17:02
问题 Is it bad practice to initialize the objects in the module, in the module code? in Module.py : class _Foo(object): def __init__(self): self.x = 'Foo' Foo = _Foo() Than in user code, you could: >>> from Module import Foo >>> print Foo.x 'Foo' >>> ...without having to initialize the Foo class in the user code. Of course, only useful if you don't need arguments to initialize the object. Is there a reason not to do this? 回答1: Typically, you only want to run the minimum necessary to have your

Scala: Overridden value parent code is run but value is not assigned at parent

妖精的绣舞 提交于 2019-12-01 10:57:02
Running the code below: class Parent { val value = { println("Setting value in parent") "ParentVal" } println(s"Parent value is ${value}") } class Child extends Parent { override val value = { println("Setting value in child") "ChildVal" } println(s"Child value is ${value}") } new Child Produces this output: Setting value in parent Parent value is null Setting value in child Child value is ChildVal So the code associated with the parent value assignment is run, however the value is not really assigned at the parent. Afterwards the child code runs and it assigns the value as expected. Could

Scala: Overridden value parent code is run but value is not assigned at parent

我们两清 提交于 2019-12-01 05:53:42
问题 Running the code below: class Parent { val value = { println("Setting value in parent") "ParentVal" } println(s"Parent value is ${value}") } class Child extends Parent { override val value = { println("Setting value in child") "ChildVal" } println(s"Child value is ${value}") } new Child Produces this output: Setting value in parent Parent value is null Setting value in child Child value is ChildVal So the code associated with the parent value assignment is run, however the value is not really

Why does implement abstract method using val and call from superclass in val expression return NullPointerException

自古美人都是妖i 提交于 2019-11-30 09:43:10
问题 I have an abstract class with an unimplemented method numbers that returns a list of numbers, and this method is used in another val property initialization: abstract class Foo { val calcNumbers = numbers.map(calc) def numbers: List[Double] } The implementing class implements using a val expression: class MainFoo extends Foo { val numbers = List(1,2,3) } This compiles fine, but at run time it throws a NullPointerException and it points to the line of val calcNumbers : [error] (run-main-0)

Why does implement abstract method using val and call from superclass in val expression return NullPointerException

假如想象 提交于 2019-11-29 17:30:27
I have an abstract class with an unimplemented method numbers that returns a list of numbers, and this method is used in another val property initialization: abstract class Foo { val calcNumbers = numbers.map(calc) def numbers: List[Double] } The implementing class implements using a val expression: class MainFoo extends Foo { val numbers = List(1,2,3) } This compiles fine, but at run time it throws a NullPointerException and it points to the line of val calcNumbers : [error] (run-main-0) java.lang.ExceptionInInitializerError [error] java.lang.ExceptionInInitializerError ... [error] Caused by: