Why macros don't accept parameter types?

Everything else that doesn't fall into one of the other PB categories.
Justin
Addict
Addict
Posts: 948
Joined: Sat Apr 26, 2003 2:49 pm

Why macros don't accept parameter types?

Post by Justin »

Code: Select all

Macro test(a.l)
	a+1
EndMacro
is a bit confusing, specially if you pass structures
thefool
Always Here
Always Here
Posts: 5875
Joined: Sat Aug 30, 2003 5:58 pm
Location: Denmark

Post 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 ;)
Dare2
Moderator
Moderator
Posts: 3321
Joined: Sat Dec 27, 2003 3:55 am
Location: Great Southern Land

Post 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.
@}--`--,-- A rose by any other name ..
Justin
Addict
Addict
Posts: 948
Joined: Sat Apr 26, 2003 2:49 pm

Post 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.
Fred
Administrator
Administrator
Posts: 18254
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Post 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.
venom
User
User
Posts: 56
Joined: Fri Jul 25, 2003 1:54 pm
Location: Australia

Post by venom »

The only thing I can think of thats closest to this are inline functions, which PureBasic doesn't have.
Justin
Addict
Addict
Posts: 948
Joined: Sat Apr 26, 2003 2:49 pm

Post 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
Post Reply