www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - Arrays of interfaces

reply Mike Parker <aldacron71 yahoo.com> writes:
I know casting from an array of classes to an array of interfaces is not 
allowed, but IMO this should be:

*******************************************
interface MyInterface
{
...
}

MyInterface[] interfaceArray;
*******************************************

The compiler spits out: no [] overload for type MyInterface

Doing this:
MyInterface[4] interfaceArray;

Yields this message: '4' is not an lvalue

Bug or not?
Jun 15 2004
parent reply "Ivan Senji" <ivan.senji public.srce.hr> writes:
"Mike Parker" <aldacron71 yahoo.com> wrote in message
news:cank2k$3nm$1 digitaldaemon.com...
 I know casting from an array of classes to an array of interfaces is not
 allowed, but IMO this should be:

 *******************************************
 interface MyInterface
 {
 ...
 }

 MyInterface[] interfaceArray;
 *******************************************

 The compiler spits out: no [] overload for type MyInterface
Are you sure? I tried this (now and before) and it works. At least on dmd 0.92.
 Doing this:
 MyInterface[4] interfaceArray;

 Yields this message: '4' is not an lvalue

 Bug or not?
Jun 15 2004
parent reply Mike Parker <aldacron71 yahoo.com> writes:
Ivan Senji wrote:
 Are you sure? I tried this (now and before) and it works.
 At least on dmd 0.92.
I'm sure. dmd 0.92 on Windows. Here's my exact code sans comments: ****************************************** interface EventGenerator { public: void frame(); } private EventGenerator[] generators; ****************************************** I've tried it without the access modifiers, without the method declaration, moving the brackets... it keeps spitting out the same errors.
Jun 15 2004
next sibling parent Regan Heath <regan netwin.co.nz> writes:
On Wed, 16 Jun 2004 06:31:39 +0900, Mike Parker <aldacron71 yahoo.com> 
wrote:
 Ivan Senji wrote:
 Are you sure? I tried this (now and before) and it works.
 At least on dmd 0.92.
I'm sure. dmd 0.92 on Windows. Here's my exact code sans comments: ****************************************** interface EventGenerator { public: void frame(); } private EventGenerator[] generators; ****************************************** I've tried it without the access modifiers, without the method declaration, moving the brackets... it keeps spitting out the same errors.
This compiles for me too dmd 0.92 Windows. Regan -- Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
Jun 15 2004
prev sibling next sibling parent reply Derek <derek psyc.ward> writes:
On Wed, 16 Jun 2004 06:31:39 +0900, Mike Parker wrote:

 Ivan Senji wrote:
 Are you sure? I tried this (now and before) and it works.
 At least on dmd 0.92.
I'm sure. dmd 0.92 on Windows. Here's my exact code sans comments: ****************************************** interface EventGenerator { public: void frame(); } private EventGenerator[] generators; ****************************************** I've tried it without the access modifiers, without the method declaration, moving the brackets... it keeps spitting out the same errors.
Whoa!! I am really misunderstanding Interfaces. What does an array of interfaces give one? I thought that an interface was a type of instruction to the compiler and didn't, in itself, generate any machine code. I think of them as a set of items that when placed in a class definition, is telling the compiler that these items are mandatory for this class. That is, that these items must be defined for the class. One can't instantiate an interface object. You can't do ... class Foo: generators[2] { . . . } class Bar: generators[4] { . . . } I'm obviously missing something important here. -- Derek Melbourne, Australia
Jun 15 2004
parent reply Mike Parker <aldacron71 yahoo.com> writes:
Derek wrote:
 On Wed, 16 Jun 2004 06:31:39 +0900, Mike Parker wrote:
 
 
Ivan Senji wrote:

Are you sure? I tried this (now and before) and it works.
At least on dmd 0.92.
I'm sure. dmd 0.92 on Windows. Here's my exact code sans comments: ****************************************** interface EventGenerator { public: void frame(); } private EventGenerator[] generators; ****************************************** I've tried it without the access modifiers, without the method declaration, moving the brackets... it keeps spitting out the same errors.
Whoa!! I am really misunderstanding Interfaces. What does an array of interfaces give one? I thought that an interface was a type of instruction to the compiler and didn't, in itself, generate any machine code. I think of them as a set of items that when placed in a class definition, is telling the compiler that these items are mandatory for this class. That is, that these items must be defined for the class. One can't instantiate an interface object.
class Keyboard : EventGenerator { } class Mouse : EventGenerator { } registerGenerator(mouseObject); registerGenerator(keyboardObject); The registerGenerator method should be able to store any EventGenerator implementations in an array of EventGenerators. That's what one gets from an array of interfaces.
Jun 15 2004
parent reply Derek Parnell <derek psych.ward> writes:
On Wed, 16 Jun 2004 07:57:58 +0900, Mike Parker wrote:

 Derek wrote:
 On Wed, 16 Jun 2004 06:31:39 +0900, Mike Parker wrote:
 
