Wish buffer object could be used like object

Discuss the future of the AutoHotkey language
User avatar
V2User
Posts: 219
Joined: 30 Apr 2021, 04:04

Wish buffer object could be used like object

Post by V2User » 20 Nov 2023, 22:19

image.png
image.png (43.17 KiB) Viewed 2229 times

Code: Select all

class buffer2 extends buffer {
	p1=>NumGet(this,'int64')+this.p2
}
c:={p2:25,base:Buffer2(8)}.p1
Wish buffer object could be used completely as normal object, if it doesn't affect the performance.
coffee
Posts: 136
Joined: 01 Apr 2017, 07:55

Re: Wish buffer object could be used like object

Post by coffee » 29 Dec 2023, 00:13

Are you complaining about the curly brace syntax? or can you explain more about the example and the wish?
A huge point of the "extends" on native classes is that you can "extend" them to assign custom properties or functionalities as you see fit while still having access to the non-overriden native class' functionality. You are not being blocked because you can't use an instance of buffer2 as base, you are getting blocked from it because you didn't read the documentation part where it said you can only change the base of an object to that of a similar/most-immediate native type.

Object curly brace syntax definition creates an object from object.prototype. Not anything else. Using base inside curly braces won't change the fact that the curly braces are syntax sugar just for object.prototype.

https://www.autohotkey.com/docs/v2/lib/Object.htm#Base
If assigning the new base would change the native type of the object, an exception is thrown. An object's native type is decided by the nearest prototype object belonging to a built-in class, such as Object.Prototype or Array.Prototype. For example, an instance of Array must always derive from Array.Prototype, either directly or indirectly.
You can't re-base {...} with a higher abstraction since it breaks the limitation specified above. But the limitation only applies to the curly brace syntax.
Even then, what is the -2000 IQ pro autohotkey move you are trying to do in the example? you can assign properties directly into the instance of buffer2 and use it like a plain object. You can assign anything to anything and rebase anything to any base as long as it follows the valid prototype chain.

Code: Select all

class buffer2 extends buffer {
	p1 {
		get => ( NumGet(this,'int64')+this.p2 )
	}
}

myBuff2 := buffer2(8)
myBuff2.base := buffer2(8)
myBuff2.p2 := 25

msgbox(myBuff2.p1)

; can also do this
myBuff2.hello := "world"
msgbox(myBuff2.hello)
; or
anotherBuff2 := buffer2(8)
anotherBuff2.base := myBuff2

; i'm now a function, the best function
anotherBuff2.call := (*) => msgbox("it's me, the function")
anotherBuff2()

; but wait, there's more
msgbox(anotherBuff2.base == myBuff2)
msgbox((anotherBuff2 is Buffer2) " -- " (anotherBuff2 is Buffer))

You are extending an instance, but the instance is the stateful extension of the class prototype and the class object extension is the extension of the native type. Any of them can be changed directly.

The limitation on {... base: higherBase} may be in place due to internal optimizations on the data structures themselves, at least inferring from static call docs:
https://www.autohotkey.com/docs/v2/lib/Class.htm#Call
This static method is typically inherited from the Object, Array or Map class. It performs the following functions:

Allocate memory and initialize the binary structure of the object, which depends on the object's native type (e.g. whether it is an Array or Map, or just an Object).

edit:
dam son, misread that date as december 20
Image
User avatar
V2User
Posts: 219
Joined: 30 Apr 2021, 04:04

Re: Wish buffer object could be used like object

Post by V2User » 02 Jan 2024, 04:44

coffee wrote:
29 Dec 2023, 00:13
You can't re-base {...} with a higher abstraction since it breaks the limitation specified above. But the limitation only applies to the curly brace syntax.
Thanks for your answer. I have decided to cancel this usage in this situation now. Instead, I have defined properties directly on the buffer obj as an alternative. It can be seen in Bfr.ahk in my recent posts:
viewtopic.php?f=83&t=123348
can you explain more about the example and the wish?
This usage will get explained furtherly, in my subsequent script later.
Post Reply

Return to “AutoHotkey Development”