StringFormat in a function-like macro

 

I'm wondering if there's a way to counter this :

#define fun(X) StringFormat(X);
string s = fun("Help %s","Me");

It goes

too many arguments for function-like macro 'fun'
'StringFormat' - wrong parameters count

which is what's supposed to happen, but how to define the macro so the second line stays the same

 
ILoveBouldering: I'm wondering if there's a way to counter this : It goes too many arguments for function-like macro 'fun' 'StringFormat' - wrong parameters count which is what's supposed to happen, but how to define the macro so the second line stays the same

You can't use Macros that way, as you have to set a fixed number of parameters in the macro.

You can however (i think) use a Stringizing expansion as follows:

#define fun(X) StringFormat( #X );
string s = fun("Help %s","Me");

Please note, that I have never tried it myself before.

EDIT: Forget my suggestion. It expands to a string not code.

 

Try to see if a Variadic Macro will work in MQL. I'v never tried it and I don't know if MQL supports it.

#define fun( ... ) StringFormat( __VA_ARGS__ );
string s = fun("Help %s","Me");

Try the different variation here and see if any of them work

#define fun( args... ) StringFormat( args );
string s = fun("Help %s","Me");
EDIT: Just tried both and MQL did not support it and gave compilation errors.
 
Can't think of any other option to work around it. You will have to use a fixed number of parameters.
 
Yeah it's not working for me either, but thank you for always trying to help though <3
 
ILoveBouldering #: Yeah it's not working for me either, but thank you for always trying to help though <3
You are welcome!
 

If you create a macro with arguments, anything that goes inside the parenthesis is an argument, and since they are separated with commas if there are more than the macro could expect it gives compilation errors (imagine a macro with 2 arguments and you call it with 3, how would it parse them?)

In your specific case you could have 2 options, the first is to use 2 arguments:

#define fun(X,Y) StringFormat(X,Y);
string s = fun("Help %s","Me") //Notice that it doesn't need an extra ";", it's already in the macro

And the second would be not to use arguments

#define fun StringFormat
string s = fun("Help %s","Me");

In this case, the compiler has no doubt that the parenthesis are outside of the macro since it has no arguments (and it fits quite well in this example). However, now you must put the ";" at the end

And if your macro really needed parameters, then it could work with double parenthesis and with the open function only at the end (but that would be getting quite complex):

#define fun(x) x = StringFormat
fun(string s)("Help %s","Me");


Edit: I noticed there's another way in the middle of both options, which is to use another macro:

#define TO_PARAMS(x,y) x,y
#define fun(X) StringFormat(X);
string s = fun(TO_PARAMS("Help %s","Me"))

I don't know if it would be very useful however, at this point it seems to be overcomplicating things (and it can't be used to get "pseudo-variadic functions" in mql5, since everything is at compile level)

 
Manuel Alejandro Cercos Perez #:

If you create a macro with arguments, anything that goes inside the parenthesis is an argument, and since they are separated with commas if there are more than the macro could expect it gives compilation errors (imagine a macro with 2 arguments and you call it with 3, how would it parse them?)

In your specific case you could have 2 options, the first is to use 2 arguments:

And the second would be not to use arguments

In this case, the compiler has no doubt that the parenthesis are outside of the macro since it has no arguments (and it fits quite well in this example). However, now you must put the ";" at the end

And if your macro really needed parameters, then it could work with double parenthesis and with the open function only at the end (but that would be getting quite complex):

Edit: I noticed there's another way in the middle of both options, which is to use another macro:

I don't know if it would be very useful however, at this point it seems to be overcomplicating things (and it can't be used to get "pseudo-variadic functions" in mql5, since everything is at compile level)

Your post is informative but does not address the key issue. The OP is trying to achieve the equivalent of a Variadic Macro in MQL where any number of parameters could be used with the macro.

Our conclusion is, that not only does MQL not support Variadic Macros, there are also no equivalent options for it.

 
Fernando Carreiro #:

Your post is informative but does not address the key issue. The OP is trying to achieve the equivalent of a Variadic Macro in MQL where any number of parameters could be used with the macro.

Our conclusion is, that not only does MQL not support Variadic Macros, there are also no equivalent options for it.

Yes, it's not possible. I've also tried several times to get something similar, but you can only get so close with "dirty" tricks heheh

 

***

As Manuel said, you try to call a macro which accepts one parameter with the wrong number of parameters 

Reason: