Page 1 of 1

Why macros don't accept parameter types?

Posted: Sun Feb 05, 2006 5:02 pm
by Justin

Code: Select all

Macro test(a.l)
	a+1
EndMacro
is a bit confusing, specially if you pass structures

Posted: Sun Feb 05, 2006 5:04 pm
by thefool
Well since the macro basically just puts the text you write in the code, it would be weird to let it have different types. This is one of the great things about macro's ;)

Posted: Sun Feb 05, 2006 5:15 pm
by Dare2
Hi Justin,

My take:

Macros are not procedures/functions/etc. They are code blocks that PureBasic "copies" and "pastes" into place when it encounters the macro name elsewhere in the code.

The "parameters" in the macros are placemarkers that are further replaced by the parameters encountered where the macro is used.

Everything is text.

So:

Code: Select all

Macro AddOne(a)
   a+1
EndMacro
Is really saying to PureBasic: When you see "AddOne" in the code, then:
  • Replace it with "a + 1"
Further, where you see "a" in the macro,
  • Replace it with the "parameter" value supplied by the macro placemarker.
Manually done, we would copy some code, paste it elsewhere, change a few bits.

So in code:

Any "AddOne(z)" becomes "z+1". It is like an automatic copy/paste with bells on. It saves retyping repetitive code, and gives the macro name a temporary importance or keyword status in the code containing the macro. But it is not a procedure in and of itself.

Another Eg:

Code: Select all

Macro AddToList(myList)
  AddElement(myList())
EndMacro
...
AddToList(things)
becomes

Code: Select all

AddElement(things())
And then PureBasic compiles it.

Posted: Sun Feb 05, 2006 11:35 pm
by Justin
I thought they were like C macros(HIWORD(), MAKELONG()...), all the SendMessage() macro/wrappers, or to wrap get/put properties in objects. without types doing it will be very weird.

Posted: Sun Feb 05, 2006 11:40 pm
by Fred
Look carefully, in C you don't specify the type as well:

Code: Select all

#define ListView_GetItem(h,p) SendMessage(h,LVM_GETITEM,0,(LPARAM)(p))
With parameter type it would look like:

Code: Select all

#define ListView_GetItem(HWND h, LPARAM p) SendMessage(h,LVM_GETITEM,0,(LPARAM)(p))
Which is not the case.

Posted: Mon Feb 06, 2006 10:00 am
by venom
The only thing I can think of thats closest to this are inline functions, which PureBasic doesn't have.

Posted: Mon Feb 06, 2006 11:40 am
by Justin
yes there are no types in C either, although they are later cast inside the macro so you have some clue. i was looking at the wrong place

i'll use a commented syntax at the macro beginning