digitalmars.D.bugs - Bug casting class[] to interface[] ?
- Ivan Senji (46/46) Jun 04 2004 I have interface Ifunc and class A implementing it.
- Walter (4/50) Jun 05 2004 The compiler should issue an error diagnostic on this. While you can con...
- Ivan Senji (11/73) Jun 06 2004 convert
- Hauke Duden (18/34) Jun 06 2004 This is an old problem. Some languages allow it, others don't.
- Ivan Senji (7/41) Jun 06 2004 Thanks! I see the problem now. Allowing this cast would be
- Ivan Senji (21/55) Jun 06 2004 Problem solved:
- Walter (3/5) Jun 07 2004 Arr, this be not International Talk Like A Pirate Day, me matey! Arr.
- Ivan Senji (5/11) Jun 07 2004 LOL! :)
- Walter (3/17) Jun 07 2004 I guess that's the way it should be, though.
I have interface Ifunc and class A implementing it. The bug occurs when casting A[] to Ifunc[] Here is the code to demonstrate it: <CODE START> import std.c.stdio; interface Ifunc { int func(); } class A: Ifunc { this(int x){this.x=x;} int anotherfunc(int x) { int y = 4; return x; } int func() { return x; } int x; } void funcX(Ifunc[] u) { foreach(Ifunc f;u) { printf("%d ",f.func()); } } int main ( char [] [] args ) { A[] arr; arr~=new A(1); arr~=new A(3); arr~=new A(7); arr~=new A(11); funcX(arr); getch(); return 1; } <CODE END> Or is this casting not suposed to be possible? I hope it should be possible :) I didn't mention it but the funcX prints Object <adress> pairs or something like that!
Jun 04 2004
The compiler should issue an error diagnostic on this. While you can convert a class to its base interface, that doesn't apply to arrays of classes. "Ivan Senji" <ivan.senji public.srce.hr> wrote in message news:c9qv5i$23np$1 digitaldaemon.com...I have interface Ifunc and class A implementing it. The bug occurs when casting A[] to Ifunc[] Here is the code to demonstrate it: <CODE START> import std.c.stdio; interface Ifunc { int func(); } class A: Ifunc { this(int x){this.x=x;} int anotherfunc(int x) { int y = 4; return x; } int func() { return x; } int x; } void funcX(Ifunc[] u) { foreach(Ifunc f;u) { printf("%d ",f.func()); } } int main ( char [] [] args ) { A[] arr; arr~=new A(1); arr~=new A(3); arr~=new A(7); arr~=new A(11); funcX(arr); getch(); return 1; } <CODE END> Or is this casting not suposed to be possible? I hope it should be possible :) I didn't mention it but the funcX prints Object <adress> pairs or something like that!
Jun 05 2004
"Walter" <newshound digitalmars.com> wrote in message news:c9u6vu$png$1 digitaldaemon.com...The compiler should issue an error diagnostic on this. While you canconverta class to its base interface, that doesn't apply to arrays of classes.Really? Isn't this something that is neaded? My class accepts an array of interfaces and now the user can't just pass his own classes array, but he has to manually convert an array of his class to an array of interfaces. It complicates the user code a lot. Is it not possible to implement this kind of conversion, or is it for some other reason not wise to have this?"Ivan Senji" <ivan.senji public.srce.hr> wrote in message news:c9qv5i$23np$1 digitaldaemon.com...I have interface Ifunc and class A implementing it. The bug occurs when casting A[] to Ifunc[] Here is the code to demonstrate it: <CODE START> import std.c.stdio; interface Ifunc { int func(); } class A: Ifunc { this(int x){this.x=x;} int anotherfunc(int x) { int y = 4; return x; } int func() { return x; } int x; } void funcX(Ifunc[] u) { foreach(Ifunc f;u) { printf("%d ",f.func()); } } int main ( char [] [] args ) { A[] arr; arr~=new A(1); arr~=new A(3); arr~=new A(7); arr~=new A(11); funcX(arr); getch(); return 1; } <CODE END> Or is this casting not suposed to be possible? I hope it should be possible :) I didn't mention it but the funcX prints Object <adress> pairs or something like that!
Jun 06 2004
Ivan Senji wrote:This is an old problem. Some languages allow it, others don't. It is rooted in type-safety constraints. Here's an example: class Base {} class Sub : Base {} Sub[] s=new Sub[10]; Base[] b; //this is not allowed because a Base is not a Sub s[0]=new Base(); //but if this was allowed b=cast(Base[])s; //then you can do this b[0]=new Base(); //and since they are the same arrays, s[0] is not an instance of Sub //anymore, without the compiler being able to catch this bug. HaukeThe compiler should issue an error diagnostic on this. While you canconverta class to its base interface, that doesn't apply to arrays of classes.Really? Isn't this something that is neaded? My class accepts an array of interfaces and now the user can't just pass his own classes array, but he has to manually convert an array of his class to an array of interfaces. It complicates the user code a lot. Is it not possible to implement this kind of conversion, or is it for some other reason not wise to have this?
Jun 06 2004
"Hauke Duden" <H.NS.Duden gmx.net> wrote in message news:c9utas$1nkc$1 digitaldaemon.com...Ivan Senji wrote:Thanks! I see the problem now. Allowing this cast would be cool and helpfull sometimes but if it causes too much problems than it is probbably not a good idea :( PS. I just love this NG, you can ask all kinds of questions, and there is allways a good person out there to answer :)This is an old problem. Some languages allow it, others don't. It is rooted in type-safety constraints. Here's an example: class Base {} class Sub : Base {} Sub[] s=new Sub[10]; Base[] b; //this is not allowed because a Base is not a Sub s[0]=new Base(); //but if this was allowed b=cast(Base[])s; //then you can do this b[0]=new Base(); //and since they are the same arrays, s[0] is not an instance of Sub //anymore, without the compiler being able to catch this bug.The compiler should issue an error diagnostic on this. While you canconverta class to its base interface, that doesn't apply to arrays of classes.Really? Isn't this something that is neaded? My class accepts an array of interfaces and now the user can't just pass his own classes array, but he has to manually convert an array of his class to an array of interfaces. It complicates the user code a lot. Is it not possible to implement this kind of conversion, or is it for some other reason not wise to have this?Hauke
Jun 06 2004
Problem solved: template cast2interfaceArr(A,B) { B[] cast2interfaceArr(A[] aa) { B[] b = new B[aa.length]; foreach(int index,inout A a; aa) { b[index] = a; } return b; } } and the user code thoesn't look that bad: somefunc(cast2interfaceArr!(MyClass,ISomeInterface)(myarray)); instead of: somefunc(myarray); But an error message must happen to as a warning to the user that the class array can not be passed directly! "Hauke Duden" <H.NS.Duden gmx.net> wrote in message news:c9utas$1nkc$1 digitaldaemon.com...Ivan Senji wrote:This is an old problem. Some languages allow it, others don't. It is rooted in type-safety constraints. Here's an example: class Base {} class Sub : Base {} Sub[] s=new Sub[10]; Base[] b; //this is not allowed because a Base is not a Sub s[0]=new Base(); //but if this was allowed b=cast(Base[])s; //then you can do this b[0]=new Base(); //and since they are the same arrays, s[0] is not an instance of Sub //anymore, without the compiler being able to catch this bug. HaukeThe compiler should issue an error diagnostic on this. While you canconverta class to its base interface, that doesn't apply to arrays of classes.Really? Isn't this something that is neaded? My class accepts an array of interfaces and now the user can't just pass his own classes array, but he has to manually convert an array of his class to an array of interfaces. It complicates the user code a lot. Is it not possible to implement this kind of conversion, or is it for some other reason not wise to have this?
Jun 06 2004
"Ivan Senji" <ivan.senji public.srce.hr> wrote in message news:ca05tf$e57$1 digitaldaemon.com...Problem solved: template cast2interfaceArr(A,B)Arr, this be not International Talk Like A Pirate Day, me matey! Arr.
Jun 07 2004
"Walter" <newshound digitalmars.com> wrote in message news:ca2dk6$qut$1 digitaldaemon.com..."Ivan Senji" <ivan.senji public.srce.hr> wrote in message news:ca05tf$e57$1 digitaldaemon.com...LOL! :) But seriously: i really can't figure out how to call it to make sence: castClassArray2InterfaceArray is a bit(not bool) too long! :)Problem solved: template cast2interfaceArr(A,B)Arr, this be not International Talk Like A Pirate Day, me matey! Arr.
Jun 07 2004
"Ivan Senji" <ivan.senji public.srce.hr> wrote in message news:ca2g8n$v71$1 digitaldaemon.com..."Walter" <newshound digitalmars.com> wrote in message news:ca2dk6$qut$1 digitaldaemon.com...I guess that's the way it should be, though."Ivan Senji" <ivan.senji public.srce.hr> wrote in message news:ca05tf$e57$1 digitaldaemon.com...LOL! :) But seriously: i really can't figure out how to call it to make sence: castClassArray2InterfaceArray is a bit(not bool) too long! :)Problem solved: template cast2interfaceArr(A,B)Arr, this be not International Talk Like A Pirate Day, me matey! Arr.
Jun 07 2004