Jump to page: 1 2
Thread overview
Templated Interfaces?
Aug 24, 2008
Benji Smith
Aug 24, 2008
Ary Borenszweig
Aug 24, 2008
Benji Smith
Aug 24, 2008
Simen Kjaeraas
Aug 24, 2008
Benji Smith
Aug 24, 2008
Benji Smith
Aug 25, 2008
Ary Borenszweig
Aug 25, 2008
Sergey Gromov
Aug 26, 2008
Ary Borenszweig
Aug 27, 2008
Ary Borenszweig
Sep 08, 2008
Bruno Medeiros
Aug 24, 2008
Jason House
Aug 24, 2008
Benji Smith
August 24, 2008
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
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
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
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
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
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
"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
"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
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
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
« First   ‹ Prev
1 2