Classes in PureBasic

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: Classes in PureBasic

Post by ts-soft »

The Concept by Fred is good!

Only one wish: Source-Module and Binary-Module
To make Binary modules around the code before others inaccessible,
around this to selling or simply because one is ashamed of that :mrgreen:
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_Russian
Addict
Addict
Posts: 1443
Joined: Wed Nov 12, 2008 5:01 pm
Location: Russia

Re: Classes in PureBasic

Post by User_Russian »

freak, If we add modules in PB, it is necessary do it properly, rather than pseudo-modules, you have to offer. All constants, structures, procedures need to be isolated from the main program, and then the that suggest, can not be called isolation!
So it is better not to waste time on the modules than to implement them according to your plan. :( :(
freak wrote:Modules in modules:
I don't see a common need for this. I have a hard time coming up with a useful example for it to be honest. And the semantics of such a feature would be hard to define and understand. Once again: we are trying to keep things understandable and not make them unnecessarily complex.
Imagine you have a game engine (for example), divided into several modules. Engine itself is a module, and modules engine - nested modules main module.
Why do it?
For isolation of code. Using modules of the required functions are exported engine, but the program will not be available functions and structures of the inner engine modules.

But on your plan, it does not make sense, because there is no complete isolation of code.
freak wrote:What other feature should we have dropped to get the time for this?
For example, 3D function.
Last edited by User_Russian on Mon Feb 04, 2013 9:18 pm, edited 1 time in total.
PMV
Enthusiast
Enthusiast
Posts: 727
Joined: Sat Feb 24, 2007 3:15 pm
Location: Germany

Re: Classes in PureBasic

Post by PMV »

freak wrote:Its not like we did nothing all these years. Look at the release history. What other feature should we have dropped to get the time for this?
Oh ... if you mean my quiping ... sorry if it was to much. :)
User avatar
fsw
Addict
Addict
Posts: 1572
Joined: Tue Apr 29, 2003 9:18 pm
Location: North by Northwest

Re: Classes in PureBasic

Post by fsw »

Fred wrote:As you seem interested by the module topic, here is our draft, so you can get a better idea. Please keep in mind than it's no promise, we can drop it if we change our mind so don't expect it for the next version.

Code: Select all

      ; Here is my suggestion: ...

Fred, the same stuff can be achieved with less complication, just look at the above post of mine.

We have already IncludeFile and IncludeBinary why not add IncludeModule and make each file a module?
There is no need for Module/EndModule and stuff that over-complicates things.

In Go if there are several procedures with the same name (because several modules are imported) the only thing that needs to be done is:

Code: Select all

Crypto1.Cipher() ; call Cipher from module 1
Crypto2.Cipher() ; call Cipher from module 2
the PureBasic equivalent could look like this:

Code: Select all

Crypto1\\Cipher() ; call Cipher from module 1
Crypto2\\Cipher() ; call Cipher from module 2
Please keep it simple.

Thanks for reading :wink:

I am to provide the public with beneficial shocks.
Alfred Hitshock
User avatar
luis
Addict
Addict
Posts: 3876
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: Classes in PureBasic

Post by luis »

Thanks for the more detailed info. The "External" bit was the piece missing to make it work better for me.
freak wrote:There is no confusion between an X_Foo() procedure that belongs to module X and another X_Foo() procedure that you write elsewhere because you can't. They refer to the same thing.
Scenario: you include the source of module MOD in your main program, the module contains a Foo() procedure inside it (scope defined below).

1) You don't use OpenModule

1A) Foo() in the module is private (default)
You try do define a Foo() proc in the main program. Should be possible.
You call that proc with Foo() and can't access the one inside the module. OK.

1B) Foo() in the module is External
You try do define a Foo() proc in the main program. Should be possible (?)
You call that proc with Foo() and the one inside the module with MOD_Foo(). OK.

2) You use OpenModule MOD

1A) Foo() in the module is private (default)
You try do define a Foo() proc in the main program. Should be possible.
You call that proc with Foo() and can't access the one inside the module. OK.

1B) Foo() in the module is External
You try do define a Foo() proc in the main program. What happens ? Error ? (you now can access Foo() in the module simply through its name right ?)

Is all the above right ?
"Have you tried turning it off and on again ?"
A little PureBasic review
Little John
Addict
Addict
Posts: 4519
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Classes in PureBasic

Post by Little John »

fsw wrote:We have already IncludeFile and IncludeBinary why not add IncludeModule and make each file a module?
Yes, that would be simple and useful.
Each file included by "IncludeModule" is a separate namespace. But procedures, structures or variables in the module which are declared "External" can be also accessed by code which is outside of the module. Keep it really simple. :-)
User avatar
idle
Always Here
Always Here
Posts: 5042
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Classes in PureBasic

Post by idle »

it makes perfect sense to use an underscore
So how would you escape a namespace and call a globally scoped function from within a namespace
seems like we need an escape symbol

Code: Select all

;globally scoped function
Procedure Foo() 
;...
EndProcedure 

NameSpace Foo 
   state.i
   procedure Foo() 
   ;...
   EndProcedure 
EndNameSpace

