www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Signature conditions

reply "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
I'm trying to write a parametrized class with a type parameter whose
existence depends on whether std.utf.decode() exists for that type.
What's the syntax to do this?

	class C(T)
		if ( /* ??? exists(decode(T)...?) */ )
	{ ... }

Thanks!


T

-- 
It is not the employer who pays the wages. Employers only handle the
money. It is the customer who pays the wages. -- Henry Ford
Feb 10 2012
parent reply David Nadlinger <see klickverbot.at> writes:
On 2/10/12 7:28 PM, H. S. Teoh wrote:
 I'm trying to write a parametrized class with a type parameter whose
 existence depends on whether std.utf.decode() exists for that type.
 What's the syntax to do this?

class(T) if (__traits(compiles, { std.utf.decode(T.init); } )) { … } David
Feb 10 2012
next sibling parent reply David Nadlinger <see klickverbot.at> writes:
On 2/10/12 7:35 PM, David Nadlinger wrote:
 class(T) if (__traits(compiles, { std.utf.decode(T.init); } )) { … }

(untested, you might have to return the value from the delegate to avoid an expression-without-effect error)
Feb 10 2012
next sibling parent Timon Gehr <timon.gehr gmx.ch> writes:
On 02/10/2012 07:50 PM, H. S. Teoh wrote:
 On Fri, Feb 10, 2012 at 07:36:38PM +0100, David Nadlinger wrote:
 On 2/10/12 7:35 PM, David Nadlinger wrote:
 class(T) if (__traits(compiles, { std.utf.decode(T.init); } )) { … }

(untested, you might have to return the value from the delegate to avoid an expression-without-effect error)

Works for me. Thanks!! On that note, I discovered that if you want signature constraints on a derived class, the syntax is unbearably ugly: class Derived(T) if (/* conditions */) : Base { ... } I would have expected the syntax to be like this instead: class Derived(T) : Base if (/* conditions */) { ... } But this doesn't compile. T

It makes more sense as it is. Consider the instantiation 'Derived!int' and the condition 'is(typeof(T)==class)'.
Feb 10 2012
prev sibling parent "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
On Fri, Feb 10, 2012 at 08:53:58PM +0100, Timon Gehr wrote:
 On 02/10/2012 07:50 PM, H. S. Teoh wrote:

On that note, I discovered that if you want signature constraints on
a derived class, the syntax is unbearably ugly:

	class Derived(T)
		if (/* conditions */)
		: Base
	{
		...
	}

I would have expected the syntax to be like this instead:

	class Derived(T) : Base
		if (/* conditions */)
	{
		...
	}


 It makes more sense as it is. Consider the instantiation
 'Derived!int' and the condition 'is(typeof(T)==class)'.

Well, to me, the "if" condition should apply to the entire class specification "class Derived(T) : Base", and not just to Derived(T). For example, something like this wouldn't be in a logical sequence: class Derived(T) if (is(Base!T ...)) : Base!T { ... } since you'll want the condition to restrict what values Base!T can take on. This would be more readable: class Derived(T) : Base!T if (is(Base!T ...)) { ... } T -- "I'm running Windows '98." "Yes." "My computer isn't working now." "Yes, you already said that." -- User-Friendly
Feb 10 2012
prev sibling parent "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
On Fri, Feb 10, 2012 at 07:36:38PM +0100, David Nadlinger wrote:
 On 2/10/12 7:35 PM, David Nadlinger wrote:
class(T) if (__traits(compiles, { std.utf.decode(T.init); } )) { … }

(untested, you might have to return the value from the delegate to avoid an expression-without-effect error)

Works for me. Thanks!! On that note, I discovered that if you want signature constraints on a derived class, the syntax is unbearably ugly: class Derived(T) if (/* conditions */) : Base { ... } I would have expected the syntax to be like this instead: class Derived(T) : Base if (/* conditions */) { ... } But this doesn't compile. T -- If Java had true garbage collection, most programs would delete themselves upon execution. -- Robert Sewell
Feb 10 2012