Comparison as argument

Just starting out? Need help? Post your questions and find answers here.
User avatar
charvista
Addict
Addict
Posts: 969
Joined: Tue Sep 23, 2008 11:38 pm
Location: Belgium

Comparison as argument

Post by charvista »

In the blog of Purebasic, I found this interesting code:

Code: Select all

Integer = 0.6 ; rounded to 1
Check(Integer * 2 = 2) 
I am wondering how the procedure Check could have been written, as comparisons are not allowed here, except to give a default value to the argument.
I am puzzled... :shock:
- Windows 11 Home 64-bit
- PureBasic 6.30 (x64)
- 64 Gb RAM
- 13th Gen Intel(R) Core(TM) i9-13900K 3.00 GHz
- 8K monitor with DPI @ 300%
infratec
Always Here
Always Here
Posts: 7779
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Comparison as argument

Post by infratec »

Code: Select all

Macro Check(a)
  Bool(a)
EndMacro

Integer = 0.6 ; rounded to 1
Debug Check(Integer * 2 = 2)
Debug Check(Integer * 2 = 3)
User avatar
charvista
Addict
Addict
Posts: 969
Joined: Tue Sep 23, 2008 11:38 pm
Location: Belgium

Re: Comparison as argument

Post by charvista »

Oh, you are right! With a Macro.
But then, why the need of a Macro here, as
Debug Bool(Integer * 2 = 2)
Debug Bool(Integer * 2 = 3)
is more straight?

But now that we are speaking about Macro's, what is given in the parentheses is got as is, how to "transform" the value of a variable as the value itself?
This works:

Code: Select all

Define.i a0,a1,a2,a3,a4,a5,a6,a7,a8,a9 
Define.i x

Macro A(x)
    a#x=x
EndMacro

A(7)

Debug a0
Debug a1
Debug a2
Debug a3
Debug a4
Debug a5
Debug a6
Debug a7
Debug a8
Debug a9
but this does not work:

Code: Select all

Define.i a0,a1,a2,a3,a4,a5,a6,a7,a8,a9 
Define.i x

Macro A(x)
    a#x=x
EndMacro

x=7
A(x)

Debug a0
Debug a1
Debug a2
Debug a3
Debug a4
Debug a5
Debug a6
Debug a7
Debug a8
Debug a9
I know why it does not work, but I wonder how to make it working...
- Windows 11 Home 64-bit
- PureBasic 6.30 (x64)
- 64 Gb RAM
- 13th Gen Intel(R) Core(TM) i9-13900K 3.00 GHz
- 8K monitor with DPI @ 300%
Post Reply