www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Synchronized methods in D2

reply Jacob Carlborg <doob me.com> writes:
I'm currently porting a D1 code base to D2 which has the following class 
hierarchy:

interface Map
{
      void clear ();
}

class Hashtable : Map
{
     synchronized void clear () {};
}

class HashMap : Map
{
     void clear () {};
}

When I compiler the code I get an error about the "clear" method in 
Hashtable not being covariant with the "clear" method in Map. Any 
suggestions how I could solve this, preferable working in D1 as well?

-- 
/Jacob Carlborg
Sep 04 2010
parent reply Era Scarecrow <rtcvb32 yahoo.com> writes:
 I'm currently porting a D1 code base to D2 which has the
 following class=20
 hierarchy:
=20
 interface Map
 {
 =A0 =A0 =A0 void clear ();
 }
=20
 class Hashtable : Map
 {
 =A0 =A0=A0=A0synchronized void clear () {};
 }
=20
 class HashMap : Map
 {
 =A0 =A0=A0=A0void clear () {};
 }
=20
 When I compiler the code I get an error about the "clear"
 method in=20
 Hashtable not being covariant with the "clear" method in
 Map. Any=20
 suggestions how I could solve this, preferable working in
 D1 as well?
=20
 --=20
 /Jacob Carlborg
I remember reading about this; The signatures have to match EXACTLY for i= t to work. The interface is a declaration of a contract, of what it expects= . If a part of the contract is broken, an error tells you where to fix it. Even if technically it would be compatible, the compiler and type checking= won't allow it. So either synchronize your interface as well, or drop it f= rom the implementation. A third option, is to match the declaration, and have a private function t= hat is synchronized that is called from clear. At least, i believe this is = right. Era=0A=0A=0A
Sep 04 2010
parent Jacob Carlborg <doob me.com> writes:
On 2010-09-04 21:06, Era Scarecrow wrote:
 I'm currently porting a D1 code base to D2 which has the
 following class
 hierarchy:

 interface Map
 {
        void clear ();
 }

 class Hashtable : Map
 {
       synchronized void clear () {};
 }

 class HashMap : Map
 {
       void clear () {};
 }

 When I compiler the code I get an error about the "clear"
 method in
 Hashtable not being covariant with the "clear" method in
 Map. Any
 suggestions how I could solve this, preferable working in
 D1 as well?

 --
 /Jacob Carlborg
I remember reading about this; The signatures have to match EXACTLY for it to work. The interface is a declaration of a contract, of what it expects. If a part of the contract is broken, an error tells you where to fix it. Even if technically it would be compatible, the compiler and type checking won't allow it. So either synchronize your interface as well, or drop it from the implementation. A third option, is to match the declaration, and have a private function that is synchronized that is called from clear. At least, i believe this is right. Era
If a private method works then that might be the solution, thanks. -- /Jacob Carlborg
Sep 05 2010