Is it possible to use a getter for a variable across an entire class?

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
Qriist
Posts: 161
Joined: 11 Sep 2016, 04:02

Is it possible to use a getter for a variable across an entire class?

Post by Qriist » 02 Dec 2024, 12:25

Before anything else: I have a working approach, everything returns as expected, nothing's broken. I'm not asking for help, per se.

So, I have a large library I'm writing. It's designed to be user friendly for simple projects but allow for finer control as needed. This is accomplished by auto-creating certain handles and then defaulting to them when that variable was not explicitly passed. This works as intended.

To show you what I mean, consider this random example method:

Code: Select all

    AddEasyToMulti(easy_handle?,multi_handle?){ ;auto-invoked during EasyInit()
        easy_handle ??= this.easyHandleMap[0][-1]   ;defaults to the last created easy_handle
        multi_handle ??= this.multiHandleMap[0][-1] ;defaults to the last created multi_handle
        ret := this._curl_multi_add_handle(multi_handle,easy_handle)
        this.easyHandleMap[easy_handle]["associated_multi_handle"] := multi_handle
        return ret
    }
The first two lines use the ??= operator to conditionally assign the easy_handle and multi_handle variables that then get subsequently used.

To put it another way, calling foo.AddEasyToMulti() is designed to be equally as valid as calling foo.AddEasyToMulti(easy_handle,multi_handle). Nearly all of the forward facing class methods (plus several supporting methods) have one or both of those ??= lines. I expect many more future methods to start with those boilerplate lines, as well.

I'm curious if there are any other possible approaches to handling default values in exposed class methods. In particular does a getter work across an entire class? If so, how would I go about implementing it? The variables can be encapsulated/attached to this if needed.

It's also okay if it can't be done. This is more of a thought exercise.

Return to “Ask for Help (v2)”