Page 1 of 1

MacroReturn

Posted: Sat Mar 11, 2006 10:38 am
by Rescator

Code: Select all

Macro test(a,b)
 MacroReturn a+b
EndMacro

Debug test(1,2)

;which would equal:

Debug 1+2
Which obviously was identical to:

Code: Select all

Macro test(a,b)
 a+b
EndMacro

Debug test(1,2)

;which would equal:

Debug 1+2
Now here is where MacroReturn truly shines:

Code: Select all

Macro test(a,b)
 c=a+b
 MacroReturn c
EndMacro

Debug test(1,2)
;which would equal:

c=1+2
Debug c
This would fail or get truncated:

Code: Select all

Macro test(a,b)
 c=a+b
 MacroReturn c
 c=a
 MacroReturn c
EndMacro

Debug test(a,b)
;which would equal:

c=a+b
Debug c
MacroReturn basicaly act's sort of like a newline if you will,
so anything before it ends up inserted before the line with the test() macro call,
and what is on the same line as the MacroReturn will end up in-place
on the line where the macro is actually used.

Practical example:

Code: Select all

Macro LibraryFunctionAddress(Library,Function)
 While NextLibraryFunction()
  If LibraryFunctionName()=Function
   *ptr=GetProcAddress_(LibraryID(Library),Function)
   Break
  EndIf
 Wend
 MacroReturn *ptr
EndMacro

#Somelib=0
OpenLibrary(#Somelib,"kernel32.dll")
Debug LibraryFunctionAddress(#SomeLib,"GetProcAddress")

;which would equal:

#Somelib=0
OpenLibrary(#Somelib,"kernel32.dll")
While NextLibraryFunction()
 If LibraryFunctionName()="GetProcAddress"
  *ptr=GetProcAddress_(LibraryID(#SomeLib),"GetProcAddress")
  Break
 EndIf
Wend
Debug *ptr
Yeah yeah, stupid example, but you get the idea I hope :)

Posted: Sat Mar 11, 2006 10:42 am
by Rescator
However, a better way would simply to change current Macro code,
so that newlines are treated literaly, AND only the last line in a macro
is actualy placed "in-line" (last line in a macro would automatically act as a MacroReturn.
The behaviour would be the same as above.
Personaly I would prefer a MacroReturn though.

Posted: Sat Mar 11, 2006 10:48 am
by Rescator
Or (yeah brainstorming here).

Add another macro feature.

MacroInsert
EndMacroInsert

Where everything except the last (nonblank) line in the macro
is inserted before the line the macro is actually used at.

Posted: Sat Mar 11, 2006 2:14 pm
by freak
I quess what you are looking for are some sort of inlined procedures.
Macros are a simple search & replace.. a "MacroReturn" makes no sense in that context.

Posted: Sat Mar 11, 2006 2:24 pm
by PB
> Macros are a simple search & replace

Yeah... I get the feeling a lot of people want them to act like Procedures.
That's not what they're intended for. Returning a value isn't the concept.

Posted: Sun Mar 12, 2006 8:03 am
by Rescator
Yeah I know, it's just that there has been a lot of times that I needed to "wrap" a api call or two.
And I know that using a Macro is way faster than making a wrapper procedure,
esp. if the call is in a loop etc.

It was just a suggestion, Fred doesn't have to do it.
Heck he might even have a better idea lurking for PB 4.1 :P


It's all about speed here, I still use procedure for large repeating code,
and code where a return value is needed.
And macros for one liner code that is used often.

Basicaly 3 lines of macroed code roughly equals a procedure with 1 line of code in it speedwise.

However, with some extra work I am able to return values from a macro.
I just can't use the macro inline and return the value, that's all.

And please note, the macro examples above do not truly return any value at all.
For the compiler (implementation wise) it's just a matter of replacing the text before MacroReturn before the line the macro is inlined on, and that's the whole magic :P

Btw! Is there any languages that has macros that can be used inline at all?
It's not that I don't love the new macros, it's just that in some cases it's a headache as the macro is added inplace so that when you do

Debug mymacro()

you get:

Debug While
;some code here
Wend
myresult

rather than the wanted:

While
;some code here
Wend
Debug myresult

if you know what I mean...
Obviously this can be "faked" by doing:

mymacro()
Debug myresult

It's just not as convenient :P

Posted: Sun Mar 12, 2006 1:02 pm
by Nik
Macros are no Inline functios, that's all they are simply not made for this task as it is nonsence, they are as Freak said an incode find & replace feature

Posted: Thu Apr 17, 2008 4:00 pm
by Psychophanta
Related also with:
http://www.purebasic.fr/english/viewtopic.php?t=30564

I understand the standard macro conception but I add a vote for this request (or inlined procedures).
And what about something like this?

Code: Select all

Macro macr
  ForEach v()
    If v()\va>10
      ;do something...
      ; ...
      MacroBreak; just like MacroReturn but without value
    EndIf
  Next
  ; do other things
  ; ...
  ; ...
EndMacro
Looks like it does not fit also in the standard macro conception, but at the point of view of the programmer it is effectively just a break and GOTO to the End of the macro definition. So in some way it fits in the macro conception !?