lazy-initialization

Should C# have a lazy key word

萝らか妹 提交于 2019-12-03 12:27:01
Should C# have a lazy keyword to make lazy initialization easier? E.g. public lazy string LazyInitializeString = GetStringFromDatabase(); instead of private string _backingField; public string LazyInitializeString { get { if (_backingField == null) _backingField = GetStringFromDatabase(); return _backingField; } } I don't know about a keyword but it now has a System.Lazy<T> type. It is officially part of .Net Framework 4.0 . It allows lazy loading of a value for a member . It supports a lambda expression or a method to provide a value. Example: public class ClassWithLazyMember { Lazy<String>

shared, weak and lazy pointers in C++

感情迁移 提交于 2019-12-03 11:53:47
问题 Is anyone aware of an implementation of shared_ptr and weak_ptr together with a lazy initialization partner? The requirements of the classes were: A lazy_ptr class that allows a client to construct the object later (if at all), without needing the constructor implementation A weak_lazy_ptr class that has three possible states: not yet constructed (won't lock to a shared_ptr ), constructed (will lock to a shared_ptr ) and destroyed (won't lock to a shared_ptr ) I created some classes that didn

Difference when serializing a lazy val with or without @transient

Deadly 提交于 2019-12-03 08:51:03
问题 Working on spark, sometimes I need to send a non-serializable object in each task. A common pattern is @transient lazy val , e.g class A(val a: Int) def compute(rdd: RDD[Int]) = { // lazy val instance = { @transient lazy val instance = { println("in lazy object") new A(1) } val res = rdd.map(instance.a + _).count() println(res) } compute(sc.makeRDD(1 to 100, 8)) I found that @transient is not necessary here. lazy val can already create the non-serializable upon each task is executed. But

How to create decorator for lazy initialization of a property

旧城冷巷雨未停 提交于 2019-12-03 08:14:50
I want to create a decorator that works like a property, only it calls the decorated function only once, and on subsequent calls always return the result of the first call. An example: def SomeClass(object): @LazilyInitializedProperty def foo(self): print "Now initializing" return 5 >>> x = SomeClass() >>> x.foo Now initializing 5 >>> x.foo 5 My idea was to write a custom decorator for this. So i started, and this is how far I came: class LazilyInitializedProperty(object): def __init__(self, function): self._function = function def __set__(self, obj, value): raise AttributeError("This property

Python class member lazy initialization

泪湿孤枕 提交于 2019-12-03 04:47:57
I would like to know what is the python way of initializing a class member but only when accessing it, if accessed. I tried the code below and it is working but is there something simpler than that? class MyClass(object): _MY_DATA = None @staticmethod def _retrieve_my_data(): my_data = ... # costly database call return my_data @classmethod def get_my_data(cls): if cls._MY_DATA is None: cls._MY_DATA = MyClass._retrieve_my_data() return cls._MY_DATA You could use a @property on the metaclass instead: class MyMetaClass(type): @property def my_data(cls): if getattr(cls, '_MY_DATA', None) is None:

Difference when serializing a lazy val with or without @transient

亡梦爱人 提交于 2019-12-02 22:46:25
Working on spark, sometimes I need to send a non-serializable object in each task. A common pattern is @transient lazy val , e.g class A(val a: Int) def compute(rdd: RDD[Int]) = { // lazy val instance = { @transient lazy val instance = { println("in lazy object") new A(1) } val res = rdd.map(instance.a + _).count() println(res) } compute(sc.makeRDD(1 to 100, 8)) I found that @transient is not necessary here. lazy val can already create the non-serializable upon each task is executed. But people suggest using @transient . What is the advantage, if we set @transient on the non-initialized lazy

lazy function definitions in scala

大憨熊 提交于 2019-12-02 20:51:40
I've been learning scala and I gotta say that it's a really cool language. I especially like its pattern matching capabilities and function literals but I come from a javascript, ruby background and one of my favorite patterns in those languages is the lazy function and method definition pattern. An example in javascript is var foo = function() { var t = new Date(); foo = function() { return t; }; return foo(); }; The same code with minor tweaks works in ruby where you just use the singleton object to redefine the method after the computation is performed. This kind of thing comes in really

LazyInitializationException with CDI Managed Bean and Stateful Session Bean

不羁的心 提交于 2019-12-01 19:36:50
I have a CDI Managed Bean (a bean annotated with @Named that is used on JSF) that has a Stateful Session Bean injected. This session bean is like a service, it has the entity manager (annotated with @PersistenceContext(type= PersistenceContextType.EXTENDED)) and expose come methods to manipulate some entities. Those entities are on the managed bean, who is ConversationScoped. Then, the JSF calls a method of the managed bean and the managed bean calls some method of the "service" (the stateful session bean). I don't know if this is the best design, but it was working well. But there's an Entity

Property initializers run before 'self' is available

懵懂的女人 提交于 2019-12-01 15:44:45
Seems like I'm having a problem with something that shouldn't be the case... But I would like to ask for some help. There are some explanations here on the Stack I don't get. Having two simple classes where one refers to another, as per below; class User { lazy var name: String = "" lazy var age: Int = 0 init (name: String, age: Int) { self.name = name self.age = age } } class MyOwn { let myUser: User = User(name: "John", age: 100) var life = myUser.age //Cannot use instance member 'myUser' within property initializer //property initializers run before 'self' is available } I get the commented

Property initializers run before 'self' is available

最后都变了- 提交于 2019-12-01 13:49:57
问题 Seems like I'm having a problem with something that shouldn't be the case... But I would like to ask for some help. There are some explanations here on the Stack I don't get. Having two simple classes where one refers to another, as per below; class User { lazy var name: String = "" lazy var age: Int = 0 init (name: String, age: Int) { self.name = name self.age = age } } class MyOwn { let myUser: User = User(name: "John", age: 100) var life = myUser.age //Cannot use instance member 'myUser'