www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Interface as argument restriction

reply nail <nail_member pathlink.com> writes:
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) ?
Jan 24 2005
parent reply Norbert Nemec <Norbert Nemec-online.de> writes:
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.)
Jan 24 2005
parent reply =?ISO-8859-1?Q?Anders_F_Bj=F6rklund?= <afb algonet.se> writes:
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
Jan 24 2005
parent reply nail <nail_member pathlink.com> writes:
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?
Jan 24 2005
next sibling parent Chris Sauls <Chris_member pathlink.com> writes:
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
Jan 24 2005
prev sibling parent reply Norbert Nemec <Norbert Nemec-online.de> writes:
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...)
Jan 24 2005
parent reply Andy Friesen <andy ikagames.com> writes:
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
Jan 24 2005
parent reply Norbert Nemec <Norbert Nemec-online.de> writes:
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.
Jan 25 2005
parent Jason Jasmin <zod269-d yahoo.com> writes:
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
Jan 26 2005