[Done] Remove the confusion with "Extends"

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
Sicro
Enthusiast
Enthusiast
Posts: 538
Joined: Wed Jun 25, 2014 5:25 pm
Location: Germany
Contact:

[Done] Remove the confusion with "Extends"

Post by Sicro »

Code: Select all

Structure Main
  var1.i
EndStructure

Structure Sub Extends Main
  var2.i
EndStructure
If you read these structures, you would normally assume that the structure "Main" is extended by the structure "Sub".

In the code:
Structure Sub Extends Main
As it really is:
Structure Main Extends Sub
I have not looked at how it is with other programming languages. Anyway, it is confusing.

If at some point a PB version is released that is incompatible with the previous PB versions, it might be good to remove this confusion.
Last edited by Sicro on Sun Sep 20, 2020 11:41 am, edited 1 time in total.
Image
Why OpenSource should have a license :: PB-CodeArchiv-Rebirth :: Pleasant-Dark (syntax color scheme) :: RegEx-Engine (compiles RegExes to NFA/DFA)
Manjaro Xfce x64 (Main system) :: Windows 10 Home (VirtualBox) :: Newest PureBasic version
BarryG
Addict
Addict
Posts: 3292
Joined: Thu Apr 18, 2019 8:17 am

Re: Remove the confusion with "Extends"

Post by BarryG »

Sicro wrote:If you read these structures, you would normally assume that the structure "Main" is extended by the structure "Sub".
I don't use structures, but I assume the opposite; that a new structure called "Sub" is created that is an extended version of the existing structure called "Main". Isn't that how Extends works?
User avatar
skywalk
Addict
Addict
Posts: 3972
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Remove the confusion with "Extends"

Post by skywalk »

BarryG wrote:I don't use structures
:shock:
I cannot believe this statement.

Anyway, EXTENDS is perfectly reasonable as is.
I use it to add some unique traits to a base structure.
I have thousands of lines of Structures.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
User avatar
Sicro
Enthusiast
Enthusiast
Posts: 538
Joined: Wed Jun 25, 2014 5:25 pm
Location: Germany
Contact:

Re: Remove the confusion with "Extends"

Post by Sicro »

BarryG wrote:a new structure called "Sub" is created that is an extended version of the existing structure called "Main". Isn't that how Extends works?
Yes, that is how it works.
skywalk wrote:Anyway, EXTENDS is perfectly reasonable as is.
I use it to add some unique traits to a base structure.
I have thousands of lines of Structures.
This thread is not about removing the feature "Extends", but about removing the confusion. I̶̶t̶̶ ̶̶s̶̶h̶̶o̶̶u̶̶l̶̶d̶̶ ̶̶w̶̶o̶̶r̶̶k̶̶ ̶̶l̶̶i̶̶k̶̶e̶̶ ̶̶t̶̶h̶̶i̶̶s̶̶: It looks like the keyword could work like this:

Code: Select all

Structure Main
EndStructure

Structure Sub1 Extends Main
  var1.i
EndStructure

Structure Sub2 Extends Main
  var2.i
EndStructure

Define.Main test
test\var1 = 10
test\var2 = 20
If I read

Code: Select all

Structure Sub Extends Main
than I understand that the structure sub extends the structure main.

If I read

Code: Select all

Structure Sub ExtendWith Main
than I understand that the structure sub is extended with the main structure.
Last edited by Sicro on Sun Sep 20, 2020 8:12 pm, edited 2 times in total.
Image
Why OpenSource should have a license :: PB-CodeArchiv-Rebirth :: Pleasant-Dark (syntax color scheme) :: RegEx-Engine (compiles RegExes to NFA/DFA)
Manjaro Xfce x64 (Main system) :: Windows 10 Home (VirtualBox) :: Newest PureBasic version
User avatar
TI-994A
Addict
Addict
Posts: 2512
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: Remove the confusion with "Extends"

Post by TI-994A »

Sicro wrote:I have not looked at how it is with other programming languages.
This is known as inheritance, and it is the general convention used in most object-oriented programming languages.

