Macroses with variable number of optional arguments
Hi,
I need a simple function for formatting a string, like in Python or C#. That's easy:
Class Very.Very.Long.Class.Name
{
ClassMethod Format(fmt As %String, args...) As %String [ Language = python ]
{
return fmt.format(*args)
}
}
To simplify calls, I want to wrap the function into a macro:
ROUTINE StringUtil [Type=INC]
#define FMT(%fmt,%a0) ##class(Very.Very.Long.Class.Name).Format(%fmt,%a0)
/// #define FMT(%fmt,%a0,%a1) ##class(Very.Very.Long.Class.Name).Format(%fmt,%a0,%a1)
/// #define FMT(%fmt,%a0,%a1,%a2) ##class(Very.Very.Long.Class.Name).Format(%fmt,%a0,%a1,%a2)
/// #define FMT(%fmt,%a0,%a1,%a2,%a3) ##class(Very.Very.Long.Class.Name).Format(%fmt,%a0,%a1,%a2,%a3)
/// #define FMT(%fmt,%a0,%a1,%a2,%a3,%a4) ##class(Very.Very.Long.Class.Name).Format(%fmt,%a0,%a1,%a2,%a3,%a4)
The following code won't compile, IRIS fails with either "MPP5614 : Not enough arguments to macro: 'FMT'" or "MPP5613 : Too many arguments to macro: 'FMT'"
Set x = $$$FMT("{0} {1}", "Hello", "World")
It seems to me IRIS supports macro overrides, so if I uncomment that overrides then the .inc file gets compiled but the .cls file doesn't. Is this problem solvable at all?
Comments
I'd just use def1arg:
#def1arg FMT(%parms) ##class(Very.Very.Long.Class.Name).Format(%parms)
(From memory, I don't have an instance to test available at the moment.) The macro 'intellisense' doesn't help with the parameters anymore, but in your case they are very easy to remember: format, args. I use something like this myself.
Your macro works perfectly, thanks a lot!
Caché/IRIS already has ready-made macros: $$$FormatText(), $$$FormatTextHTML(), $$$FormatTextJS()
, which may be useful to you and which use #def1arg.