UseNameSpace Foo 
   Foo() 
   ;how to escape a namespace to the global namespace?         
   _foo() ; using underscore could break existing code 
   \foo() ; is just confusing    
   ::foo() ;would work well   
   
   ;or like like this which would be a hassle   
    UseNameSpace Main 
       foo() 
    CloseNameSpace 
 
Windows 11, Manjaro, Raspberry Pi OS
Image
freak
PureBasic Team
PureBasic Team
Posts: 5929
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Re: Classes in PureBasic

Post by freak »

ts-soft wrote:[...] To make Binary modules around the code before others inaccessible,
around this to selling or simply because one is ashamed of that :mrgreen:
Be honest: The second one is the critical one :D
User_Russian wrote: All constants, structures, procedures need to be isolated from the main program, and then the that suggest, can not be called isolation!
The concept above would apply to all names. The procedures are just examples.
User_Russian wrote:Imagine you have a game engine (for example), divided into several modules. Engine itself is a module, and modules engine - nested modules main module.
You can have the nested modules side by side. Just use descriptive names and there will be no problem. There is no need for them to be nested. I don't know a real world project that uses nested namespaces, even though other languages allow it. People avoid this because it makes things more complex, not less.
User_Russian wrote:freak wrote:
What other feature should we have dropped to get the time for this?
For example, 3D function.
That is just your opinion. If i ask somebody else he would mention your most favorite feature. Would you be ok if we had postponed that?
PMV wrote:
freak wrote:Its not like we did nothing all these years. Look at the release history. What other feature should we have dropped to get the time for this?
Oh ... if you mean my quiping ... sorry if it was to much. :)
It was aimed at the general "why did feature X take so many years?" questions. Its not feature X that took so long. The tons of other features combined did.
luis wrote: 1B) Foo() in the module is External
You try do define a Foo() proc in the main program. What happens ? Error ? (you now can access Foo() in the module simply through its name right ?)
You get an "already defined" error because you can see the one in the module and try to define something with the same name.
quidquid Latine dictum sit altum videtur
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: Classes in PureBasic

Post by ts-soft »

I think adding a Alias was a good idea

Code: Select all

Module GUI
  External Procedure Foo()
  EndProcedure
EndModule

Procedure Foo()
EndProcedure

OpenModule GUI As Blub
  BLub_Foo()
  Foo()
CloseModule
No conflict
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
fsw
Addict
Addict
Posts: 1572
Joined: Tue Apr 29, 2003 9:18 pm
Location: North by Northwest

Re: Classes in PureBasic

Post by fsw »

idle wrote:it makes perfect sense to use an underscore
No an underscore makes no sense.
At least to me :P

This is better:

Code: Select all

Module main
IncludeModule "myFoo" ; has a Foo() procedure

Procedure Foo() 
;...
EndProcedure 

   Foo() ; this is the local Foo
   myFoo\\Foo() ; this is the Foo from module myFoo
   
Besides "\" is used for structure items; it makes sense to use "\\" for module items.

If there is no local Foo() the code could look like this:

Code: Select all

Module main
IncludeModule "myFoo" ; has a Foo() procedure

   Foo() ; this is the Foo from module myFoo
   
No module name needed as there is only one Foo().


Using NameSpace/EndNameSpace, UseNameSpace/CloseNameSpace and other things are not clever, it asks for trouble...

This is my opinion and I stick to it :mrgreen:
Last edited by fsw on Mon Feb 04, 2013 10:21 pm, edited 1 time in total.

I am to provide the public with beneficial shocks.
Alfred Hitshock
User avatar
skywalk
Addict
Addict
Posts: 3972
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Classes in PureBasic

Post by skywalk »

freak wrote:
User_Russian wrote: All constants, structures, procedures need to be isolated from the main program, and then the that suggest, can not be called isolation!
The concept above would apply to all names. The procedures are just examples.
By 'names' you mean constants and structures would be isolated too?
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
freak
PureBasic Team
PureBasic Team
Posts: 5929
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Re: Classes in PureBasic

Post by freak »

skywalk wrote:
freak wrote:
User_Russian wrote: All constants, structures, procedures need to be isolated from the main program, and then the that suggest, can not be called isolation!
The concept above would apply to all names. The procedures are just examples.
By 'names' you mean constants and structures would be isolated too?
Yes.

Btw, another reason for the '_': #MyConstant inside Module Foo becomes "#Foo_MyConstant" which looks good. "Foo::#MyConstant" or "#Foo::MyConstant" are both a bit weird imho :)
quidquid Latine dictum sit altum videtur
User avatar
skywalk
Addict
Addict
Posts: 3972
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Classes in PureBasic

Post by skywalk »

Ok, that is very tempting and I will sacrifice my top wish for modules :mrgreen:
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
PMV
Enthusiast
Enthusiast
Posts: 727
Joined: Sat Feb 24, 2007 3:15 pm
Location: Germany

Re: Classes in PureBasic

Post by PMV »

freak wrote:The concept above would apply to all names. The procedures are just examples.
With that the underscore makes much more sense as everything else. But i
will repeat myself ... It is a solid design and thats what i expected. Very nice!
Image



Only still curious about the release version. :P
Fred
Administrator
Administrator
Posts: 16619
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: Classes in PureBasic

Post by Fred »

See why we don't talk about next features publicly ;)
Post Reply