View mode: basic / threaded / horizontal-split · Log in · Help
January 24, 2005
Interface as argument restriction
Hi all.

If I want to receive a class as an argument I write
void foo(Object o)

But is there a way to specify that I want to receive only interfaces (or
interface s and classes) ?
January 24, 2005
Re: Interface as argument restriction
nail wrote:

> Hi all.
> 
> If I want to receive a class as an argument I write
> void foo(Object o)
> 
> But is there a way to specify that I want to receive only interfaces (or
> interface s and classes) ?

The question itself does not make sense:

An object in memory is always an instance of some specific class. There is
no such thing as an instance of an interface.

A reference that you pass to a function therefore is always a pointer to a
class instance, even if it is handled as a interface reference.

Therefore you cannot "receive interfaces" at all (except, of course, that
you work with class pointers, but I don't think that is what you are
doing.)
January 24, 2005
Re: Interface as argument restriction
Norbert Nemec wrote:
> nail wrote:
>>If I want to receive a class as an argument I write
>>void foo(Object o)
>>
>>But is there a way to specify that I want to receive only interfaces (or
>>interface s and classes) ?

> The question itself does not make sense:
[...]
> Therefore you cannot "receive interfaces" at all (except, of course, that
> you work with class pointers, but I don't think that is what you are
> doing.)

If you only want to accept objects *implementing* a certain interface:

	void bar(Interface i)

much like:

	void foo(Subclass O)

But that was not what nail asked... (you would still get Objects always)

--anders
January 24, 2005
Re: Interface as argument restriction
>
>If you only want to accept objects *implementing* a certain interface:
>
>	void bar(Interface i)
>
>much like:
>
>	void foo(Subclass O)
>
>But that was not what nail asked... (you would still get Objects always)
>
>--anders

Well, well, well. Sorry for complication in my post. I'll try to explain my
problem. I have some array that maps ClassInfo to uint, ala
uint myArray[ ClassInfo ];
Then I have some function foo, that must get two arguments of type class or
interface that uses that array, i.e.

interface I {}
class C : I {}

void foo(Object x, Object y)
{
uint idx1 = myArray[x.classinfo];
uint idx2 = myArray[y.classinfo];
...
}

int main()
{
I x = new C();
I y = x;
foo(x, y); // compile error
// I can do following
foo(cast(Object)x, cast(Object)y) // but it's too long :)
return 0;
}

That is my problem. I don't want use cast(Object) or make function in template,
just be able to receive interfaces and classes, not basic types. Ideas?
January 24, 2005
Re: Interface as argument restriction
This is simple... because its already available.  (Just check out the Mango
library over at dsource.org to see it used /extensively/.)

Given an interface Foo:
#
#  interface Foo { ... }
#

You can specify it in any argument list, such as:
#
#  uint doSomething(Foo foo) { ... }
#

And the function will only accept an instance of a class implementing that
interface as its argument.

Ta da!  :D

-- Chris Sauls
January 24, 2005
Re: Interface as argument restriction
nail wrote:
> interface I {}
> class C : I {}
> 
> void foo(Object x, Object y)
> {
> uint idx1 = myArray[x.classinfo];
> uint idx2 = myArray[y.classinfo];
> ...
> }
> 
> int main()
> {
> I x = new C();
> I y = x;
> foo(x, y); // compile error
> // I can do following
> foo(cast(Object)x, cast(Object)y) // but it's too long :)
> return 0;
> }
> 
> That is my problem. I don't want use cast(Object) or make function in
> template, just be able to receive interfaces and classes, not basic types.
> Ideas?

IMO, this code should clearly work. The current language specs do not
provide for this, but that should be fixed.

Simple solution:
Make every interface reference implicitely cast-able to an Object reference.
Since every class inherits from Object and every interface reference
actually point to a class instance, this special rule would be safe. (Just
the question how the compiler would handle it)

More complex solution:
introduce a topmost interface in parallel to the topmost class Object. Then
Object could implement this interface, so every class can be handled via an
interface reference. (This solution probably is overkill...)
January 25, 2005
Re: Interface as argument restriction
Norbert Nemec wrote:
> nail wrote:
> 
>>That is my problem. I don't want use cast(Object) or make function in
>>template, just be able to receive interfaces and classes, not basic types.
>>Ideas?
> 
> IMO, this code should clearly work. The current language specs do not
> provide for this, but that should be fixed.

I'm not so sure: COM objects probably don't inherit the D Object class.

It really sucks, but implicitly casting to Object won't work.

 -- andy
January 25, 2005
Re: Interface as argument restriction
Andy Friesen wrote:
> Norbert Nemec wrote:
>> nail wrote:
>> 
>>>That is my problem. I don't want use cast(Object) or make function in
>>>template, just be able to receive interfaces and classes, not basic
>>>types. Ideas?
>> 
>> IMO, this code should clearly work. The current language specs do not
>> provide for this, but that should be fixed.
> 
> I'm not so sure: COM objects probably don't inherit the D Object class.

OK, that would call for what I called 'complex solution':

Define an interface called 'Interface' which defines the top of the
interface hierarchy. (Just like Object defines the top of the class
hierarchy)

Every Interface and every Object should automatically inherit that
interface.

That way, COM objects or possible other kinds objects of the future that do
not inherit from Object could still be handled via Interface.
January 26, 2005
Re: Interface as argument restriction
Norbert Nemec wrote:
> Andy Friesen wrote:
> 
>>Norbert Nemec wrote:
>>
>>>nail wrote:
>>>
>>>
>>>>That is my problem. I don't want use cast(Object) or make function in
>>>>template, just be able to receive interfaces and classes, not basic
>>>>types. Ideas?
>>>
>>>IMO, this code should clearly work. The current language specs do not
>>>provide for this, but that should be fixed.
>>
>>I'm not so sure: COM objects probably don't inherit the D Object class.
> 
> 
> OK, that would call for what I called 'complex solution':
> 
> Define an interface called 'Interface' which defines the top of the
> interface hierarchy. (Just like Object defines the top of the class
> hierarchy)
> 
> Every Interface and every Object should automatically inherit that
> interface.
> 
> That way, COM objects or possible other kinds objects of the future that do
> not inherit from Object could still be handled via Interface.
> 

the return of iunknown....

sorry my shift keys are broken atm, gotta reboot to fix that...

jason jasmin
Top | Discussion index | About this forum | D home