www.digitalmars.com         C & C++   DMDScript  

D - Interfaces that are not "COM" cause "Error: Access Violation"

reply "Mike Wynn" <mike.wynn l8night.co.uk> writes:
the code at the end of posting compiles but when run causes
Error: Access Violation

however ... if callLayout was defined as
void callLayout( CanLayout cl ) {
    cl.doLayout();
}
it works (the compiler knows BaseLayout implements CanLayout [in main])
as does
void callLayoutInd(CanLayout cl) {
 if ( cl ) {
  cl.doLayout();
 } else {
  println( "the 'Base' you passed did not implement 'CanLayout'" );
 }
}
void callLayout( Base b ) {
 callLayoutInd( cast(BaseLayout)b );
}

is this intended "interface" behaviour ? [have I missed a big point with D
interfaces?] (would wish for a more "Java" interface).

------------------
//
// interface test 001
//
import c.stdio;

void println( char[] str ) { printf( "%.*s\n", str ); }

interface CanLayout {
 void doLayout();
}

class Base {
 void print() { println("Base::print"); }
}

class BaseLayout : Base, CanLayout {
 void doLayout() { println( "BaseLayout::doLayout" ); }
}

void callLayout( Base b ) {
 CanLayout cl = cast(CanLayout)b;
 if ( cl ) {
  cl.doLayout();
 } else {
  println( "the 'Base' you passed did not implement 'CanLayout'" );
 }
}

int main( char[][] args ) {
 callLayout( new BaseLayout() );
 return 0;
}
Aug 15 2003
parent reply "Walter" <walter digitalmars.com> writes:
Yes, it's intended to work that way, although I'll investigate why there's a
crash rather than just getting a null pointer.

"Mike Wynn" <mike.wynn l8night.co.uk> wrote in message
news:bhil4u$2hj6$1 digitaldaemon.com...
 the code at the end of posting compiles but when run causes
 Error: Access Violation

 however ... if callLayout was defined as
 void callLayout( CanLayout cl ) {
     cl.doLayout();
 }
 it works (the compiler knows BaseLayout implements CanLayout [in main])
 as does
 void callLayoutInd(CanLayout cl) {
  if ( cl ) {
   cl.doLayout();
  } else {
   println( "the 'Base' you passed did not implement 'CanLayout'" );
  }
 }
 void callLayout( Base b ) {
  callLayoutInd( cast(BaseLayout)b );
 }

 is this intended "interface" behaviour ? [have I missed a big point with D
 interfaces?] (would wish for a more "Java" interface).

 ------------------
 //
 // interface test 001
 //
 import c.stdio;

 void println( char[] str ) { printf( "%.*s\n", str ); }

 interface CanLayout {
  void doLayout();
 }

 class Base {
  void print() { println("Base::print"); }
 }

 class BaseLayout : Base, CanLayout {
  void doLayout() { println( "BaseLayout::doLayout" ); }
 }

 void callLayout( Base b ) {
  CanLayout cl = cast(CanLayout)b;
  if ( cl ) {
   cl.doLayout();
  } else {
   println( "the 'Base' you passed did not implement 'CanLayout'" );
  }
 }

 int main( char[][] args ) {
  callLayout( new BaseLayout() );
  return 0;
 }
Aug 15 2003
parent "Mike Wynn" <mike.wynn l8night.co.uk> writes:
"Walter" <walter digitalmars.com> wrote in message
news:bhj2rf$2tm2$1 digitaldaemon.com...
 Yes, it's intended to work that way, although I'll investigate why there's
a
 crash rather than just getting a null pointer.
why can't I cast an instance of BaseLayout (passed as Base) to an interface it implements ? this behaviour makes D even more restricive to use than Java, and seems to contradict the whole idea of interfaces (a set of methods any Object can implement) allowing limited MI like behavious.
 "Mike Wynn" <mike.wynn l8night.co.uk> wrote in message
 news:bhil4u$2hj6$1 digitaldaemon.com...
 the code at the end of posting compiles but when run causes
 Error: Access Violation

 however ... if callLayout was defined as
 void callLayout( CanLayout cl ) {
     cl.doLayout();
 }
 it works (the compiler knows BaseLayout implements CanLayout [in main])
 as does
 void callLayoutInd(CanLayout cl) {
  if ( cl ) {
   cl.doLayout();
  } else {
   println( "the 'Base' you passed did not implement 'CanLayout'" );
  }
 }
 void callLayout( Base b ) {
  callLayoutInd( cast(BaseLayout)b );
 }

 is this intended "interface" behaviour ? [have I missed a big point with
D
 interfaces?] (would wish for a more "Java" interface).

 ------------------
 //
 // interface test 001
 //
 import c.stdio;

 void println( char[] str ) { printf( "%.*s\n", str ); }

 interface CanLayout {
  void doLayout();
 }

 class Base {
  void print() { println("Base::print"); }
 }

 class BaseLayout : Base, CanLayout {
  void doLayout() { println( "BaseLayout::doLayout" ); }
 }

 void callLayout( Base b ) {
  CanLayout cl = cast(CanLayout)b;
  if ( cl ) {
   cl.doLayout();
  } else {
   println( "the 'Base' you passed did not implement 'CanLayout'" );
  }
 }

 int main( char[][] args ) {
  callLayout( new BaseLayout() );
  return 0;
 }
Aug 15 2003