Procedures inside Structures

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User_Russian
Addict
Addict
Posts: 1443
Joined: Wed Nov 12, 2008 5:01 pm
Location: Russia

Re: Procedures inside Structures

Post by User_Russian »

It is a pity that it has not yet been added to PB.
User avatar
jacdelad
Addict
Addict
Posts: 1477
Joined: Wed Feb 03, 2021 12:46 pm
Location: Planet Riesa
Contact:

Re: Procedures inside Structures

Post by jacdelad »

You could add the address of a function to a structure and call it, but I guess you already know this and you want to call it directly, like

Code: Select all

MyVar\MyFunc()
?
PureBasic 6.04/XProfan X4a/Embarcadero RAD Studio 11/Perl 5.2/Python 3.10
Windows 11/Ryzen 5800X/32GB RAM/Radeon 7770 OC/3TB SSD/11TB HDD
Synology DS1821+/36GB RAM/130TB
Synology DS920+/20GB RAM/54TB
Synology DS916+ii/8GB RAM/12TB
User_Russian
Addict
Addict
Posts: 1443
Joined: Wed Nov 12, 2008 5:01 pm
Location: Russia

Re: Procedures inside Structures

Post by User_Russian »

Is it a "dirty" method. It is easy to make a mistake by specifying the address of another procedure.
In addition, the procedure does not have native access to the fields of the structure.
The procedure must be inline in the structure and there must be no name collision.

Code: Select all

Structure Test
  
  x.l
  s.s
  Mutex.i
  
  Procedure.s Proc(p1, p2) ; Regular procedure in a structure.
    x = p1 * p2
    ProcedureReturn s
  EndProcedure
  
  Procedure Test()       ; Called automatically when a structure is created (constructor).
    x = 10
    s = "1234"
    Mutex = CreateMutex()
  EndProcedure
  
  Procedure ~Test()     ; Called automatically when the structure is freed (destructor).
    x = 0
    s = ""
    If Mutex
      FreeMutex(Mutex)
    EndIf
  EndProcedure
  
EndStructure



Procedure Proc() ; There is no name collision with a procedure in a structure.
  x.Test
  
  Debug x\Proc(1, 2) ; The result is "1234".
EndProcedure

Proc()
User avatar
jacdelad
Addict
Addict
Posts: 1477
Joined: Wed Feb 03, 2021 12:46 pm
Location: Planet Riesa
Contact:

Re: Procedures inside Structures

Post by jacdelad »

I just saw that this is not a new thread and read the first posts. Why not use the prototype variant? Because the procedure itself is declared outside?
PureBasic 6.04/XProfan X4a/Embarcadero RAD Studio 11/Perl 5.2/Python 3.10
Windows 11/Ryzen 5800X/32GB RAM/Radeon 7770 OC/3TB SSD/11TB HDD
Synology DS1821+/36GB RAM/130TB
Synology DS920+/20GB RAM/54TB
Synology DS916+ii/8GB RAM/12TB
Fred
Administrator
Administrator
Posts: 16686
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: Procedures inside Structures

Post by Fred »

User_Russian wrote: Sat Jun 10, 2023 3:10 pm It is a pity that it has not yet been added to PB.
May be you should watch your wording because it looks very aggressive to me.
Rinzwind
Enthusiast
Enthusiast
Posts: 638
Joined: Wed Mar 11, 2009 4:06 pm
Location: NL

Re: Procedures inside Structures

Post by Rinzwind »

I read it as a sign of personal disappointment and disillusion.
Still tiny hope left ;

The language can use a bit more polish her and there. Still useful the way it is (otherwise I wouldn't be here, same as mr Russian I guess), just a less than optimal dev experience in certain cases. It is KISS, but doing that makes its use complex or code bloated in certain scenarios.

Btw this feature would make custom gadgets also more manageable.
User_Russian
Addict
Addict
Posts: 1443
Joined: Wed Nov 12, 2008 5:01 pm
Location: Russia

Re: Procedures inside Structures

Post by User_Russian »

Fred wrote: Sat Jun 10, 2023 4:32 pm
User_Russian wrote: Sat Jun 10, 2023 3:10 pm It is a pity that it has not yet been added to PB.
May be you should watch your wording because it looks very aggressive to me.
Sorry, I use google-translator and this is probably an imperfect translation into english.
I didn't mean anything bad.
User avatar
skywalk
Addict
Addict
Posts: 3999
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Procedures inside Structures

Post by skywalk »

I use prototypes-in-structure for simple objects. I don't think it supports nested or structure unions.
I would not put this high on my What's Next PB list.
+ Fast strings
+ Static lib creation
+ Import C libs
+ Unsigned types (to support C libs)
+...
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
Post Reply