call sequence

106 views
Skip to first unread message

hbenrico

unread,
Jun 17, 2021, 2:26:28 AM6/17/21
to Harbour Developers
Hi harbour developers,
please have a look at this harbour behavior

a := class1():new() 
a := class1():new() 

those statements call, in sequence:
1) class1 constructor (relative to first assignment)
2) class1 constructor (relative to second assignment)
3) class1 destructor (relative to first assignment)

would it be possible to change the sequence to:
1) class1 constructor (relative to first assignment)
2) class1 destructor (relative to first assigment)
3) class1 constructor (relative to second assignment)

thanks in advance
Enrico

Maurizio la Cecilia

unread,
Jun 17, 2021, 2:48:13 AM6/17/21
to Harbour developers group
Hi Enrico,
just a thought, not tested.
Could you try this:

a := class1():new()
a := nil
a := class1():new() 

The sequence is managed by the garbage collector and the flow is correct, the first object become invalid after the second object instance call.

Best regards.
--
Maurizio


--
You received this message because you are subscribed to the Google Groups "Harbour Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to harbour-deve...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/harbour-devel/80c2c107-e246-4d46-bccd-72016c6238b2n%40googlegroups.com.

hbenrico

unread,
Jun 17, 2021, 4:35:44 AM6/17/21
to Harbour Developers
Hello Maurizio...

:) yes I know it works in that way (corrently im using the solution you provided)
just wanted to know if it was possible to implement automatically another call sequence since in the program flow some time I'm forgetting to assign the NIL before reassigning the value 


thank you!!!

Enrico

Aleksander Czajczynski

unread,
Jun 17, 2021, 6:03:53 AM6/17/21
to harbou...@googlegroups.com
Hi!

New object instance is created before the assignment happens.
Personally, i wouldn't write any code that relies on the order of
destructors. But you can play with preprocessor to always apply NIL in
between. Because why not.

https://os.allcom.pl/hb/3.2/#!eJxdjUFrg0AUhO_vVwzmsgtBSHqzxiIqNGBXWDe5r2abCLKBrKXJv69b2FJzezPzzbzVYPvx62QQXbp-1M7F_SUiWt2nm7Zu1JNBqjMkO6RdxngizDdD2q_jOM7AsctCLPY1XoNgS9qTnKhuiryGJtKeUSHfzNnS2c78k_XiLSrqvG2hCMAxl7D--KjUe1Nib4eJWY6iEa2Sh0I10qdlFRRKxqkSJX5HiJ57f9NJYv1rPy4rdZACzoyfswqVeegf_oboZNx0uz4Ge47Wvr5s_gCnRlh0

In playground it doesn't seem to make any difference, without NIL
assignment, destruction order is the same.

Best regards, Aleksander

--

#include "hbclass.ch"

#xtranslate <a> := <b>():New( <c,...> ) => <a> := NIL ; <a> := (
<b>():New( <c> ) )

LOCAL a

a := T():New( 1 )
a := T():new( 2 )
a := T():new( 3 )

CLASS T
VAR n
METHOD Init(n) CONSTRUCTOR
DESTRUCTOR D()
END CLASS

METHOD Init(n) CLASS T
::n := n
RETURN self

METHOD D() CLASS T
? "destroying", ::n
RETURN self
> <https://groups.google.com/d/msgid/harbour-devel/80c2c107-e246-4d46-bccd-72016c6238b2n%40googlegroups.com?utm_medium=email&utm_source=footer>.
>
> --
> You received this message because you are subscribed to the Google
> Groups "Harbour Developers" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to harbour-deve...@googlegroups.com
> <mailto:harbour-deve...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/harbour-devel/2c661a98-20d4-4a9a-80f7-a4d1837faf4fn%40googlegroups.com
> <https://groups.google.com/d/msgid/harbour-devel/2c661a98-20d4-4a9a-80f7-a4d1837faf4fn%40googlegroups.com?utm_medium=email&utm_source=footer>.


Aleksander Czajczynski

unread,
Jun 17, 2021, 6:11:42 AM6/17/21
to harbou...@googlegroups.com
or even
#xtranslate <a> := <b>():New( <c,...> ) => ( <a> := NIL, <a> := (
<b>():New( <c> ) ) )
that seems to be a bit better than
#xtranslate <a> := <b>():New( <c,...> ) => <a> := NIL ; <a> := (
<b>():New( <c> ) )
for inline expressions.

Aleksander Czajczynski wrote:
> New object instance is created before the assignment happens.
> Personally, i wouldn't write any code that relies on the order of
> destructors. But you can play with preprocessor to always apply NIL in
> between. Because why not.
[...]

hbenrico

unread,
Jun 17, 2021, 7:37:20 AM6/17/21
to Harbour Developers
I was (erroneausly) thinking it would have been better to have an object destroyed before a new one being assigned to the same variable.
now I understand I can't rely on the destructor calls sequence.
I will implement your solution though, playing with preprocessor.

thanks again :)
Enrico-

Reply all
Reply to author
Forward
0 new messages