Procedures inside Structures

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
aonyn
User
User
Posts: 43
Joined: Tue May 05, 2009 5:20 am

Procedures inside Structures

Post by aonyn »

Hi All,

I have a request for a future update to PureBasic.
I use both PureBasic, and GLBasic, each for different types of projects.

The most recent major update to GLBasic is a feature I have come to like very much.
It now allows Functions inside of Types.
This allows for a (sort of) OOP approach to organizing code, without added complexity.
I find it allows me to make nice, self contained little code objects, which are very suited to re-usability, and still think and write in a procedural way.

Would it be possible to add such a feature to PureBasic.
I know, OOP is entirely possible with Prototypes, Interfaces, Function Pointers, etc.
This proposed method though, while definitely not adhering to all the concepts of OOP, it borrows just enough to be useful, while not becoming cumbersome for amateurs such as myself.

Here is how such an implementation may look (borrowed directly from the new version of GLBasic, but adapted to PureBasic syntax).

Code: Select all

Structure foobar
	; Members
	x.i
	y.i
	
	; Methods
	Procedure.i bar(z.i)
		result.i = (self\x + self\y) * z
		ProcedureReturn result
	EndProcedure
	
	Procedure set(x.i, y.i)
		self\x = x
		self\y = y
	EndProcedure
EndStructure

Dim foo.foobar(1)
foo(0)\set(10, 20)

result.i = foo(0)\bar(30)
I understand that Fred is not interested in adding full OOP support to PureBasic, and if this idea is not acceptable, I of course fully understand.
I would be very pleased though of course if it is deemed a good idea, and implemented. :)

Thanks in advance.
Dave
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: Procedures inside Structures

Post by ts-soft »

PureBasic-Version is a bit larger :wink:
Only as hint:

Code: Select all

Prototype bar(z.i)
Prototype set(x.i, y.i)

Structure foobar
   ; Members
   x.i
   y.i
   ; Methods
   bar.bar
   set.set
EndStructure

Global Dim foo.foobar(1)

Procedure.i bar(z.i)
  result.i = (foo(0)\x + foo(0)\y) * z
  ProcedureReturn result
EndProcedure

Procedure set(x.i, y.i)
  foo(0)\x = x
  foo(0)\y = y
EndProcedure

foo(0)\bar = @bar()
foo(0)\set = @set()

foo(0)\set(10, 20)

result.i = foo(0)\bar(30)
Debug result
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
User avatar
dobro
Enthusiast
Enthusiast
Posts: 766
Joined: Sun Oct 31, 2004 10:54 am
Location: France
Contact:

Re: Procedures inside Structures

Post by dobro »

I prefer the syntax suggested by Aonym
very good idea Aonym :)


Aonym 15 lines ( 16 with debug line )
ts-soft 22 lines .. hum !
Image
Windows 98/7/10 - PB 5.42
■ sites : http://michel.dobro.free.fr/
aonyn
User
User
Posts: 43
Joined: Tue May 05, 2009 5:20 am

Re: Procedures inside Structures

Post by aonyn »

Thank You ts-soft,

I appreciate you time and effort to show me a reasonable solution, and it will certainly be useful to me. :)
I copied and pasted your sample into a PB template, so I can refer to it later and use this.
It is not quite as tidy as the way GLB handles it, but certainly simple enough compared to other solutions I have seen.

I do still stand by the original request, as I do see some disadvantages to the approach from ts-soft.
For example, in GLB, you may have more than one type(structure) which have internal functions with the same name, yet variations to the code within.
For example, foo\bar may do (x*y)+z, and goo\bar may do (x*y)+z^2.
I do understand that this can also be done, by pointing from members with the same name to procedures with different names.
However, this will still result in much more complex code, while by actually writing the procedures within the structures keeps everything contained, and easily portable IMO.

regards,
Dave
aonyn
User
User
Posts: 43
Joined: Tue May 05, 2009 5:20 am

Re: Procedures inside Structures

Post by aonyn »

Thanks dobro,

I am glad to know my request would be appreciated by more users than just myself (well one more so far) :)

regards,
Dave
User avatar
Joakim Christiansen
Addict
Addict
Posts: 2452
Joined: Wed Dec 22, 2004 4:12 pm
Location: Norway
Contact:

Re: Procedures inside Structures

Post by Joakim Christiansen »

aonyn wrote:I am glad to know my request would be appreciated by more users than just myself (well one more so far) :)
I too like your idea! It is clean and simple!
I like logic, hence I dislike humans but love computers.
Marlin
Enthusiast
Enthusiast
Posts: 406
Joined: Sun Sep 17, 2006 1:24 pm
Location: Germany

Re: Procedures inside Structures

Post by Marlin »

I'd vote for that too.

I would like to be able to use such things.

While, to my understanding, it would basically work the same way
as in the current PB way that ts-soft suggested
(thank you ts-soft for giving that example btw.),
I would use aonyn's way if I could.

I would not want to clutter my code with extra prototypes,
extra strangely named procedures and setting references ...
and that even outside of the area,
where those parts really belong too...

This also seems feasible to me.

A nice way to introduce OOP to PB,
without loosing anything. :D

Thank you for that suggestion, aonyn.
aonyn
User
User
Posts: 43
Joined: Tue May 05, 2009 5:20 am

Re: Procedures inside Structures

Post by aonyn »

Hi All,

Well I am glad the suggestion seems to be welcomed.

