MacroReturn

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
Rescator
Addict
Addict
Posts: 1769
Joined: Sat Feb 19, 2005 5:05 pm
Location: Norway

MacroReturn

Post 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 :)
User avatar
Rescator
Addict
Addict
Posts: 1769
Joined: Sat Feb 19, 2005 5:05 pm
Location: Norway

Post 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.
User avatar
Rescator
Addict
Addict
Posts: 1769
Joined: Sat Feb 19, 2005 5:05 pm
Location: Norway

Post 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.
freak
PureBasic Team
PureBasic Team
Posts: 5940
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Post 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.
quidquid Latine dictum sit altum videtur
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post 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.
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
User avatar
Rescator
Addict
Addict
Posts: 1769
Joined: Sat Feb 19, 2005 5:05 pm
Location: Norway

Post 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
Nik
Addict
Addict
Posts: 1017
Joined: Fri May 13, 2005 11:45 pm
Location: Germany
Contact:

Post 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
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Post 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 !?
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
Post Reply