www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Struct toString works but not std.conv.to!string

reply =?UTF-8?B?Tm9yZGzDtnc=?= <per.nordlow gmail.com> writes:
I have defined a struct UTCOffset in

https://github.com/nordlow/justd/blob/master/datetime_ex.d

Everything works as desired except for

     import std.conv : to;
     assert(UTCOffset(+14, 0).to!string == "UTC+14:00");

which fails as

/usr/include/dmd/phobos/std/conv.d(293,14): Error: template 
instance isRawStaticArray!() does not match template declaration 
isRawStaticArray(T, A...)
datetime_ex.d(129,29): Error: cannot resolve type for 
UTCOffset(cast(ubyte)0u).this(cast(byte)14, 
cast(ubyte)0u).to!string

I don't understand what's wrong.
Oct 13 2015
next sibling parent reply Jonathan M Davis via Digitalmars-d-learn writes:
On Tuesday, October 13, 2015 21:07:07 Nordl÷w via Digitalmars-d-learn wrote:
 I have defined a struct UTCOffset in

 https://github.com/nordlow/justd/blob/master/datetime_ex.d

 Everything works as desired except for

      import std.conv : to;
      assert(UTCOffset(+14, 0).to!string == "UTC+14:00");

 which fails as

 /usr/include/dmd/phobos/std/conv.d(293,14): Error: template
 instance isRawStaticArray!() does not match template declaration
 isRawStaticArray(T, A...)
 datetime_ex.d(129,29): Error: cannot resolve type for
 UTCOffset(cast(ubyte)0u).this(cast(byte)14,
 cast(ubyte)0u).to!string

 I don't understand what's wrong.
Just glancing at your code, you've marked toString with property, which is kind of a weird thing to do, nd if we ever make property enforce that it's not called with parens, then that code won't work. So, you might try moving that property: to after toString and see if that fixes your problem. But given the error, my guess is that the problem relates to the fact that you templatized the constructor, which is often problematic, and whatever type introspection std.conv.to is doing could be choking on that. So, you should probably try making it so that the constructor isn't templatized and see if that fixes the problem. - Jonathan M Davis
Oct 13 2015
parent =?UTF-8?B?Tm9yZGzDtnc=?= <per.nordlow gmail.com> writes:
On Tuesday, 13 October 2015 at 21:50:54 UTC, Jonathan M Davis 
wrote:
 Just glancing at your code, you've marked toString with 
  property, which is kind of a weird thing to do, nd if we ever 
 make  property enforce that it's not called with parens, then 
 that code won't work. So, you might try moving that  property: 
 to after toString and see if that fixes your problem. But given 
 the error, my guess is that the problem relates to the fact 
 that you templatized the constructor, which is often 
 problematic, and whatever type introspection std.conv.to is 
 doing could be choking on that. So, you should probably try 
 making it so that the constructor isn't templatized and see if 
 that fixes the problem.

 - Jonathan M Davis
None of you advice helped. Fortunately I found a solution: If I move import std.conv : to; into the function scopes the problem goes away. Thanks, anyway, for you time.
Oct 13 2015
prev sibling parent reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 10/13/2015 02:07 PM, Nordl├Âw wrote:
 I have defined a struct UTCOffset in

 https://github.com/nordlow/justd/blob/master/datetime_ex.d

 Everything works as desired except for

      import std.conv : to;
      assert(UTCOffset(+14, 0).to!string == "UTC+14:00");

 which fails as

 /usr/include/dmd/phobos/std/conv.d(293,14): Error: template instance
 isRawStaticArray!() does not match template declaration
 isRawStaticArray(T, A...)
 datetime_ex.d(129,29): Error: cannot resolve type for
 UTCOffset(cast(ubyte)0u).this(cast(byte)14, cast(ubyte)0u).to!string

 I don't understand what's wrong.
Reduced with a workaround: struct UTCOffset { import std.conv : to; // Move to module scope to compile string toString() const { return "hello"; } } void main() { import std.conv : to; UTCOffset().to!string; } This is an issue that I know to be known. :) I think it is about private definitions (isRawStaticArray) of modules not working outside? or when in inner scopes? Something like that... Ali
Oct 13 2015
parent anonymous <anonymous example.com> writes:
On Tuesday, 13 October 2015 at 22:21:43 UTC, Ali Çehreli wrote:
 Reduced with a workaround:

 struct UTCOffset
 {
     import std.conv : to;    // Move to module scope to compile
This introduces UTCOffset.to as an alias to std.conv.to.
     string toString() const
     {
         return "hello";
     }
 }

 void main() {
     import std.conv : to;
This ends up being ignored, because UTCOffset has a member called `to`.
     UTCOffset().to!string;
This does not do call std.conv.to through UFCS. Instead, it calls UTCOffset's static alias of std.conv.to without an argument. That is: `UTCOffset().to!string;` = `UTCOffset.to!string;` = `std.conv.to!string;`
 }
Oct 13 2015