Thread overview | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
June 15, 2004 Arrays of interfaces | ||||
---|---|---|---|---|
| ||||
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? |
June 15, 2004 Re: Arrays of interfaces | ||||
---|---|---|---|---|
| ||||
Posted in reply to Mike Parker | "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? > > > |
June 15, 2004 Re: Arrays of interfaces | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ivan Senji | 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. |
June 15, 2004 Re: Arrays of interfaces | ||||
---|---|---|---|---|
| ||||
Posted in reply to Mike Parker | 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/ |
June 15, 2004 Re: Arrays of interfaces | ||||
---|---|---|---|---|
| ||||
Posted in reply to Mike Parker | 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 |
June 15, 2004 Re: Arrays of interfaces | ||||
---|---|---|---|---|
| ||||
Posted in reply to Derek | 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. |
June 15, 2004 Re: Arrays of interfaces | ||||
---|---|---|---|---|
| ||||
Posted in reply to Mike Parker | 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.
|
June 16, 2004 Re: Arrays of interfaces | ||||
---|---|---|---|---|
| ||||
Posted in reply to Mike Parker | 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("Generator #%d is a %.*s\n", i, t); }; 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 |
June 16, 2004 Re: Arrays of interfaces | ||||
---|---|---|---|---|
| ||||
Posted in reply to Mike Parker | "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 :) |
June 16, 2004 Re: Arrays of interfaces | ||||
---|---|---|---|---|
| ||||
Posted in reply to Derek Parnell | 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/ |
Copyright © 1999-2021 by the D Language Foundation