Page 1 of 1
Procedure default argument bypass?
Posted: Mon Mar 12, 2012 3:20 pm
by charvista
Not sure it can in any way, but I was wondering if we can jump an argument value to keep the "default" value?
Code: Select all
Procedure Ax(S.i,D.i=5,E.i=6)
Debug S
Debug D
Debug E
EndProcedure
Ax(100)
Ax(100,#PB_Default,4)
#PB_Default is NOT the correct solution, but that way you can understand my idea. I would like to keep the default for D.i=5 while changing the argument E.i to 4.
Is there a legal way?
Thanks
Re: Procedure default argument bypass?
Posted: Mon Mar 12, 2012 3:36 pm
by Shield
You have to create another procedure without the parameter D and then call the existing one with the correct value
since PB doesn't support function overloading or named parameters.
Re: Procedure default argument bypass?
Posted: Mon Mar 12, 2012 3:48 pm
by Danilo
charvista wrote:Code: Select all
Procedure Ax(S.i,D.i=5,E.i=6)
Debug S
Debug D
Debug E
EndProcedure
Ax(100)
Ax(100,#PB_Default,4)
#PB_Default is NOT the correct solution, but that way you can understand my idea.
If you don't need -1 (#PB_Default), you can use it like your example.
Code: Select all
Procedure Ax(S.i,D.i=5,E.i=6)
If D = #PB_Default : D = 5 : EndIf
Debug S
Debug D
Debug E
EndProcedure
Ax(100)
Ax(100,#PB_Default,4)
I've seen a BASIC language that does allow empty expressions for procedure args, so the default is taken.
Like "Ax(100,,4)" - maybe a feature request?
Re: Procedure default argument bypass?
Posted: Mon Mar 12, 2012 4:39 pm
by charvista
Nice idea as workaround Danilo, thank you! So far the best one, but not very practical, as -1 (#PB_Default) is not an
universal way to tell the called procedure that I want to keep the default value, because -1 can be a value, too.
Ax(100,,4)
Yes, this is exactly what I had in thoughts.
Feature request +1
If PureBasic need to "invent" something else to avoid copying existing syntaxes, then
Ax(100,*,4)
is very nice too.
Re: Procedure default argument bypass?
Posted: Mon Mar 12, 2012 4:47 pm
by charvista
@Shield
Do you mean like this:
Code: Select all
Procedure Ax(S.i,D.i=5,E.i=6)
Debug S
Debug D
Debug E
EndProcedure
Procedure Ax2(S.i,E.i=6)
Ax(S,5,E)
EndProcedure
Ax2(100)
Ax2(100,4)
Not a bad idea either...

Re: Procedure default argument bypass?
Posted: Mon Mar 12, 2012 6:40 pm
by Shield
Yes, exactly.
But it would be way cooler if you could create procedures with the same name and with the compiler
deciding what function to call based on the number (and type) of the parameters.

Re: Procedure default argument bypass?
Posted: Mon Mar 12, 2012 7:38 pm
by STARGĂ…TE
But in PB you can't change the type of a variable, so you know the type of your variables, so you know which procedure you must call.
Re: Procedure default argument bypass?
Posted: Mon Mar 12, 2012 8:16 pm
by Tenaja
This is a super simple solution.
Code: Select all
#Ax_p1_default = 5
Procedure Ax(S.i,D.i=#Ax_p1_default,E.i=6)
Debug S
Debug D
Debug E
EndProcedure
Ax(100,#Ax_p1_default, 4)
If you can't bother typing in whatever name you give #Ax_p1_default, then make a macro.
Code: Select all
Macro Ax2(p0, p2)
Ax(p0,#Ax_p1_default, p2)
endmacro