lazy-initialization

How to create decorator for lazy initialization of a property

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 =...

Difference when serializing a lazy val with or without @transient

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...

lazy function definitions in scala

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...

LazyInitializationException with CDI Managed Bean and Stateful Session Bean

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...

Property initializers run before 'self' is available

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:...

Lazy Var vs Let

I want to use Lazy initialization for some of my properties in Swift. My current code looks like this: lazy var fontSize : CGFloat = { if (someCase) { return CGFloat(30) } else { return CGFloat(17) } }() The thing is that once the fontSize is set it will NEVER change. So I wanted to do something...

Scala lazy values : performance penalty? Threadsafe? [duplicate]

Possible Duplicate: What's the (hidden) cost of lazy val? (Scala) Scala allows the definition of lazy values lazy val maybeUnusedValue = someCostlyInitialization where someCostlyInitialization is evaluated only on the first use of maybeUnusedValue . That is, it is evaluated at most once, and if...

Kotlin lazy properties and values reset: a resettable lazy delegate

So I use kotlin for android, and when inflating views, I tend to do the following: private val recyclerView by lazy { find<RecyclerView>(R.id.recyclerView) } This method will work. However, there is a case in which it will bug the app. If this is a fragment, and the fragment goes to the backstack,...