lazy-initialization

Overriding property getters with lazy loading in Objective-C

為{幸葍}努か 提交于 2019-12-10 04:03:05
问题 I usually lazy instantiate my @property objects in their getter methods like this: @interface MyGenericClass : UIViewController @property(nonatomic, readonly) UIImageView *infoImageView // ... @implementation GenericClass - (UIImageView *)infoImageView { if (!_infoImageView) { _infoImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"PlaceholderInfoImage"]]; } return _infoImageView; } But when subclassing, I would often like to override some of the @properties to be more

Getting “could not initialize proxy - no Session” despite the fact I'm using a @Transactional annotation

走远了吗. 提交于 2019-12-10 02:38:07
问题 I'm using Spring 3.1.1.RELEASE and Hibernate 4.1.0.Final. I'm getting a "could not initialize proxy - no Session" exception, despite the fact I'm wrapping the relevant method call in a @Transactional annotation. Here's my method … @Service("teacherImportService") public class TeacherImportServiceImpl extends AbstractmyprojectService { … @Transactional @Override public void saveUserObject(final JsonObject jsonData) { … final myprojectOrganization myprojectOrg = myprojectOrgDao

Why would I want to re-implement lazy?

和自甴很熟 提交于 2019-12-09 14:27:48
问题 I was reading the section on Lazyness [sic] over at Twitter's Effective Scala page, which includes this suggestion (emphasis is mine): Use lazy fields for this purpose [computing and caching values on-demand], but avoid using lazyness when lazyness is required by semantics . In these cases it's better to be explicit since it makes the cost model explicit, and side effects can be controlled more precisely. I don't understand why they would make this claim. Why would it be better to avoid using

Python class member lazy initialization

孤者浪人 提交于 2019-12-09 05:15:26
问题 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 回答1: You could use a @property on the

lazy function definitions in scala

与世无争的帅哥 提交于 2019-12-09 04:17:02
问题 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

Is there a way to late-initialize a member variable (a class) in C++?

荒凉一梦 提交于 2019-12-08 16:39:24
问题 I am coming from the Java background. I have the following program. #include <string> #include <iostream> class First { public: First(int someVal): a(someVal) { } int a; }; class Second { public: First first; Second() { // The other option would be to add default value as ": first(0)" first = First(123); } }; int main() { Second second; std::cout << "hello" << second.first.a << std::endl; } In class Second , I wanted to variable first to remain uninitialized until I specifically initialize it

Array with lazy elements

☆樱花仙子☆ 提交于 2019-12-08 06:26:23
问题 I was wondering if there is a construct in the Swift 3 programming language that allows me to store some objects in an array, but initialize each element in this array lazily. Imagine this example class: class A { let test = "hello" let test2 = 2.0 } Now I want to store an array of 'A' objects in an array of another class, like this: class B { var lazy(?) array: [A] = { // Some code to initialize the element being accessed }() } If I access any element now, it would be cool if it is

Array with lazy elements

一曲冷凌霜 提交于 2019-12-08 04:26:26
I was wondering if there is a construct in the Swift 3 programming language that allows me to store some objects in an array, but initialize each element in this array lazily. Imagine this example class: class A { let test = "hello" let test2 = 2.0 } Now I want to store an array of 'A' objects in an array of another class, like this: class B { var lazy(?) array: [A] = { // Some code to initialize the element being accessed }() } If I access any element now, it would be cool if it is initialized just when I am accessing it, so lazily print(B.array[1].test) (element at index one is now

Why do the courses at Stanford use the lazy initialisation? [duplicate]

北慕城南 提交于 2019-12-07 12:32:01
问题 This question already has answers here : When to use lazy instantiation in iOS? (4 answers) Closed 5 years ago . Why does the course at Stanford use the lazy initialization for all getters? Is this correct? Does it have any real advantage? One advantage (for me) is that the init method can become much shorter and you need not check if a variable is allocated. 回答1: The idea is to load resources on demand. This way everything loads faster and when needed. In the cases it's not used, it doesn't

How to initialize an array whose size is initially unknown?

自闭症网瘾萝莉.ら 提交于 2019-12-07 07:49:33
问题 Say I have this: int x; int x = (State Determined By Program); const char * pArray[(const int)x]; // ?? How would I initialize pArray before using it? Because the initial size of the Array is determined by user input Thanks! 回答1: You cannot initialize an array at compile-time if you are determining the size at run-time. But depending on what you are trying to do, a non-const pointer to const data may provide you with what you're going for. const char * pArray = new const char[determine_size()