static-initialization

How to fill a Javascript object literal with many static key/value pairs efficiently?

我的梦境 提交于 2019-12-03 06:28:49
问题 The typical way of creating a Javascript object is the following: var map = new Object(); map[myKey1] = myObj1; map[myKey2] = myObj2; I need to create such a map where both keys and values are Strings. I have a large but static set of pairs to add to the map. Is there any way to perform something like this in Javascript: var map = { { "aaa", "rrr" }, { "bbb", "ppp" } ... }; or do I have to perform something like this for each entry: map["aaa"]="rrr"; map["bbb"]="ppp"; ... Basically, remaining

__attribute__((constructor)) call order confusion

对着背影说爱祢 提交于 2019-12-03 06:27:31
The answer here demonstrates that __attribute__((constructor)) is not called after static initialization, it is called in the declaration order. Then, what is the purpose of it, if it is not guaranteed to be called when all data is initialized? We could as well have our ((constructor)) code in the Foo constructor. What I'm looking for is a way to have, in a shared library, a code that will be executed after all static data is initialized and static constructors are called. I saw people recommending __attribute__((constructor)) as a replacement for DllMain; as we can see this is wrong, because

How to fill a Javascript object literal with many static key/value pairs efficiently?

≡放荡痞女 提交于 2019-12-02 19:02:52
The typical way of creating a Javascript object is the following: var map = new Object(); map[myKey1] = myObj1; map[myKey2] = myObj2; I need to create such a map where both keys and values are Strings. I have a large but static set of pairs to add to the map. Is there any way to perform something like this in Javascript: var map = { { "aaa", "rrr" }, { "bbb", "ppp" } ... }; or do I have to perform something like this for each entry: map["aaa"]="rrr"; map["bbb"]="ppp"; ... Basically, remaining Javascript code will loop over this map and extract values according to criterias known 'at runtime'.

Is final ill-defined?

浪尽此生 提交于 2019-12-02 14:15:43
First, a puzzle: What does the following code print? public class RecursiveStatic { public static void main(String[] args) { System.out.println(scale(5)); } private static final long X = scale(10); private static long scale(long value) { return X * value; } } Answer: 0 Spoilers below. If you print X in scale(long) and redefine X = scale(10) + 3 , the prints will be X = 0 then X = 3 . This means that X is temporarily set to 0 and later set to 3 . This is a violation of final ! The static modifier, in combination with the final modifier, is also used to define constants. The final modifier

Java: use static initializer blocks to register classes to global static registry

不想你离开。 提交于 2019-12-02 03:56:08
问题 I have an Eclipse Plug-in with references to some JAR s files (which where configured in the Runtime tab of the MANIFEST.MF ). I can access and instantiate classes contained in those JAR s files, so they are contained in the classpath. I wanted the classes to register themselves to a global static registry, so I added a static initializer block to all of them: public class SomeStrategy extends Strategy { static { StrategyRegistry.register("SomeStrategy", SomeStrategy.class); } } I have

Spring static initialization of a bean

余生颓废 提交于 2019-12-02 03:25:46
问题 Hey, how one should deal with static initializations in Spring ? I mean, my bean has a static initialization private static final Map<String, String> exceptionMapping = ErrorExceptionMapping.getExceptionMapping(); And I need to take care that ErrorExceptionMapping is loaded before. I tried this: <bean id="errorExceptionMapping" class="cz.instance.transl.util.ErrorExceptionMapping" /> <bean id="validateService" class="cz.instance.transl.services.ValidateService" depends-on=

Spring static initialization of a bean

懵懂的女人 提交于 2019-12-01 23:42:32
Hey, how one should deal with static initializations in Spring ? I mean, my bean has a static initialization private static final Map<String, String> exceptionMapping = ErrorExceptionMapping.getExceptionMapping(); And I need to take care that ErrorExceptionMapping is loaded before. I tried this: <bean id="errorExceptionMapping" class="cz.instance.transl.util.ErrorExceptionMapping" /> <bean id="validateService" class="cz.instance.transl.services.ValidateService" depends-on="errorExceptionMapping" > But I got java.lang.NoClassDefFoundError: Could not initialize class cz.instance.transl.util

Java: use static initializer blocks to register classes to global static registry

好久不见. 提交于 2019-12-01 23:38:56
I have an Eclipse Plug-in with references to some JAR s files (which where configured in the Runtime tab of the MANIFEST.MF ). I can access and instantiate classes contained in those JAR s files, so they are contained in the classpath. I wanted the classes to register themselves to a global static registry, so I added a static initializer block to all of them: public class SomeStrategy extends Strategy { static { StrategyRegistry.register("SomeStrategy", SomeStrategy.class); } } I have several of those classes which should all register themselves to the StrategyRegistry (which is just a static

MSVC 2017 violating static initialization order within single translation unit

左心房为你撑大大i 提交于 2019-12-01 22:14:06
MSVC 2017 Community with -std=c++17 chokes on the following example: #include <iostream> struct TC { static TC const values[]; static TC const& A; static TC const& B; static TC const& C; int const _value; }; inline constexpr TC const TC::values[]{ { 42 }, { 43 }, { 44 } }; inline constexpr TC const& TC::A{ values[0U] }; inline constexpr TC const& TC::B{ values[1U] }; inline constexpr TC const& TC::C{ values[2U] }; int main(int, char**) noexcept { std::cout << std::boolalpha << "&A == &values[0]? " << (&TC::A == &TC::values[0U]) << "\n" << "&B == &values[1]? " << (&TC::B == &TC::values[1U]) <<

Thread-safety of static initializers in C#

Deadly 提交于 2019-12-01 18:14:27
Everyone says static initializers are thread-safe, but I'm worried about a particular detail. Let's say I have static class MyStaticClass { public static readonly object myField = MyOtherClass.GetNewObject(); } static class MyOtherClass { public static object GetNewObject() { /* arbitrary code that returns a new object */ } } Which of the following does C# guarantee, when MyStaticClass.myField is not yet initialized? If threads 1 and 2 try to access myField together (in that order), GetNewObject will have started executing before thread 2 reads myField . If threads 1 and 2 try to access