Combining int function with float function using tagof...
#1

Suppose I have these:
pawn Код:
stock range(...)
{
    new mini = getarg(0), maxi = mini;
    for(new i = numargs() - 1; i != 1; i--) {
        if(getarg(i) > maxi)
            maxi = getarg(i);
        if(getarg(i) < mini)
            mini = getarg(i);
    }
   
    return maxi - mini;
}

stock Float:frange(Float:...)
{
    new Float:mini = Float:getarg(0), Float:maxi = mini;
    for(new i = numargs() - 1; i != 1; i--) {
        if(Float:getarg(i) > maxi)
            maxi = Float:getarg(i);
        if(Float:getarg(i) < mini)
            mini = Float:getarg(i);
    }
   
    return maxi - mini;
}
I want to make them a single function that accepts both integer arguments and float arguments. I started with this, but it crashes the compiler:
pawn Код:
stock Float:xrange({Float, _}:...)
{
    new Float:mini, Float:maxi, tag = tagof(getarg(0));
    switch(tag) {
        case ((tagof(Float:))): {
            mini = Float:getarg(0);
            maxi = mini;
        }
        default: {
            mini = getarg(0);
            maxi = mini;
        }
    }
    // That's how far I got...
}
I already know about the compiler bug that causes "case (tagof(Float)" to crash, which is why I used "case ((tagof(Float))" (as ****** suggested somewhere a long time ago).

1. What am I doing wrong?
2. How do I fix it?

Never really used tagof before in something like this.
Reply
#2

Can u overload functions? I never tried, but if it works just do that. And shouldnt the float one accept int anyways?

Float:float = 1 (float is now 1.0) thought it would work like that, maybe i just used a bit too much java recently :P
Reply
#3

First of all you don't need to have unlimited arguments, just one like you said, so
Код:
stock Float:xrange({Float, _}:...)
becomes
Код:
stock Float:xrange({Float,_}:value, tagid = tagof(value))
since you only want Float and int to be accepted by one argument.

Look at this:
https://sampforum.blast.hk/showthread.php?pid=3459023#pid3459023
Reply
#4

@Marcel: No you can't really overload functions in PAWN. What I'm doing is as close as you can get to overloading.

@NaS: No, the entire point of the function is finding the range of the unlimited arguments.
Reply
#5

Nobody ever wants to help me... D:
Reply
#6

Quote:
Originally Posted by Crayder
Посмотреть сообщение
Nobody ever wants to help me... D:
I wanted to, but I was in a hurry and didn't notice that you required unlimited arguments

I only know you have to do it like in this include (using #emit):

https://sampforum.blast.hk/showthread.php?tid=313488

Or like a SendClientMessageFormat function found several times in the Useful Functions thread.

Just instead calling format you have to do your calculations.
But I guess you already looked at that.
Reply
#7

Quote:
Originally Posted by NaS
Посмотреть сообщение
I wanted to, but I was in a hurry and didn't notice that you required unlimited arguments

I only know you have to do it like in this include (using #emit):

https://sampforum.blast.hk/showthread.php?tid=313488

Or like a SendClientMessageFormat function found several times in the Useful Functions thread.

Just instead calling format you have to do your calculations.
But I guess you already looked at that.
I'm guessing you mean to use a format string, like "ddddfdfdfffdf", but that can't be the best solution. That could be a bit out of hand with enough values. It could work, even without using emit that would work. I just don't think that would be very pretty.
Reply
#8

Quote:
Originally Posted by Crayder
Посмотреть сообщение
I'm guessing you mean to use a format string, like "ddddfdfdfffdf", but that can't be the best solution. That could be a bit out of hand with enough values. It could work, even without using emit that would work. I just don't think that would be very pretty.
Thats not the part that I mean, with that technique (inside formatex) you can get all arguments you passed and process them (either float or int), just I can't tell you how to rewrite the code to not call format, but to do the stuff you want to do (calculate the range throughout all arguments).
That's all I can help you with.

I only know tagof does only work for known argument count since getarg will always return an integer (also it crashes..), the only way to know the tag is by specifying it in the function header (impossible for unknown argument count) like seen in the function overloading tutorial.

So you have 3 possible ways:
- format method like you said above
- pure float function with float() for every integer input
- #emit for which I cannot tell you how it works
Reply
#9

Quote:
Originally Posted by NaS
Посмотреть сообщение
Thats not the part that I mean, with that technique (inside formatex) you can get all arguments you passed and process them (either float or int), just I can't tell you how to rewrite the code to not call format, but to do the stuff you want to do (calculate the range throughout all arguments).
That's all I can help you with.

I only know tagof does only work for known argument count since getarg will always return an integer (also it crashes..), the only way to know the tag is by specifying it in the function header (impossible for unknown argument count) like seen in the function overloading tutorial.

So you have 3 possible ways:
- format method like you said above
- pure float function with float() for every integer input
- #emit for which I cannot tell you how it works
The assembly code there is mostly just to pass the parameters to format. I don't think that is at all related.

tagof is the only thing I know of that can do this.
Reply
#10

It can't be done in such a simple manner. Tags during runtime are only saved in the debug information.

Look at how I did it in SQLite (sort of here).

Other things you could do if you really wish to cut down on manual meta-data:
* Write a recursive macro that saves the type. Might work OK, but probably not.
* Write a recursive macro that wraps all arguments in a wrapper that returns an array with 2 cells - tag info and the value.
* Create a custom type that works in a similar manner to my previous suggestion. You could make custom conversion functions for T->_ and T->Float.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)