www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Checking for possibility of implicit conversions

reply "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
How do I check if a given type T can be implicitly converted to some
type S at compile-time? I'm trying to write a signature constraint for a
template function that should only be instantiated if the parameter type
can be implicitly assigned to some given type S.

	struct S(T) {
		T value;

		void setValue(S)(S newValue)
			if ( /* ???? */ )
		{
			value = newValue;
		}

I tried __traits(compiles, value = newValue) but it seems to always
return true, and then later the compiler errors out at the actual
assignment statement. I'd like to be able to catch this at the signature
constraint instead of inside the function body.


T

-- 
If you look at a thing nine hundred and ninety-nine times, you are
perfectly safe; if you look at it the thousandth time, you are in
frightful danger of seeing it for the first time. -- G. K. Chesterton
Mar 14 2012
next sibling parent =?ISO-8859-1?Q?Alex_R=F8nne_Petersen?= <xtzgzorex gmail.com> writes:
On 14-03-2012 18:07, H. S. Teoh wrote:
 How do I check if a given type T can be implicitly converted to some
 type S at compile-time? I'm trying to write a signature constraint for a
 template function that should only be instantiated if the parameter type
 can be implicitly assigned to some given type S.

 	struct S(T) {
 		T value;

 		void setValue(S)(S newValue)
 			if ( /* ???? */ )
 		{
 			value = newValue;
 		}

 I tried __traits(compiles, value = newValue) but it seems to always
 return true, and then later the compiler errors out at the actual
 assignment statement. I'd like to be able to catch this at the signature
 constraint instead of inside the function body.


 T
http://dlang.org/phobos/std_traits.html#isImplicitlyConvertible -- - Alex
Mar 14 2012
prev sibling parent reply "Adam D. Ruppe" <destructionator gmail.com> writes:
if(is(T : S))

http://dlang.org/expression.html#IsExpression


Mar 14 2012
parent reply "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
On Wed, Mar 14, 2012 at 06:08:24PM +0100, Adam D. Ruppe wrote:
 if(is(T : S))
 
 http://dlang.org/expression.html#IsExpression
 

Ahh, thanks. I keep forgetting what the various forms of is() do... sigh. Another question: is wstring assignable to dstring, or string to wstring or dstring? I'm finding that is(wstring:dstring) returns false. T -- There are four kinds of lies: lies, damn lies, and statistics.
Mar 14 2012
parent reply "Nick Sabalausky" <a a.a> writes:
"H. S. Teoh" <hsteoh quickfur.ath.cx> wrote in message 
news:mailman.662.1331746435.4860.digitalmars-d-learn puremagic.com...
 On Wed, Mar 14, 2012 at 06:08:24PM +0100, Adam D. Ruppe wrote:
 if(is(T : S))

 http://dlang.org/expression.html#IsExpression


Ahh, thanks. I keep forgetting what the various forms of is() do... sigh. Another question: is wstring assignable to dstring, or string to wstring or dstring? I'm finding that is(wstring:dstring) returns false.
None of those three string types are implicitly convertable to each other. What might make that a little confusing, though, is that string literals which are not suffixed with c/w/d are *not* necessarily string, but rather can acually *be* (ie, not "implicitly convertable to", but they actually *are*) either string/wstring/dstring depending on context. If it can't be inferred from context, *then* string is just simply assumed.
Mar 14 2012
parent "Nick Sabalausky" <a a.a> writes:
"Nick Sabalausky" <a a.a> wrote in message 
news:jjqp0n$1pga$1 digitalmars.com...
 What might make that a little confusing, though, is that string *literals* 
 which are not suffixed with c/w/d are *not* necessarily string, but rather 
 can acually *be* (ie, not "implicitly convertable to", but they actually 
 *are*) either string/wstring/dstring depending on context. If it can't be 
 inferred from context, *then* string is just simply assumed.
I think Andrei calls that a polysemous type.
Mar 14 2012