It is currently Sat May 25, 2013 10:08 am

All times are UTC + 1 hour




Post new topic Reply to topic  [ 29 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: Procedures inside Structures
PostPosted: Tue Jan 25, 2011 9:05 am 
Offline
User
User

Joined: Tue May 05, 2009 5:20 am
Posts: 28
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:
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


Top
 Profile  
 
 Post subject: Re: Procedures inside Structures
PostPosted: Tue Jan 25, 2011 9:43 am 
Offline
Addict
Addict
User avatar

Joined: Thu Jun 24, 2004 2:44 pm
Posts: 4715
Location: Berlin - Germany
PureBasic-Version is a bit larger :wink:
Only as hint:
Code:
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.11 | Windows 7 SP1 (x64) | Mageia 3 (x64) | RealSource

The use of EnableExplicit is free of charge and avoids errors.


Top
 Profile  
 
 Post subject: Re: Procedures inside Structures
PostPosted: Tue Jan 25, 2011 10:02 am 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Sun Oct 31, 2004 10:54 am
Posts: 511
I prefer the syntax suggested by Aonym
very good idea Aonym :)


Aonym 15 lines ( 16 with debug line )
ts-soft 22 lines .. hum !

_________________
Image
************************************
site : http://michel.dobro.free.fr/
Videos : http://www.youtube.com/user/DobroMG
************************************


Top
 Profile  
 
 Post subject: Re: Procedures inside Structures
PostPosted: Tue Jan 25, 2011 10:11 am 
Offline
User
User

Joined: Tue May 05, 2009 5:20 am
Posts: 28
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


Top
 Profile  
 
 Post subject: Re: Procedures inside Structures
PostPosted: Tue Jan 25, 2011 10:14 am 
Offline
User
User

Joined: Tue May 05, 2009 5:20 am
Posts: 28
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


Top
 Profile  
 
 Post subject: Re: Procedures inside Structures
PostPosted: Tue Jan 25, 2011 10:59 am 
Offline
Addict
Addict
User avatar

Joined: Wed Dec 22, 2004 4:12 pm
Posts: 2328
Location: Norway
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!

_________________
Repeat
  sleep()
  eat()
  work()
Until death Or suicide


Top
 Profile  
 
 Post subject: Re: Procedures inside Structures
PostPosted: Tue Jan 25, 2011 3:47 pm 
Offline
Enthusiast
Enthusiast

Joined: Sun Sep 17, 2006 1:24 pm
Posts: 389
Location: Germany
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.


Top
 Profile  
 
 Post subject: Re: Procedures inside Structures
PostPosted: Tue Jan 25, 2011 11:48 pm 
Offline
User
User

Joined: Tue May 05, 2009 5:20 am
Posts: 28
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


Top
 Profile  
 
 Post subject: Re: Procedures inside Structures
PostPosted: Wed Jan 26, 2011 1:24 am 
Offline
Addict
Addict
User avatar

Joined: Wed Dec 23, 2009 10:14 pm
Posts: 1387
Location: Boston, MA
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:
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)

_________________
To understand recursion, you must first understand recursion. ~ unknown
I never make stupid mistakes. Only very, very clever ones. ~ John Peel


Top
 Profile  
 
 Post subject: Re: Procedures inside Structures
PostPosted: Fri Feb 25, 2011 3:19 pm 
Offline
Enthusiast
Enthusiast

Joined: Thu May 21, 2009 6:56 pm
Posts: 249
I like the idea also.

Simon


Top
 Profile  
 
 Post subject: Re: Procedures inside Structures
PostPosted: Fri Feb 25, 2011 4:20 pm 
Offline
Enthusiast
Enthusiast

Joined: Mon Jun 09, 2003 10:08 pm
Posts: 635
Location: Nottingham
I also like Aonym's syntax but I think the variation below is neater.
Code:
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


Top
 Profile  
 
 Post subject: Re: Procedures inside Structures
PostPosted: Fri Feb 25, 2011 4:53 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Fri Jun 26, 2009 3:51 pm
Posts: 194
Location: Westernmost tip of Norway
akj wrote:
I also like Aonym's syntax but I think the variation below is neater.
Code:
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?

_________________
You never learn anything by doing it right.


Top
 Profile  
 
 Post subject: Re: Procedures inside Structures
PostPosted: Fri Feb 25, 2011 5:09 pm 
Offline
Addict
Addict
User avatar

Joined: Wed Dec 23, 2009 10:14 pm
Posts: 1387
Location: Boston, MA
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.

_________________
To understand recursion, you must first understand recursion. ~ unknown
I never make stupid mistakes. Only very, very clever ones. ~ John Peel


Top
 Profile  
 
 Post subject: Re: Procedures inside Structures
PostPosted: Fri Feb 25, 2011 10:31 pm 
Offline
Enthusiast
Enthusiast

Joined: Sun Sep 17, 2006 1:24 pm
Posts: 389
Location: Germany
@[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.

_________________
You do not need to hurry, when going in the wrong direction.


Top
 Profile  
 
 Post subject: Re: Procedures inside Structures
PostPosted: Wed Mar 02, 2011 11:44 am 
Offline
User
User

Joined: Wed Feb 04, 2009 8:11 am
Posts: 42
Location: Armenia
It is different.

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


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 29 posts ]  Go to page 1, 2  Next

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye