digitalmars.D.learn - Checking for possibility of implicit conversions
- H. S. Teoh (20/20) Mar 14 2012 How do I check if a given type T can be implicitly converted to some
- =?ISO-8859-1?Q?Alex_R=F8nne_Petersen?= (4/20) Mar 14 2012 http://dlang.org/phobos/std_traits.html#isImplicitlyConvertible
- Adam D. Ruppe (3/3) Mar 14 2012 if(is(T : S))
- H. S. Teoh (7/12) Mar 14 2012 Ahh, thanks. I keep forgetting what the various forms of is() do... sigh...
- Nick Sabalausky (8/17) Mar 14 2012 None of those three string types are implicitly convertable to each othe...
- Nick Sabalausky (3/8) Mar 14 2012 I think Andrei calls that a polysemous type.
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
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. Thttp://dlang.org/phobos/std_traits.html#isImplicitlyConvertible -- - Alex
Mar 14 2012
if(is(T : S)) http://dlang.org/expression.html#IsExpression
Mar 14 2012
On Wed, Mar 14, 2012 at 06:08:24PM +0100, Adam D. Ruppe wrote:if(is(T : S)) http://dlang.org/expression.html#IsExpressionAhh, 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
"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: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.if(is(T : S)) http://dlang.org/expression.html#IsExpressionAhh, 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.
Mar 14 2012
"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