www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Determining whether something is an interface?

reply Christopher Wright <dhasenan gmail.com> writes:
You can determine whether a type is a class:
is (T : Object)

There's no equivalent for interfaces. You can use the compiles trait:
__traits(compiles, class Something : T {})
Except you can't -- it only takes expressions, not declarations.

There are traits for abstract classes (interfaces aren't) and final 
classes; how about something for interfaces? Or should I just start 
using abstract classes instead?
Nov 12 2007
next sibling parent torhu <no spam.invalid> writes:
Christopher Wright wrote:
 You can determine whether a type is a class:
 is (T : Object)
 
 There's no equivalent for interfaces. You can use the compiles trait:
 __traits(compiles, class Something : T {})
 Except you can't -- it only takes expressions, not declarations.
 
 There are traits for abstract classes (interfaces aren't) and final 
 classes; how about something for interfaces? Or should I just start 
 using abstract classes instead?

Doesn't this work? It's legal syntax, so I suppose it does what you want. is(foo == interface)
Nov 12 2007
prev sibling parent reply Daniel Keep <daniel.keep.lists gmail.com> writes:
Christopher Wright wrote:
 You can determine whether a type is a class:
 is (T : Object)
 
 There's no equivalent for interfaces. You can use the compiles trait:
 __traits(compiles, class Something : T {})
 Except you can't -- it only takes expressions, not declarations.
 
 There are traits for abstract classes (interfaces aren't) and final
 classes; how about something for interfaces? Or should I just start
 using abstract classes instead?

Have you tried is( T == interface ) ? -- Daniel
Nov 12 2007
parent reply Christopher Wright <dhasenan gmail.com> writes:
Daniel Keep wrote:
 
 Christopher Wright wrote:
 You can determine whether a type is a class:
 is (T : Object)

 There's no equivalent for interfaces. You can use the compiles trait:
 __traits(compiles, class Something : T {})
 Except you can't -- it only takes expressions, not declarations.

 There are traits for abstract classes (interfaces aren't) and final
 classes; how about something for interfaces? Or should I just start
 using abstract classes instead?

Have you tried is( T == interface ) ? -- Daniel

No, I haven't. `interface` isn't a type, so it didn't occur to me to try that. Thank you, and thank torhu.
Nov 12 2007
parent BCS <ao pathlink.com> writes:
Reply to Christopher,

 Daniel Keep wrote:
 
 Christopher Wright wrote:
 
 You can determine whether a type is a class:
 is (T : Object)
 There's no equivalent for interfaces. You can use the compiles
 trait:
 __traits(compiles, class Something : T {})
 Except you can't -- it only takes expressions, not declarations.
 There are traits for abstract classes (interfaces aren't) and final
 classes; how about something for interfaces? Or should I just start
 using abstract classes instead?
 

-- Daniel

try that. Thank you, and thank torhu.

the full list of things you can do is at: http://www.digitalmars.com/d/1.0/expression.html#IsExpression
Nov 12 2007