Page 1 of 1

a\\func(...) as shortcut for a\func(a,...)

Posted: Mon Mar 20, 2017 8:38 pm
by GPI
See topic, nothing to add :)

Re: a\\func(...) as shortcut for a\func(a,...)

Posted: Tue Mar 21, 2017 4:54 pm
by STARGÅTE
This method is already implemented as Interfaces:

Code: Select all

Interface Test
	func.i(b.i, c.i)
EndInterface

Structure TestS
	*Functions
	Value.i
EndStructure

Procedure func(*a.TestS, b.i, c.i)
	
	ProcedureReturn *a\Value*b+c
	
EndProcedure

Procedure New(Value.i)
	
	Protected *Element.TestS = AllocateStructure(TestS)
	
	*Element\Functions = ?Functions
	*Element\Value= Value
	
	ProcedureReturn *Element
	
	DataSection
		Functions:
		Data.i @func()
	EndDataSection
	
EndProcedure

Define a.Test = New(1)
Define a2.Test = New(2)
Define b.i = 4
Define c.i = 6

Debug a\func(b, c)
Debug a2\func(1, 0)

Re: a\\func(...) as shortcut for a\func(a,...)

Posted: Tue Mar 21, 2017 5:27 pm
by GPI
I know, but i think, something like this is more ellgant, easier to understand:

Code: Select all

Prototype _GetCounter(*self)
Prototype _SetCounter(*self,value)
Prototype _AddCounter(*self,value=1)

Structure Counter
  Get._GetCounter
  Set._SetCounter
  Add._AddCounter
  _value.i
EndStructure

Procedure counter_Get(*self.counter)
  ProcedureReturn *self\_value
EndProcedure
Procedure counter_Set(*self.counter,value)
  *self\_value=value
  ProcedureReturn *self\_value
EndProcedure  
Procedure counter_Add(*self.counter,value=1)
  *self\_value+value
  ProcedureReturn *self\_value
EndProcedure  
  
Procedure New_counter(value=0)
  Protected *self.counter=AllocateStructure(counter)
  *self\Get=@counter_Get()
  *self\Set=@counter_Set()
  *self\Add=@counter_Add()
  *self\_value=value
  ProcedureReturn *self
EndProcedure

Macro DisposeObject(a)
  FreeStructure(a)
EndMacro

*x.counter=New_counter()
Debug *x\Get(*x)
*x\Set(*x,10)
Debug *x\get(*x)
Debug *x\Add(*x)

DisposeObject(*x)
it would be nice to write here simple:

Code: Select all

Debug *x\\Get()
*x\\Set(10)
Debug *x\\get()
Debug *x\\Add()
And by the way - when the structre could handle automatic prototype like this:

Code: Select all

Structure Counter
  Get.i (*self.counter)
  Set.i (*self.counter,value)
  Add.i (*self.counter,value=1)
  _value.i
EndStructure
it would be very nice. Maybe it would be nice to have default-values for structures.... Then it would be perfect, like

Code: Select all

Structure Counter
  Get.i (*self.counter) = @counter_get()
  Set.i (*self.counter,value) = @counter_set()
  Add.i (*self.counter,value=1) = @counter_add()
  _value.i = 1
EndStructure
In this case, you don't need a "new" anymore.

Code: Select all

x.counter
debug x\\Get()
x\\Set(10)
Debug x\\get()
Debug x\\Add()

Re: a\\func(...) as shortcut for a\func(a,...)

Posted: Wed Mar 22, 2017 9:58 am
by STARGÅTE
For your first part:

Code: Select all

Interface Counter
	Get()
	Set(value)
	Add(value=1)
EndInterface

Structure CounterS
	*Functions
	Value.i
EndStructure

Procedure counter_Get(*self.CounterS)
	ProcedureReturn *self\Value
EndProcedure
Procedure counter_Set(*self.CounterS,value)
	*self\Value=value
	ProcedureReturn *self\Value
EndProcedure  
Procedure counter_Add(*self.CounterS,value=1)
	*self\Value+value
	ProcedureReturn *self\Value
EndProcedure  

Procedure New_counter(value=0)
	Protected *self.CounterS=AllocateStructure(CounterS)
	*self\Functions = ?Functions
	DataSection
		Functions:
		Data.i @counter_Get(), @counter_Set(), @counter_Add()
	EndDataSection
	ProcedureReturn *self
EndProcedure

Macro DisposeObject(a)
	FreeStructure(a)
EndMacro

*x.counter=New_counter()
Debug *x\Get()
*x\Set(10)
Debug *x\Get()
Debug *x\Add()

DisposeObject(*x)
For your second part:
Yes it whould be nice to initialize a Interface by a simple definition, without a "new" but often the "new" function do more than a simple setting of values.

Edit:
I use prototypes in structures also very often, and I like it, but I think Interfaces are the native way in PB to use methodes for objects and the use of prototypes in structures is a bonus.

Re: a\\func(...) as shortcut for a\func(a,...)

Posted: Wed Mar 22, 2017 3:15 pm
by GPI
I personal think, that Objects with Interaface (and structures) are not an easy to handle option. You must create first a Interface, then a structure, "connect" them, fill the vtable and so on. When you extend the object, you must be carefull, that you insert the new function in the interface and vtable in the correct position. In my opinion this is not a "BASIC"-Solution.

PureBasic support objects, sadly it doesn't have a "Basic-Style" method to create one. Something is missing here.

Re: a\\func(...) as shortcut for a\func(a,...)

Posted: Wed Mar 22, 2017 8:32 pm
by HanPBF
+1

The interface solution is o.k. but You need a pre-compiler to make it work seamless.
I tried to do a project with it natively but You end up typing 24/7;-)
Too much overhead.