Skip to content

Instantly share code, notes, and snippets.

@Axlefublr
Created October 21, 2022 07:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Axlefublr/3f2959445a1a4233d422fa30d5107a7c to your computer and use it in GitHub Desktop.
Save Axlefublr/3f2959445a1a4233d422fa30d5107a7c to your computer and use it in GitHub Desktop.

At first, objects seem scary, but once you learn how to use them you will love them!

In v1, objects very much exist, but aren't used in places where they make a lot of sense to be in

For example, v1 has this janky global variable "ErrorLevel", that gets updated by many commands

You used to have to use it to check what a command "returns"

Well sike, commands don't return anything, and Errorlevel can technically change at any moment

Often you actually had to save Errorlevel to a temporary variable, to check the value later.

Why would we need a middle man now?

Wouldn't it be better if the command just returned the value we want?

Well yes, and v2 does exactly that.

As you may already know, v2 replaced all commands with functions and removed the need for errorlevel by just returning the value

That's exactly where objects come in

For functions that return multiple values, we got a nice bundle of them

We can use the object that's returned to have the cake and eat it

Let's have a look at guis

In v1, they occupied the global space, and were an overall pain in the ass to contain

You call the id of the gui and then do whatever with it

In v2, you don't have to deal with that: guis are object oriented

You can easily have a gui in a function, and overall, have as many guis as you want in a single script, without any hassle

That's because you get the object of the gui, and then operate on that object

You won't need to worry about affecting some other gui in any way.

"Object" is actually a really broad abstraction

Arrays are objects, and so are maps

And both of them have their own properties and methods

Now hold on, Axlefublr (if that's even your real name), what are properties and methods?

The difference is actually really simple

I laughed my ass off when I learnt it

Here I'm using a variable and calling a function

Now I'm using a property and calling a method

Yes, the only difference between a normal variable and a property, a function and a method is belonging.

Properties and methods belong to something, like a class or an object

And to refer to a property or method, you first have to access the object

Alright, let's create our own object

The proper method to create an object is to call its function, but we don't actually need to because we can use curly braces to reach the same effect

To create a property, you name it literally

It might look like it's a variable, but this is actually seen more or less as a string

Then add a colon with an optional space and specify your value

To declare more than one property, you have to separate them with commas

And since ahk has such a clear separator, you can format the property declarations as you want

All on a single line, one true brace style, on multiple lines or any other formatting choice

Whitespace doesn't matter

That's because instead of relying on newlines, we have a bunch of expressions separated by commas in brackets

Brackets are what contains the property definitions, and commas are what separates them

This idea is pretty much the same for normal brackets in arrow functions

I go into more detail on statement versus expression in my tutorial on them

Talking about arrow functions: let's declare a method now

The key declaration is the same, what's different is the function definition

We can't declare a function as normal, because as I said before, the value expects an expression

And to add, even though we're declaring a function, there would be nothing to tell ahk that it's that function we want to call

So, we should declare the function elsewhere and get its function object as the value to the key

Yes, we need a function object, so we can also declare an inline function using arrow syntax or using bind

More on function objects in my tutorial on them and in the mentioned tutorial on arrow functions

Now when we access the object, we can call the value, which is actually a function object (which can be called)

Boom - we called the method we declared

How useful it is to declare a method like this is up to you to decide

But this is an interesting insight to how methods work in ahk overall

Even in classes, this is actually how methods work behind the scenes, more or less

If I'm wrong, please feel free to generate free content for me in the comments by explaining why I'm wrong

Right now I'm gonna show you the biggest use for objects aside from arrays, guis and the like

Unfortunately, the rest of the transcript was lost. Not going to happen again with how I structure uploading

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment