1 Warning
#8

Quote:
Originally Posted by ******
Посмотреть сообщение
I see someone's been reading their documentation! That makes me very happy!

If anyone cares, here's a little more explanation as to WHY the forward is required. In PAWN everything has the same type (32 bit cell), however you can treat some variables differently using tags. Floats have the "Float" tag, true and false have the "bool" tag. "Float" starts with a capital so is strong, "bool" doesn't so is weak. Strong tags can only be stored in variables with the right tag, weak tags can be stored in any weak tag variable:

pawn Код:
new a = false; // Works.
new Float:a = false; // Shoulsn't work but does - see below.
Note that the default tag, the tag of everything with no specified tag is "_:".

PAWN actually has operator overloading - I bet very few of you knew that! So you can do:

pawn Код:
stock Moo:operator+(Moo:a, Moo:b)
{
    return Moo:(_:a * 2 + _:b * 2);
}
Now if you do:

pawn Код:
new Moo:c = Moo:3 + Moo:4;
You will get 14, not 7.

Now if you look in float.inc you will see a whole load of overloaded operators, including this one:

pawn Код:
native Float:operator=(oper) = float;
Don't worry about the syntax, it uses a feature called internal/external names (another thing I bet you didn't know), but it means doing this will work:

pawn Код:
new a = 5, Float:b = a;
a has tag "_:", b has tag "Float:". This shouldn't work but the overloaded "=" operator, which takes a variable of tag "_:" and returns a variable of tag "Float:" means that the compiler knows how to convert the two - this will compile as:

pawn Код:
new _:a = 5, Float:b = float(a);
Now imagine this code:

pawn Код:
new Float:b = moo();
The compiler has no information about the function "moo" yet, so it generates this code:

pawn Код:
new Float:b = float(moo());
Then later on in your code you have:

pawn Код:
Float:moo() return 10.0;
Now the compiler knows that the "moo" function returns a "Float:" tag value, which means the code above using "float()" to convert from a float is wrong - hence the reparse. As Kyosaur said there are two ways to fix this. The first is to put the "moo" function before where it is used so that the compiler knows it returns a "Float:" tag, the second is to forward the function - again so the compiler knows it returns a "Float:", it just doesn't yet know what it does.
Wow, really nice post!

I dont even think the pawn language guide went into as much detail as you just did lol (its been a while since i read this part tbh). Thanks for taking the time to explain this .

I knew about overloading operators (I think you're the one who wrote the article on the wiki regarding this actually?), but this my first time hearing about internal/external names (i've seen you use this for "mirroring" unformat to sscanf, but thats really about it), pretty cool stuff.
Reply


Messages In This Thread
1 Warning - by Scarface~ - 26.08.2010, 13:17
Re: 1 Warning - by Kyosaur - 26.08.2010, 13:22
Re: 1 Warning - by Jakku - 26.08.2010, 13:27
Re: 1 Warning - by Kyosaur - 26.08.2010, 13:29
Re: 1 Warning - by Scarface~ - 26.08.2010, 13:44
Re: 1 Warning - by Kyosaur - 26.08.2010, 13:47
Re: 1 Warning - by Scarface~ - 26.08.2010, 13:49
Re: 1 Warning - by Kyosaur - 26.08.2010, 15:16

Forum Jump:


Users browsing this thread: 2 Guest(s)