Got an idea for enhancing PureBasic? New command(s) you'd like to see?
Mistrel
Addict
Posts: 3415 Joined: Sat Jun 30, 2007 8:04 pm
Post
by Mistrel » Fri Jan 06, 2017 3:05 am
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)
STARGÅTE
Addict
Posts: 2227 Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:
Post
by STARGÅTE » Fri Jan 06, 2017 8:02 am
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?
langinagel
Enthusiast
Posts: 131 Joined: Fri Jan 28, 2005 11:53 pm
Location: Germany
Contact:
Post
by langinagel » Fri Jan 06, 2017 3:29 pm
-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
Shield
Addict
Posts: 1021 Joined: Fri Jan 21, 2011 8:25 am
Location: 'stralia!
Contact:
Post
by Shield » Fri Jan 06, 2017 5:12 pm
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.
HanPBF
Enthusiast
Posts: 570 Joined: Fri Feb 19, 2010 3:42 am
Post
by HanPBF » Fri Jan 06, 2017 6:19 pm
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.
idle
Always Here
Posts: 5840 Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand
Post
by idle » Sat Jan 07, 2017 4:58 am
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
the.weavster
Addict
Posts: 1576 Joined: Thu Jul 03, 2003 6:53 pm
Location: England
Post
by the.weavster » Sun Jan 08, 2017 1:31 pm
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
Posts: 570 Joined: Fri Feb 19, 2010 3:42 am
Post
by HanPBF » Sun Jan 08, 2017 3:52 pm
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).
the.weavster
Addict
Posts: 1576 Joined: Thu Jul 03, 2003 6:53 pm
Location: England
Post
by the.weavster » Sun Jan 08, 2017 4:44 pm
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
Posts: 3415 Joined: Sat Jun 30, 2007 8:04 pm
Post
by Mistrel » Sun Jan 08, 2017 4:53 pm
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.