Possible ambiguity with destructor

Forum for discussion about the documentation project.
Post Reply
sancho3
Posts: 358
Joined: Sep 30, 2017 3:22

Possible ambiguity with destructor

Post by sancho3 »

The manual states:
Only one destructor may be declared and defined per type.
But with inheritance there can be more than one destructor. In the following code type B contains and runs two destructors.

Code: Select all

Type test
	As Integer x
	Declare Destructor
End Type
Destructor test()
	?"base destructor"
End Destructor
Type b Extends test
	Declare Destructor
End Type
Destructor b()
	'
	? "another destructor"
End Destructor
Scope 
	Dim As b btest
End Scope
Sleep
Type B does not contain a type test object. Type B is a type that contains all the members of type Test. Therefore type B contains 2 destructors and that contradicts what the manual says.
Do you think that statement needs clarification?
Proposed clarification:
Only one destructor may be declared and defined per type. An exception is in the case that a type may have a destructor and also inherit the destructor from a base type.
And then perhaps add another example to show this behavior.
counting_pine
Site Admin
Posts: 6323
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

Re: Possible ambiguity with destructor

Post by counting_pine »

It's true there can be more than one destructor *executed* - for any contained and inherited types with destructors. (And yes, that should be made clear if it's not already.)
But it's still true that only one destructor can be *declared and defined* per type.
fxm
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Possible ambiguity with destructor

Post by fxm »

Proposal of adding:
[url=https://www.freebasic.net/wiki/wikka.php?wakka=KeyPgDestructor][b]DESTRUCTOR[/b][/url] documentation page wrote: .....
Only one destructor may be declared and defined per type (but several destructors can be called in a chained way if the type contains or inherits other types with their own destructors)
.....
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Possible ambiguity with destructor

Post by MrSwiss »

@fxm,

I'd shorten the *addition* somewhat, to make it clearer, by re-writing: "contains or inherits" ...
(IMHO, a source for confusion since, it can only *contain by inheritance*, no other way!)

So, the addition, now:
proposed addition NEW: wrote:(but several destructors can be called in a chained way, if the type contains inherited other types, with their own destructors)
fxm
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Possible ambiguity with destructor

Post by fxm »

I don't agree.

It's for mentioning the two distinct cases where several destructors can be called (in a chained way):
- composition (“has-a” relationship between types):

Code: Select all

Type T2
  Dim As T1 o1
  Declare destructor ()
End Type
- inheritance (“is-a” relationship between types):

Code: Select all

Type T2 Extends T1
  Declare destructor ()
End Type
sancho3
Posts: 358
Joined: Sep 30, 2017 3:22

Re: Possible ambiguity with destructor

Post by sancho3 »

(but several destructors can be called in a chained way if the type contains or inherits other types with their own destructors)
That works, thanks.
fxm
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Possible ambiguity with destructor

Post by fxm »

Done:
KeyPgDestructor → fxm [Added a precision on the chaining of destructor calls in case of composition or inheritance]
Post Reply