Ivan Senji wrote:

Are you sure? I tried this (now and before) and it works.
At least on dmd 0.92.
I'm sure. dmd 0.92 on Windows. Here's my exact code sans comments: ****************************************** interface EventGenerator { public: void frame(); } private EventGenerator[] generators; ****************************************** I've tried it without the access modifiers, without the method declaration, moving the brackets... it keeps spitting out the same errors.
Whoa!! I am really misunderstanding Interfaces. What does an array of interfaces give one? I thought that an interface was a type of instruction to the compiler and didn't, in itself, generate any machine code. I think of them as a set of items that when placed in a class definition, is telling the compiler that these items are mandatory for this class. That is, that these items must be defined for the class. One can't instantiate an interface object.
class Keyboard : EventGenerator { } class Mouse : EventGenerator { } registerGenerator(mouseObject); registerGenerator(keyboardObject); The registerGenerator method should be able to store any EventGenerator implementations in an array of EventGenerators. That's what one gets from an array of interfaces.
Okay, didn't know you could do that! Neat. I played around with this idea and got this... // ----------------- start code import std.c.stdio; interface EventGenerator { public: int frame(); void frame(int pVal); } interface ObjType { public: char[] objType(); } class Keyboard : EventGenerator, ObjType { private: int id = -1; public: int frame() { return id;} char[] objType() { return "keyboard"; } void frame(int pVal){ if (pVal >= 0) id = pVal; } } class Mouse : EventGenerator, ObjType { private: int id = -1; public: int frame() { return id;} char[] objType() { return "mouse"; } void frame(int pVal){ if (pVal >= 0) id = pVal; } } class DupException { private char[] MsgText; this(char[] pMsg) { MsgText = pMsg; } char[] Msg() { return MsgText; } } private EventGenerator[] generators; void registerGenerator(EventGenerator x) { if (x.frame < 0) { x.frame = generators.length + 1; generators ~= x; } else { char[] buffer; buffer.length = 256; _snprintf(buffer, 256, "Duplicate generator. Frame is %d",x.frame); throw new DupException(buffer); } } void main() { Keyboard keyboardObject = new Keyboard; Mouse mouseObject = new Mouse; printf("%.*s pre-reg id = %d\n", keyboardObject.objType, keyboardObject.frame()); printf("%.*s pre-reg id = %d\n", mouseObject.objType, mouseObject.frame()); try{ registerGenerator(mouseObject); registerGenerator(keyboardObject); //registerGenerator(mouseObject); } catch (DupException x) { printf("ERROR: %.*s\n", x.Msg); printf("Number registered: %d\n", generators.length); return; } for (int i = 0; i < generators.length; i++) { int id; char[] t; id = generators[i].frame(); if (mouseObject.frame == id) t = mouseObject.objType; else if (keyboardObject.frame == id) t = keyboardObject.objType; }; printf("%.*s post-reg id = %d\n", keyboardObject.objType, keyboardObject.frame()); printf("%.*s post-reg id = %d\n", mouseObject.objType, mouseObject.frame()); }// -----------------end code -- Derek Melbourne, Australia 16/Jun/04 10:57:43 AM
Jun 15 2004
parent reply J Anderson <REMOVEanderson badmama.com.au> writes:
Derek Parnell wrote:

On Wed, 16 Jun 2004 07:57:58 +0900, Mike Parker wrote:

Okay, didn't know you could do that! Neat. I played around with this idea
and got this... 
  
Its called polymorphism and its one of the cornerstones of object orientation. -- -Anderson: http://badmama.com.au/~anderson/
Jun 16 2004
parent reply Derek <derek psyc.ward> writes:
On Wed, 16 Jun 2004 17:31:56 +0800, J Anderson wrote:

 Derek Parnell wrote:
 
On Wed, 16 Jun 2004 07:57:58 +0900, Mike Parker wrote:

Okay, didn't know you could do that! Neat. I played around with this idea
and got this... 
  
Its called polymorphism and its one of the cornerstones of object orientation.
Hey, I know polymorphism. Its just that I didn't know one could use an interface as a 'property' of a class. I've not worked with modern OOP autocoder with these new-fangled punched card thingies. ;-) -- Derek Melbourne, Australia
Jun 16 2004
parent reply "Matthew" <admin stlsoft.dot.dot.dot.dot.org> writes:
"Derek" <derek psyc.ward> wrote in message
news:3jr7u6r232a8$.1791r062spmxc$.dlg 40tude.net...
 On Wed, 16 Jun 2004 17:31:56 +0800, J Anderson wrote:

 Derek Parnell wrote:

On Wed, 16 Jun 2004 07:57:58 +0900, Mike Parker wrote:

