Procedure default argument bypass?

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

Procedure default argument bypass?

Post 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
- Windows 11 Home 64-bit
- PureBasic 6.10 LTS (x64)
- 64 Gb RAM
- 13th Gen Intel(R) Core(TM) i9-13900K 3.00 GHz
- 5K monitor with DPI @ 200%
User avatar
Shield
Addict
Addict
Posts: 1021
Joined: Fri Jan 21, 2011 8:25 am
Location: 'stralia!
Contact:

Re: Procedure default argument bypass?

Post 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.
Image
Blog: Why Does It Suck? (http://whydoesitsuck.com/)
"You can disagree with me as much as you want, but during this talk, by definition, anybody who disagrees is stupid and ugly."
- Linus Torvalds
User avatar
Danilo
Addict
Addict
Posts: 3036
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Re: Procedure default argument bypass?

Post 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?
User avatar
charvista
Addict
Addict
Posts: 949
Joined: Tue Sep 23, 2008 11:38 pm
Location: Belgium

Re: Procedure default argument bypass?

Post 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.
- Windows 11 Home 64-bit
- PureBasic 6.10 LTS (x64)
- 64 Gb RAM
- 13th Gen Intel(R) Core(TM) i9-13900K 3.00 GHz
- 5K monitor with DPI @ 200%
User avatar
charvista
Addict
Addict
Posts: 949
Joined: Tue Sep 23, 2008 11:38 pm
Location: Belgium

Re: Procedure default argument bypass?

Post 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... :)
- Windows 11 Home 64-bit
- PureBasic 6.10 LTS (x64)
- 64 Gb RAM
- 13th Gen Intel(R) Core(TM) i9-13900K 3.00 GHz
- 5K monitor with DPI @ 200%
User avatar
Shield
Addict
Addict
Posts: 1021
Joined: Fri Jan 21, 2011 8:25 am
Location: 'stralia!
Contact:

Re: Procedure default argument bypass?

Post 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. :)
Image
Blog: Why Does It Suck? (http://whydoesitsuck.com/)
"You can disagree with me as much as you want, but during this talk, by definition, anybody who disagrees is stupid and ugly."
- Linus Torvalds
User avatar
STARGÅTE
Addict
Addict
Posts: 2226
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: Procedure default argument bypass?

Post 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.
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
User avatar
Tenaja
Addict
Addict
Posts: 1959
Joined: Tue Nov 09, 2010 10:15 pm

Re: Procedure default argument bypass?

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