Thread overview | |||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
August 24, 2008 Templated Interfaces? | ||||
---|---|---|---|---|
| ||||
I've been trying to create a templated interface, and can't figure out what I'm doing wrong. Here's the basic example: interface I(T) { public T x(); } class C(T) : I(T) { private T value; public this(T val) { this.value = val; } public T x() { return value; } } void main() { I!(char) c = new C!(char)('a'); char value = c.x(); } I've tried many many slight variations of this, to try to satisfy the compiler, but it always rejects the code, usually by saying something like "Interface I used as a type". Of course! That's because interface I *is* a type. What am I doing wrong? --benji |
August 24, 2008 Re: Templated Interfaces? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Benji Smith | Benji Smith a écrit :
> I've been trying to create a templated interface, and can't figure out what I'm doing wrong. Here's the basic example:
>
> interface I(T) {
> public T x();
> }
>
> class C(T) : I(T) {
class C(T) : I!(T) {
|
August 24, 2008 Re: Templated Interfaces? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ary Borenszweig | Ary Borenszweig wrote:
> Benji Smith a écrit :
>> I've been trying to create a templated interface, and can't figure out what I'm doing wrong. Here's the basic example:
>>
>> interface I(T) {
>> public T x();
>> }
>>
>> class C(T) : I(T) {
>
> class C(T) : I!(T) {
Very nice! Thanks!
Now let's say I want to create an array of those interface instances:
interface I(T) {
public T x();
}
class C(T) : I!(T) {
private T value;
public this(T val) { this.value = val; }
public T x() { return value; }
}
void main() {
I!(char)[] array = new I!(char)[];
array ~ new C!(char)('a');
array ~ new C!(char)('b');
array ~ new C!(char)('c');
char value = array[0].x();
}
Now the compiler is complaining about the line where I create the array. It says "Error: new can only create structs, dynamic arrays or class objects, not I[]'s"
Obviously, I'm trying to create a dynamic array of interface instances, so I'm not sure what I'm doing wrong.
Thanks!
--benji
|
August 24, 2008 Re: Templated Interfaces? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Benji Smith | On Sun, 24 Aug 2008 22:20:46 +0200, Benji Smith <dlanguage@benjismith.net> wrote: > Ary Borenszweig wrote: >> Benji Smith a écrit : >>> I've been trying to create a templated interface, and can't figure out what I'm doing wrong. Here's the basic example: >>> >>> interface I(T) { >>> public T x(); >>> } >>> >>> class C(T) : I(T) { >> class C(T) : I!(T) { > > Very nice! Thanks! > > Now let's say I want to create an array of those interface instances: > > interface I(T) { > public T x(); > } > > class C(T) : I!(T) { > private T value; > public this(T val) { this.value = val; } > public T x() { return value; } > } > > void main() { > I!(char)[] array = new I!(char)[]; > array ~ new C!(char)('a'); > array ~ new C!(char)('b'); > array ~ new C!(char)('c'); > char value = array[0].x(); > } > > Now the compiler is complaining about the line where I create the array. It says "Error: new can only create structs, dynamic arrays or class objects, not I[]'s" > > Obviously, I'm trying to create a dynamic array of interface instances, so I'm not sure what I'm doing wrong. > > Thanks! > > --benji You do not need to initialize the the array with the "new I!(char)[]". Just use I!(char)[] array; array ~= new C!(char)('a'); ... char value = array[0].x(); -- Simen |
August 24, 2008 Re: Templated Interfaces? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Simen Kjaeraas | Simen Kjaeraas wrote:
> You do not need to initialize the the array with the "new I!(char)[]". Just use
>
> I!(char)[] array;
> array ~= new C!(char)('a');
> ...
> char value = array[0].x();
Gotcha.
Coming from Java, it's hard getting used to which types require a 'new' and which ones don't. I find the array types especially confusing.
Thanks for your help!
--benji
|
August 24, 2008 Re: Templated Interfaces? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Simen Kjaeraas | Simen Kjaeraas wrote:
> On Sun, 24 Aug 2008 22:20:46 +0200, Benji Smith <dlanguage@benjismith.net> wrote:
>
>> Ary Borenszweig wrote:
>>> Benji Smith a écrit :
>>>> I've been trying to create a templated interface, and can't figure out what I'm doing wrong. Here's the basic example:
>>>>
>>>> interface I(T) {
>>>> public T x();
>>>> }
>>>>
>>>> class C(T) : I(T) {
>>> class C(T) : I!(T) {
>>
>> Very nice! Thanks!
>>
>> Now let's say I want to create an array of those interface instances:
>>
>> interface I(T) {
>> public T x();
>> }
>>
>> class C(T) : I!(T) {
>> private T value;
>> public this(T val) { this.value = val; }
>> public T x() { return value; }
>> }
>>
>> void main() {
>> I!(char)[] array = new I!(char)[];
>> array ~ new C!(char)('a');
>> array ~ new C!(char)('b');
>> array ~ new C!(char)('c');
>> char value = array[0].x();
>> }
>>
>> Now the compiler is complaining about the line where I create the array. It says "Error: new can only create structs, dynamic arrays or class objects, not I[]'s"
>>
>> Obviously, I'm trying to create a dynamic array of interface instances, so I'm not sure what I'm doing wrong.
>>
>> Thanks!
>>
>> --benji
>
> You do not need to initialize the the array with the "new I!(char)[]".
> Just use
>
> I!(char)[] array;
> array ~= new C!(char)('a');
> ...
> char value = array[0].x();
>
whoa, that's a bad idea!
You can not implicitly cast back and forth between interfaces and classes that implement those interfaces; the underlying pointer is not the same. There was a big discussion about this fairly recently on one of the D mailing lists...
|
August 24, 2008 Re: Templated Interfaces? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Benji Smith | "Benji Smith" <dlanguage@benjismith.net> wrote in message news:g8sih3$7h4$1@digitalmars.com... > Simen Kjaeraas wrote: >> You do not need to initialize the the array with the "new I!(char)[]". Just use >> >> I!(char)[] array; >> array ~= new C!(char)('a'); >> ... >> char value = array[0].x(); > > Gotcha. > > Coming from Java, it's hard getting used to which types require a 'new' and which ones don't. I find the array types especially confusing. > > Thanks for your help! > > --benji Well arrays work _almost_ the same way as in Java, the only difference being that they can change their size. You can either do auto array = new I!(char)[3]; array[0] = .. array[1] = .. array[2] = .. Or just declare it and append stuff. The former will be faster since you'll only do 1 memory allocation instead of n (although the implementation does over-allocate for arrays so that appending is faster, you may not want to depend on that for performance-critical code). |
August 24, 2008 Re: Templated Interfaces? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jason House | "Jason House" <jason.james.house@gmail.com> wrote in message news:g8sioq$6ul$1@digitalmars.com... > whoa, that's a bad idea! > > You can not implicitly cast back and forth between interfaces and classes that implement those interfaces; the underlying pointer is not the same. There was a big discussion about this fairly recently on one of the D mailing lists... No, what he's doing is OK. The trouble only occurs when you try to cast *the array* up or down the inheritance hierarchy. |
August 24, 2008 Re: Templated Interfaces? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jarrett Billingsley | Jarrett Billingsley wrote:
> "Jason House" <jason.james.house@gmail.com> wrote in message news:g8sioq$6ul$1@digitalmars.com...
>
>> whoa, that's a bad idea!
>>
>> You can not implicitly cast back and forth between interfaces and classes
>> that implement those interfaces; the underlying pointer is not the same.
>> There was a big discussion about this fairly recently on one of the D
>> mailing lists...
>
> No, what he's doing is OK. The trouble only occurs when you try to cast *the array* up or down the inheritance hierarchy.
Yeah, with the code I'm currently working on, once the objects are in the array, I never again care what the specific class is for each instance, since I'll only be calling the interface methods.
--benji
|
August 24, 2008 Re: Templated Interfaces? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jarrett Billingsley | Jarrett Billingsley wrote:
> Well arrays work _almost_ the same way as in Java, the only difference being that they can change their size.
>
> You can either do
>
> auto array = new I!(char)[3];
> array[0] = ..
> array[1] = ..
> array[2] = ..
>
> Or just declare it and append stuff. The former will be faster since you'll only do 1 memory allocation instead of n (although the implementation does over-allocate for arrays so that appending is faster, you may not want to depend on that for performance-critical code).
Huh. I thought that, by putting an integer literal into the array declaration, it'd be declared as a static array rather than a dynamic array.
--benji
|
Copyright © 1999-2021 by the D Language Foundation