Okay, didn't know you could do that! Neat. I played around with this idea
and got this...
Its called polymorphism and its one of the cornerstones of object orientation.
Hey, I know polymorphism. Its just that I didn't know one could use an interface as a 'property' of a class. I've not worked with modern OOP autocoder with these new-fangled punched card thingies. ;-)
Ha! Punched cards. You lived in luxury. We only had open connections for a 4-bit bus, and we had to set the bits by connecting live plugs them with conductive surfaces from our own bodies! Suffice to say you tried to avoid any codes involving the number 15 ...
Jun 16 2004
parent reply "Kris" <someidiot earthlink.dot.dot.dot.net> writes:
"Matthew" <admin stlsoft.dot.dot.dot.dot.org> wrote in message
news:caqiuk$1inp$1 digitaldaemon.com...
 "Derek" <derek psyc.ward> wrote in message
 news:3jr7u6r232a8$.1791r062spmxc$.dlg 40tude.net...
 On Wed, 16 Jun 2004 17:31:56 +0800, J Anderson wrote:

 Derek Parnell wrote:

On Wed, 16 Jun 2004 07:57:58 +0900, Mike Parker wrote:

Okay, didn't know you could do that! Neat. I played around with this
idea
and got this...
Its called polymorphism and its one of the cornerstones of object orientation.
Hey, I know polymorphism. Its just that I didn't know one could use an interface as a 'property' of a class. I've not worked with modern OOP autocoder with these new-fangled punched card thingies. ;-)
Ha! Punched cards. You lived in luxury. We only had open connections for a
4-bit
 bus, and we had to set the bits by connecting live plugs them with
conductive
 surfaces from our own bodies!

 Suffice to say you tried to avoid any codes involving the number 15 ...
You still 'ad body parts? Ooohhh, we use t' dreeeeaammmm o' body parts. Wor fingers and skin 'ad all been 'acked off t' use for printer 'eads n' cable wrap. An' wor testicles ended up in t' abacus ... 4-bit bus? Huh! You were Lucky!!
Jun 16 2004
parent J Anderson <REMOVEanderson badmama.com.au> writes:
Kris wrote:

"Matthew" <admin stlsoft.dot.dot.dot.dot.org> wrote in message
news:caqiuk$1inp$1 digitaldaemon.com...
  

"Derek" <derek psyc.ward> wrote in message
news:3jr7u6r232a8$.1791r062spmxc$.dlg 40tude.net...
    

On Wed, 16 Jun 2004 17:31:56 +0800, J Anderson wrote:

      

Hey, I know polymorphism. Its just that I didn't know one could use an
interface as a 'property' of a class. I've not worked with modern OOP

autocoder with these new-fangled punched card thingies. ;-)
      
Ha! Punched cards. You lived in luxury. We only had open connections for a
4-bit
bus, and we had to set the bits by connecting live plugs them with
    
conductive
surfaces from our own bodies!

Suffice to say you tried to avoid any codes involving the number 15 ...


    
You still 'ad body parts? Ooohhh, we use t' dreeeeaammmm o' body parts. Wor fingers and skin 'ad all been 'acked off t' use for printer 'eads n' cable wrap. An' wor testicles ended up in t' abacus ... 4-bit bus? Huh! You were Lucky!!
Reminds me of a futuristic movie I once saw. What was it called now? Oh the matrix. -- -Anderson: http://badmama.com.au/~anderson/
Jun 16 2004
prev sibling parent reply Mike Parker <aldacron71 yahoo.com> writes:
Mike Parker wrote:

 Ivan Senji wrote:
 
 Are you sure? I tried this (now and before) and it works.
 At least on dmd 0.92.
I'm sure. dmd 0.92 on Windows. Here's my exact code sans comments: ****************************************** interface EventGenerator { public: void frame(); } private EventGenerator[] generators; ******************************************
Ahem. Not a D bug. The error was actually one line below the one I was looking at, and was indeed a mistake on my part (typo). I will go stick my head in a hole-in-the-ground now.
Jun 15 2004
parent "Ivan Senji" <ivan.senji public.srce.hr> writes:
"Mike Parker" <aldacron71 yahoo.com> wrote in message
news:cao09l$lbt$1 digitaldaemon.com...
 Mike Parker wrote:

 Ivan Senji wrote:

 Are you sure? I tried this (now and before) and it works.
 At least on dmd 0.92.
I'm sure. dmd 0.92 on Windows. Here's my exact code sans comments: ****************************************** interface EventGenerator { public: void frame(); } private EventGenerator[] generators; ******************************************
Ahem. Not a D bug. The error was actually one line below the one I was looking at, and was indeed a mistake on my part (typo). I will go stick my head in a hole-in-the-ground now.
Don't do that, it happens :)
Jun 16 2004