Basically, the child or derived object (Sub in your example), inherits all the properties of the parent or base object (Main in your example), and extends it with more properties.
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
#NULL
Addict
Addict
Posts: 1440
Joined: Thu Aug 30, 2007 11:54 pm
Location: right here

Re: Remove the confusion with "Extends"

Post by #NULL »

It would not make sense. If you know the definition of structure Main it would be useless or at least incomplete because it could have additions from anywhere else in the code, bad idea. Also in this case the unextened version of Main would be gone?, retroactively during compiletime? And what would Sub actually be in the end? I guess an independent structure without fields of Main? You have to think that through a bit more, it would be way more confusing. :)
User avatar
Saki
Addict
Addict
Posts: 830
Joined: Sun Apr 05, 2020 11:28 am
Location: Pandora

Re: Remove the confusion with "Extends"

Post by Saki »

skywalk wrote:
BarryG wrote:I don't use structures
:shock:
I cannot believe this statement.
Well, it always depends on what he codes and for whom.

Primarily he can code everything he wants without structures, the code is then probably better readable and understandable for him.

I myself accept this without reservation.

I think structures are very useful, but you can also use them to make codes more difficult to read.
地球上の平和
User avatar
skywalk
Addict
Addict
Posts: 3972
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Remove the confusion with "Extends"

Post by skywalk »

Nope. Structures are so fundamental to programming, it is silly to argue.
If you pass parameters ByRef, you use Structures.
If you use drawing libs and POINT or RECT you use Structures.
If you use List() or Map() with only native types, your code is infinitely more complex to understand.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
Little John
Addict
Addict
Posts: 4519
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Remove the confusion with "Extends"

Post by Little John »

Sicro, you know how Extends works.
You just think that that isn't the proper keyword for this purpose, right?

Well, both of us are not native English speakers ...
Anyway, maybe the following temporary solution is better readable for you: :-)

Code: Select all

Macro IsAnExtensionOf
   Extends
EndMacro
; -------------------------

Structure Main
   var1.i
EndStructure

Structure Sub IsAnExtensionOf Main
   var2.i
EndStructure
User avatar
mk-soft
Always Here
Always Here
Posts: 5335
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Remove the confusion with "Extends"

Post by mk-soft »

The behaviour of the functions corresponds to the rules and behaves in the same way as with interfaces.

This is also important if you do this several times in inheritance.

If the "Extends XYZ" was not put in front of it, it would not work.

Code: Select all

;-TOP

Structure sBase
  Name.s
EndStructure

Procedure SetName(*Data.sBase, Name.s)
  *Data\Name = Name
EndProcedure

Procedure.s GetName(*Data.sBase)
  ProcedureReturn *Data\Name
EndProcedure

; ----

Structure sAddress Extends sBase
  Street.s
  City.s
EndStructure

Procedure SetAddress(*Data.sAddress, Street.s, City.s)
  With *Data
    \Street = Street
    \City = City
  EndWith
EndProcedure

Procedure.s GetAddress(*Data.sAddress)
  Protected r1.s
  
  With *Data
    r1 = \Name + ";" + \Street + ";" + \City
  EndWith
  
  ProcedureReturn r1
EndProcedure

; ----
This means that the basic functions can be used over and over again, regardless of the subsequent structures and functions.

Example with different structures: Mini Thread control
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
BarryG
Addict
Addict
Posts: 3292
Joined: Thu Apr 18, 2019 8:17 am

Re: Remove the confusion with "Extends"

Post by BarryG »

skywalk wrote:
BarryG wrote:I don't use structures
:shock:
I cannot believe this statement.
My comment was a bit wrong. What I mean is I'll have them in my code when I import other people's code for my apps, but I never actually create my own structures.
User avatar
Josh
Addict
Addict
Posts: 1183
Joined: Sat Feb 13, 2010 3:45 pm

Re: Remove the confusion with "Extends"

Post by Josh »

Sicro wrote: If I read

Code: Select all

Structure Sub Extends Main
than I understand that the structure sub extends the structure main.
This is exactly how the command works. The main command is 'Structure Sub' and the secondary command 'Extends Main' specifies that this structure is extended.
Sicro wrote: If I read

Code: Select all

