Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: Create a clone of an object #1

Closed
junkew opened this issue Aug 19, 2018 · 1 comment
Closed

Feature request: Create a clone of an object #1

junkew opened this issue Aug 19, 2018 · 1 comment
Assignees
Labels
enhancement New feature or request

Comments

@junkew
Copy link

junkew commented Aug 19, 2018

Would be nice to could write

...
$oFoo2=$oFoo1.clone
...

Example
how it works at the moment (which in general is fine but would be nice to quickly clone) is that a var references another object where you sometimes want a copy/clone

#include <AutoItObject_Internal.au3>
$cFoo=IDispatch()
$oFoo1=$cFoo
$oFoo2=$cFoo

$oFoo1.Property1="Nice"

;~ And now the fun comes as they both point to same object

consolewrite($oFoo2.Property1)

Endgoal I would like to reach is that you have

;~ Have a generic method reference that is shared
$cFoo.Method1 = cFoo_Method1
func cFoo_Method1
;~ whatever we want to have shared as a method printing a property of the current object
ConsoleWrite($self.parent.Property1)
endfunc

$oFoo1=cFoo.clone
$oFoo2=cFoo.clone

;~ Have independent propertyvalues
$oFoo1.Property1="Hello world 1"
$oFoo2.Property1="Hello world 2"

$oFoo1.Method1
$oFoo2.Method1

@genius257 genius257 added the enhancement New feature or request label Aug 20, 2018
@genius257 genius257 self-assigned this Aug 20, 2018
@genius257
Copy link
Owner

genius257 commented Aug 21, 2018

Hi @junkew.
The clone feature is implemented via the method __assign.
This is done to enable multiple objects to be referenced, a feature i wanted so inheritance from more than one would be possible, to on a very basic level, mimic PHP class extension and traits.

see __assign

The following code shows you how it can be used.

#include <AutoItObject_Internal.au3>

$cFoo=IDispatch()

func cFoo_Method1($self)
;~ whatever we want to have shared as a method printing a property of the current object
ConsoleWrite($self.parent.Property1)
endfunc

$cFoo.Method1 = cFoo_Method1

$oFoo1=$cFoo

;~ Have independent propertyvalues
$oFoo1.Property1="Hello world 1"

$oFoo2=IDispatch()

$oFoo2.__assign($oFoo1)

$oFoo2.Property1="Hello world 2"

MsgBox(0, "", $oFoo1.Property1)
MsgBox(0, "", $oFoo2.Property1)

MsgBox(0, "", FuncName($oFoo1.Method1))
MsgBox(0, "", FuncName($oFoo2.Method1))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants