Variable arguments for procedures

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Variable arguments for procedures

Post by Mistrel »

I do enjoy the limited polymorphism we get with optional parameters but I would also love to see variable arguments as well:

Code: Select all

Procedure SomeFunction(a.l, ...)
  For i=1 To CountVariableArguments()
    ;/ #PB_Integer, #PB_Float, etc.
    Debug VariableArgumentType(i)
  Next i
  
  ;/ 1
  Debug VariableArgumentI(1)
  
  ;/ 2.5
  Debug VariableArgumentF(2)
EndProcedure

SomeFunction(0, 1, 2.5)
User avatar
STARGÅTE
Addict
Addict
Posts: 2227
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: Variable arguments for procedures

Post by STARGÅTE »

How should this work?
The compiler didn't know, the passed argument type:
SomeFunction(0, 1, 2.5)
Integer? Quad? Float? Double?

For the procedure call stack it is important, to know the types. But if you have not defined it?
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
langinagel
Enthusiast
Enthusiast
Posts: 131
Joined: Fri Jan 28, 2005 11:53 pm
Location: Germany
Contact:

Re: Variable arguments for procedures

Post by langinagel »

-1

Everyone is free to request his wish of concept for Purebasic.
My concept goes more in terms of "strictly typing required" for reducing systematic errors in the code.
Sorry Mistrel...

Greetings
LN
https://www.doerpsoft.org

Boost. Work. Efficiency.
User avatar
Shield
Addict
Addict
Posts: 1021
Joined: Fri Jan 21, 2011 8:25 am
Location: 'stralia!
Contact:

Re: Variable arguments for procedures

Post by Shield »

The only way I see this working with PB's limited type system is
only allowing parameters of the same type. We really do not
want C's varargs madness.

Code: Select all

Procedure.i TestProc(a.s, ... params.i(1))
	
	Debug a
	For i = 0 To ArraySize(params())
		Debug params(i)
	Next
	
	
EndProcedure

TestProc("rofl", 10, 20, 30, 40)
I'd +1 this.
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
HanPBF
Enthusiast
Enthusiast
Posts: 570
Joined: Fri Feb 19, 2010 3:42 am

Re: Variable arguments for procedures

Post by HanPBF »

I'd like to see named arguments:

Code: Select all

theProc(P3="Test", P2=1, P1=#True)
This can be completely done by compiler; runtime parameter stack won't change.


And "with"-statement on one line:

Code: Select all

with Arg.SPersonUpdateArg: \Name="Doe": \FirstName="John": \DateOfBirth="01.01.1990": endWith
ThePerson.update(Arg)
Here, there is a chance to manage repeated or similar signatures.
User avatar
idle
Always Here
Always Here
Posts: 5840
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Variable arguments for procedures

Post by idle »

something like this

Code: Select all

Structure PBAny 
  type.b
  StructureUnion 
    a.a
    b.b 
    c.c
    u.u
    w.w
    l.l
    i.i
    f.f
    q.q
    d.d 
    *ptr 
  EndStructureUnion
  s.s
EndStructure   

Structure PBAnyArray
  count.l
  any.PBAny[0]
EndStructure 

Procedure MyFunction(*Params.PBAnyArray)
 Protected a 
  
 For a = 0 To *Params\count -1    
  Select *Params\any[a]\type 
    Case #PB_Word 
      Debug *Params\any[a]\w 
      
    Case #PB_Quad 
      Debug *Params\any[a]\q 
      
    Case #PB_Double 
      Debug *Params\any[a]\d 
      
    Case #PB_String 
      Debug *Params\any[a]\s 

  EndSelect
  Next 
EndProcedure

Procedure FreePBAnyArray(*ar.PBAnyArray) 
  Protected a 
  For a = 0 To *ar\count-1 
    ClearStructure(*ar\any[a],PBAny) 
  Next 
 FreeMemory(*ar)
EndProcedure

Macro NewPBAny(ptr,size) 
 ptr = AllocateMemory(SizeOf(PBAnyArray)+(SizeOf(PBAny)*size))
 ptr\count = size  
EndMacro   

Define *params.PBAnyArray 

NewPBAny(*params,4) 

*params\any[0]\type = #PB_Word 
*params\any[0]\w = 12345 

*params\any[1]\type = #PB_Quad 
*params\any[1]\q =  123456789 

*params\any[2]\type = #PB_Double 
*params\any[2]\d =  2 * #PI 

*params\any[3]\type = #PB_String  
*params\any[3]\s = "PureBasic V5.51 Beta 1 x64"

MyFunction(*params)

FreePBAnyArray(*params)

Windows 11, Manjaro, Raspberry Pi OS
Image
User avatar
the.weavster
Addict
Addict
Posts: 1576
Joined: Thu Jul 03, 2003 6:53 pm
Location: England

Re: Variable arguments for procedures

Post by the.weavster »

Mistrel wrote:I would also love to see variable arguments as well
HanPBF wrote:I'd like to see named arguments
How about simply passing the handle of a JSON object?
JSON can contain a variable number of named arguments of whatever type.
HanPBF
Enthusiast
Enthusiast
Posts: 570
Joined: Fri Feb 19, 2010 3:42 am

Re: Variable arguments for procedures

Post by HanPBF »

JSON is not typed and far slower.

@idle: thanks for the code! Now put this into the PureBasic compiler;-) No, really, if you need variant variables this is the way to do it:-)

These proposals are so called "workarounds"; the less powerful syntax of PureBasic compared to other languages still remains.
Nevertheless, a source for endless discussions...

A precompiler is also a workaround but has no great debugger as PureBasic (unless also implemented).
User avatar
the.weavster
Addict
Addict
Posts: 1576
Joined: Thu Jul 03, 2003 6:53 pm
Location: England

Re: Variable arguments for procedures

Post by the.weavster »

HanPBF wrote:JSON is not typed
Then state a type:

Code: Select all

{ 'params': [
    {
      'name': 'age',
      'value': 21
      'type': 0 // whatever the PB constant value is for your required type
    },
    ...
  ]
}
HanPBF wrote:and far slower.
If the time difference is measured in milliseconds human users wont notice.
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Re: Variable arguments for procedures

Post by Mistrel »

HanPBF wrote:A precompiler is also a workaround but has no great debugger as PureBasic (unless also implemented).
I had thoughts about solving this with a precompiler as well but tried to solve this without one. I wouldn't mind a precompiler though if it awarded us a form of templates. :)
Post Reply