Advertisement

Guidance on when to use @ and &in

Started by November 28, 2024 08:47 AM
2 comments, last by johannesg 1 week, 4 days ago

I guess I'm a little confused… Is the following thinking correct? I can declare a function in two ways:

void foo (reftype @);
void foo (reftype &in);

In the first case, I should be ready to deal with nullptrs. In the second case, nullptr references still exist, but the runtime throws an exception before they reach me, so I don't have to worry about it (too much).

Following up from that, it is recommended (somewhere in the docs) that opEquals is declared as:

bool opEquals (reftype @);

This is great, because if I happen to pass a nullptr I can just deal with it myself. So…

reftype r1, r2;
if (r1 == r2) …

will tell us if r1 and r2 are valid. And this also works:

reftype r1, @r2;
if (r1 == r2) …

No problem, I can deal with the nullptr! But then:

reftype @r1, @r2;
if (r1 == r2) …

This throws a nullptr exception. That is, uhh, not something I would immediately expect from a comparison operator. Is there a way to declare opEquals so that it can deal with both parameters being nullptr?

@ is a pointer that is allowed to be null. And can be reassigned to different objects.

& is a pointer that is not allowed to be null. It cannot be reassigned to different objects.

== does a value comparison, it doesn't compare addresses. It will throw exception if the operands are null

is does an address comparison. It will not throw exception if the operands are null

opEquals is a class method. You cannot call methods on a null object, hence the exception.

To check if two handles are equal do

if( r1 is r2 )

To check if their values are equal while not getting exceptions on null handles do

if( r1 !is null && r2 !is null && r1 == r2 )

(or use try / catch to catch the null pointer exception if you don't really expect them to be null)

A comparison between C++ and AngelScript

C++ AngelScript

* @

& &

*r1 == *r2 (both operands are pointers, i.e. *) r1 == r2 (value comparison)

r1 == r2 (both operands are pointers, i.e. *) r1 is r2 (address comparison)

type::operator== type::opEquals

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

Advertisement

Thanks for all your explanations, it is much appreciated!

I'm revising the scripting interface I did a few years ago (cough, when I was asking a bunch of silly questions as well ;-) ) and finding things I'm worrying about. I just wanted to verify my basic understanding of what the @ and & symbols really mean in Angelscript. Thanks again for enlightening me.

Advertisement