Structure Sub ExtendWith Main
than I understand that the structure sub is extended with the main structure.
This is complete nonsense, this suggests that 'Structure Sub' is extended by the structure 'Main', which would mean that 'Main' is appended to the end of 'Sub'.
sorry for my bad english
User avatar
Sicro
Enthusiast
Enthusiast
Posts: 538
Joined: Wed Jun 25, 2014 5:25 pm
Location: Germany
Contact:

Re: Remove the confusion with "Extends"

Post by Sicro »

Little John wrote:Sicro, you know how Extends works.
You just think that that isn't the proper keyword for this purpose, right?
Exactly.
The keyword Extends can be understood in two ways:
  • Variant 1 (correct):

    Code: Select all

    Structure Main
      var1.i
    EndStructure
    
    Structure Sub Extends Main
      ; Structure Sub extends structure Main
      var2.i
    EndStructure
    
    Define.Main example
    example\var1
  • Variant 2 (wrong):

    Code: Select all

    Structure Main
      var1.i
    EndStructure
    
    Structure Sub Extends Main
      ; Structure Sub extends structure Main
      var2.i
    EndStructure
    
    Define.Main example
    example\var1
    example\var2
Little John wrote:IsAnExtensionOf
Your keyword is good and reflects the correct function of the keyword Extends. Shorter would be: ExtensionOf
Josh wrote:This is complete nonsense, this suggests that 'Structure Sub' is extended by the structure 'Main', which would mean that 'Main' is appended to the end of 'Sub'.
You are right, ExtendWith would be an incorrect keyword.


I sometimes use Extends in structures, but this time I just thought that this keyword can be understood ambiguously, although the second interpretation makes little sense.

Obviously nobody has problems with it. All is well. But I think it is good to have mentioned it.
Image
Why OpenSource should have a license :: PB-CodeArchiv-Rebirth :: Pleasant-Dark (syntax color scheme) :: RegEx-Engine (compiles RegExes to NFA/DFA)
Manjaro Xfce x64 (Main system) :: Windows 10 Home (VirtualBox) :: Newest PureBasic version
User avatar
kenmo
Addict
Addict
Posts: 1967
Joined: Tue Dec 23, 2003 3:54 am

Re: [Done] Remove the confusion with "Extends"

Post by kenmo »

Also, as long as PureBasic remains a "single pass compiler", this proposal would cause all sorts of dangerous edge cases.

Earlier parts of the code would think a Structure is one size, and later parts of the code would think it's another size?? See example.

I agree to leave the functionality as-is, maybe the documentation could be clarified.

Code: Select all

; this is all hypothetical............!

Structure MyStruct
  Var1.l ; 4 byte
EndStructure

Procedure.i CreateStruct()
  ProcedureReturn AllocateMemory(SizeOf(MyStruct)) ; allocates 4 bytes of memory
EndProcedure

*A.MyStruct = CreateStruct()

; ...

Structure MyStructEx Extends MyStruct ; If this actually resized the MyStruct structure......
  Var2.l ; 4 more bytes
EndStructure

Procedure.i AnotherFunction()
  ProcedureReturn AllocateMemory(SizeOf(MyStruct)) ; this allocates 8 bytes of memory!?
EndProcedure

*B.MyStruct = AnotherFunction()
*B\Var2 = 10 ; OK...

*A\Var2 = 10 ; this would crash, since CreateStruct() only allocated 4 bytes, not 8 ???
User avatar
Sicro
Enthusiast
Enthusiast
Posts: 538
Joined: Wed Jun 25, 2014 5:25 pm
Location: Germany
Contact:

Re: [Done] Remove the confusion with "Extends"

Post by Sicro »

@kenmo:
Thanks for the clarification. This thread was primarily about the ambiguity of the keyword Extends. I do not want to suggest any other functionality of the keyword here. Later, I mistakenly wrote that the keyword should work differently (It should work like this:). I meant it looks like the keyword could work differently.
Image
Why OpenSource should have a license :: PB-CodeArchiv-Rebirth :: Pleasant-Dark (syntax color scheme) :: RegEx-Engine (compiles RegExes to NFA/DFA)
Manjaro Xfce x64 (Main system) :: Windows 10 Home (VirtualBox) :: Newest PureBasic version
Post Reply