Defer initialization of objects until they are needed.

Propose new features and changes
iseahound
Posts: 1444
Joined: 13 Aug 2016, 21:04
Contact:

Defer initialization of objects until they are needed.

13 Nov 2018, 10:28

When writing code, it makes sense to group all of the objects in the beginning. So I have code that reads a := new Object(), b := new Object, c := new Object()...

It would be much more efficient to allow their initialization to be delayed until their first call. Meaning that the __New() meta function of some Object() is not run until one of their methods is called. This is useful as sometimes these objects aren't even used in later code, as they are all initialized in the beginning!

Some sort of new syntax would be involved:

Code: Select all

a := newDeferred Object()
User avatar
nnnik
Posts: 4500
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: Defer initialization of objects until they are needed.

13 Nov 2018, 10:35

With meta functions you could make that yourselves.
Recommends AHK Studio
iseahound
Posts: 1444
Joined: 13 Aug 2016, 21:04
Contact:

Re: Defer initialization of objects until they are needed.

13 Nov 2018, 11:26

I don't know. I'd prefer the choice of delaying the new command vs forcing the new operator to delay it.

Code: Select all

obj := new a()
Sleep 2500
obj.California("sleeping")
Sleep 2500
obj.California("eating")

class a {

   __New() {
      if !(this.initalize) {
         MsgBox % "Function:" A_Space "Delay" A_Space A_ThisFunc
         return this
      }
      ; Resource heavy procedure here.
      MsgBox % "Function:" A_Space A_ThisFunc
      ; Implicit return this
   }

   ; Implicit __Call(self, terms*)
   __Call() {
      if !(this.initalize) {
         MsgBox % "Function:" A_Space "Delayed" A_Space A_ThisFunc
         this.initalize := true
         this := new this
         ;Implicit return this[self](terms*)
      }
      ; Implicit return this[self](terms*)
   }

   california(n := "dreaming") {
      MsgBox % "California" A_Space n
   }
}
Not tested on actual libraries.
User avatar
nnnik
Posts: 4500
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: Defer initialization of objects until they are needed.

13 Nov 2018, 11:43

No I mean you can create a function/class that does this for you.

Code: Select all

class delayedInitialization {
	static waitingForInitialization := {}
	__New(p*) {
		delayedInitialization.waitingForInitialization[this] := p
	}
	
	initialize() {
		newData := delayedInitialization.waitingForInitialization[this]
		newClass := newData.removeAt(1)
		ObjSetBase(this, newClass)
		newClass.__init.call(this)
		newClass.__new.call(this, newData*)
	}
	
	__Call(fn, p*) {
		delayedInitialization.initialize.call(this)
		return this[fn](p*)
	}
	
	__Get(p*) {
		delayedInitialization.initialize.call(this)
		return this[p*]
	}
	
	__Set(p*) {
		delayedInitialization.initialize.call(this)
		value := p.pop()
		return this[p*] := value
	}

}
Note: untested
Recommends AHK Studio
iseahound
Posts: 1444
Joined: 13 Aug 2016, 21:04
Contact:

Re: Defer initialization of objects until they are needed.

13 Nov 2018, 12:43

This is pretty funny. You wrote external code that can handle the concept of delayed initialization. I wrote internal code that could handle delayed initialization, but didn't like how it hijacked the __New and __Call meta-functions because it didn't provide the user a choice whether they wanted their initialization in the moment or delayed. I like your code better to be honest. Is it possible to get rid of the references to class delayedInitialization as I will likely nest the class?
Last edited by iseahound on 26 Mar 2021, 12:17, edited 1 time in total.
User avatar
nnnik
Posts: 4500
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: Defer initialization of objects until they are needed.

13 Nov 2018, 14:54

I'm glad you like it :)

You can resolve the dependencies dynamically by using A_ThisFunc.
Other than that there should be a solution involving ObjGetBase and then getting the method or data from there.
A last work around would be exceptions for .base but that has side effects.
Recommends AHK Studio
iseahound
Posts: 1444
Joined: 13 Aug 2016, 21:04
Contact:

Re: Defer initialization of objects until they are needed.

08 May 2020, 18:18

Again, functionality like this makes handling a collection of objects easier. There should be a built in wrapper object that delays the execution of "new" until necessary.

Return to “Wish List”

Who is online

Users browsing this forum: No registered users and 45 guests