Page 1 of 2

[Done] Remove the confusion with "Extends"

Posted: Fri Sep 18, 2020 12:56 pm
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.

Re: Remove the confusion with "Extends"

Posted: Fri Sep 18, 2020 1:13 pm
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?

Re: Remove the confusion with "Extends"

Posted: Fri Sep 18, 2020 1:31 pm
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.

Re: Remove the confusion with "Extends"

Posted: Fri Sep 18, 2020 3:02 pm
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.

Re: Remove the confusion with "Extends"

Posted: Fri Sep 18, 2020 3:07 pm
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.

Re: Remove the confusion with "Extends"

Posted: Fri Sep 18, 2020 3:14 pm
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. :)

Re: Remove the confusion with "Extends"

Posted: Fri Sep 18, 2020 3:42 pm
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.

Re: Remove the confusion with "Extends"

Posted: Fri Sep 18, 2020 3:58 pm
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.

Re: Remove the confusion with "Extends"

Posted: Fri Sep 18, 2020 4:05 pm
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

Re: Remove the confusion with "Extends"

Posted: Fri Sep 18, 2020 6:34 pm
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

Re: Remove the confusion with "Extends"

Posted: Fri Sep 18, 2020 11:40 pm
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.

Re: Remove the confusion with "Extends"

Posted: Sat Sep 19, 2020 11:08 am
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'.

Re: Remove the confusion with "Extends"

Posted: Sun Sep 20, 2020 11:40 am
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.

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

Posted: Sun Sep 20, 2020 4:35 pm
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 ???

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

Posted: Sun Sep 20, 2020 8:05 pm
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.