Page 1 of 1

Variable arguments for procedures

Posted: Fri Jan 06, 2017 3:05 am
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)

Re: Variable arguments for procedures

Posted: Fri Jan 06, 2017 8:02 am
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?

Re: Variable arguments for procedures

Posted: Fri Jan 06, 2017 3:29 pm
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

Re: Variable arguments for procedures

Posted: Fri Jan 06, 2017 5:12 pm
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.

Re: Variable arguments for procedures

Posted: Fri Jan 06, 2017 6:19 pm
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.

Re: Variable arguments for procedures

Posted: Sat Jan 07, 2017 4:58 am
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)


Re: Variable arguments for procedures

Posted: Sun Jan 08, 2017 1:31 pm
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.

Re: Variable arguments for procedures

Posted: Sun Jan 08, 2017 3:52 pm
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).

Re: Variable arguments for procedures

Posted: Sun Jan 08, 2017 4:44 pm
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.

Re: Variable arguments for procedures

Posted: Sun Jan 08, 2017 4:53 pm
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. :)