As I said in my first post, this has been done by Gernot at GLBasic, and it is a feature I have come to like very much.
He like Fred is resistant to taking GLBasic far into the realm of OOP.
This solution I think was a compromise with his users who were asking for OOP, and actually was at first a bit controversial on his forums.

In the end, now that it is implemented though, I get the impression that it is a widely liked and used feature.

regards,
Dave
User avatar
skywalk
Addict
Addict
Posts: 3972
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Procedures inside Structures

Post by skywalk »

Great topic!
Thanks ts-soft!
Your sample code really opened my eyes to some future simplification in this procedural world. :wink:

@aonyn - I also prefer your suggestion.
The prototypes and procedures and "@ assignments" are clumsy and would be neat and tidy in one structure(dare I say Class?) declaration.

Code: Select all

Prototype.s ar(Array sA.s(1), Fill.s="-999")
Prototype bar(z.i)
Prototype set(x.i, y.i)
Define.i ri
Define.s rs

Structure foobar
   ; Members
   x.i
   y.i
   Array fooray.s(1)
   ; Methods
   bar.bar
   set.set
   ar.ar
EndStructure

Global Dim foo.foobar(1)
ReDim foo(0)\fooray(1)

Procedure.s ar(Array sA.s(1), Fill.s="-999")
  ReDim sA(5)
  sA(0) = Fill
  sA(1) = Fill
  sA(2) = Fill
  ProcedureReturn sA(1)
EndProcedure

Procedure.i bar(z.i)
  result.i = (foo(0)\x + foo(0)\y) * z
  ProcedureReturn result
EndProcedure

Procedure set(x.i, y.i)
  foo(0)\x = x
  foo(0)\y = y
EndProcedure

foo(0)\bar = @bar()
foo(0)\set = @set()
foo(0)\ar = @ar()

foo(0)\set(10, 20)

ri = foo(0)\bar(30)
Debug ri
rs = foo(0)\ar(foo(0)\fooray())
Debug rs
Debug foo(0)\fooray(0)
Debug foo(0)\fooray(1)
Debug foo(0)\fooray(2)
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
swhite
Enthusiast
Enthusiast
Posts: 726
Joined: Thu May 21, 2009 6:56 pm

Re: Procedures inside Structures

Post by swhite »

I like the idea also.

Simon
Simon White
dCipher Computing
akj
Enthusiast
Enthusiast
Posts: 665
Joined: Mon Jun 09, 2003 10:08 pm
Location: Nottingham

Re: Procedures inside Structures

Post by akj »

I also like Aonym's syntax but I think the variation below is neater.

Code: Select all

Structure foobar
  ; Members
  x.i
  y.i
  ; Methods
  Declare.i bar(z.i)
  Declare   set(x.i, y.i)
EndStructure

Procedure.i bar(z.i)
  result.i = (self\x + self\y) * z
  ProcedureReturn result
EndProcedure

Procedure set(x.i, y.i)
  self\x = x
  self\y = y
EndProcedure

Dim foo.foobar(1)
foo(0)\set(10, 20)
result.i = foo(0)\bar(30)
Anthony Jordan
User avatar
KJ67
Enthusiast
Enthusiast
Posts: 218
Joined: Fri Jun 26, 2009 3:51 pm
Location: Westernmost tip of Norway

Re: Procedures inside Structures

Post by KJ67 »

akj wrote:I also like Aonym's syntax but I think the variation below is neater.

Code: Select all

Structure foobar
  ; Members
  x.i
  y.i
  ; Methods
  Declare.i bar(z.i)
  Declare   set(x.i, y.i)
EndStructure

Procedure.i bar(z.i)
  result.i = (self\x + self\y) * z
  ProcedureReturn result
EndProcedure

Procedure set(x.i, y.i)
  self\x = x
  self\y = y
EndProcedure

Dim foo.foobar(1)
foo(0)\set(10, 20)
result.i = foo(0)\bar(30)
Nice version, than there is also less risk for huge structures due large procedures inside.
The code better be a easy to read as possible and this is a nice step, but the use of 'Declare' may not really fit perfectly.. maybe a new keyword?
The best preparation for tomorrow is doing your best today.
User avatar
skywalk
Addict
Addict
Posts: 3972
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Procedures inside Structures

Post by skywalk »

KJ67 wrote:The code better be a easy to read as possible and this is a nice step, but the use of 'Declare' may not really fit perfectly.. maybe a new keyword?
...How is that different than the use of Prototypes in the Structure as ts-soft showed :?:
I think keeping stuff between the Structure(or Class or Module) is more readable.
Maybe this can isolate(Private vs Global) the methods from the rest of the code also.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
Marlin
Enthusiast
Enthusiast
Posts: 406
Joined: Sun Sep 17, 2006 1:24 pm
Location: Germany

Re: Procedures inside Structures

Post by Marlin »

@[a]kj[67]
I agree with skywalk.
Why request a feature, that is allready there?

The title says it all: Procedures inside Structures.

Besides, the idea, that the code can be where it belongs,
(you seem to disagree with that)
this is the way, you don't need to worry about naming collisions.

With procedures in global space only,
like you suggested and what has allways been that way anyways,
the naming of them is of course also done completely in the global space.
If liberty means anything at all, it means the right to tell people what they do not want to hear.
- George Orwell
DaylightDreamer
User
User
Posts: 52
Joined: Wed Feb 04, 2009 8:11 am
Location: Armenia

Re: Procedures inside Structures

Post by DaylightDreamer »

It is different.

"Prototypes" is easier way to call libraries.
"Procedures inside a structure" its a partial way to emulate OOP.
Post Reply