www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - How to convert this code: (is(ToString(t))

reply Pablo <piecuch.pawel gmail.com> writes:
This is part of the old dparse.d file:

private import std.string : ToString = toString ;

	char[] string()
	{
		static if(str)
		{
			static if(is(T == char[]))
				return t;
			else static if(is(ToString(t)))
				return ToString(t);
			else
				return "<<"~T.stringof~">>";
		}
		else
			return "<<"~T.stringof~">>";
	}

I have a problem with this part:  (is(ToString(t)) :
dparse.d(215): Error: function declaration without return type. 
(Note that constructors are always named 'this')

How to fix this code?

Regards
Pawel
May 08 2016
next sibling parent wobbles <grogan.colin gmail.com> writes:
On Sunday, 8 May 2016 at 18:55:58 UTC, Pablo wrote:
 This is part of the old dparse.d file:

 private import std.string : ToString = toString ;

 	char[] string()
 	{
 		static if(str)
 		{
 			static if(is(T == char[]))
 				return t;
 			else static if(is(ToString(t)))
 				return ToString(t);
 			else
 				return "<<"~T.stringof~">>";
 		}
 		else
 			return "<<"~T.stringof~">>";
 	}

 I have a problem with this part:  (is(ToString(t)) :
 dparse.d(215): Error: function declaration without return type. 
 (Note that constructors are always named 'this')

 How to fix this code?

 Regards
 Pawel
I think (THINK!!) you could use __traits(compiles, ToString(t)) in place of the is(ToString(t)). Unless all objects have an implicit toString? In which case it will always return true and be a useless check. On the phone and couldn't check properly...
May 08 2016
prev sibling parent reply Peter =?UTF-8?B?SMOkZ2dtYW4=?= <hagg.pet2 online.fi> writes:
On Sunday, 8 May 2016 at 18:55:58 UTC, Pablo wrote:
 This is part of the old dparse.d file:

 private import std.string : ToString = toString ;

 	char[] string()
 	{
 		static if(str)
 		{
 			static if(is(T == char[]))
 				return t;
 			else static if(is(ToString(t)))
 				return ToString(t);
 			else
 				return "<<"~T.stringof~">>";
 		}
 		else
 			return "<<"~T.stringof~">>";
 	}

 I have a problem with this part:  (is(ToString(t)) :
 dparse.d(215): Error: function declaration without return type. 
 (Note that constructors are always named 'this')

 How to fix this code?

 Regards
 Pawel
You must replace with "static if (is(typeof(to!string(t))));"
May 08 2016
parent Pablo <piecuch.pawel gmail.com> writes:
On Sunday, 8 May 2016 at 22:34:34 UTC, Peter Häggman wrote:
 On Sunday, 8 May 2016 at 18:55:58 UTC, Pablo wrote:
 This is part of the old dparse.d file:

 private import std.string : ToString = toString ;

 	char[] string()
 	{
 		static if(str)
 		{
 			static if(is(T == char[]))
 				return t;
 			else static if(is(ToString(t)))
 				return ToString(t);
 			else
 				return "<<"~T.stringof~">>";
 		}
 		else
 			return "<<"~T.stringof~">>";
 	}

 I have a problem with this part:  (is(ToString(t)) :
 dparse.d(215): Error: function declaration without return 
 type. (Note that constructors are always named 'this')

 How to fix this code?

 Regards
 Pawel
You must replace with "static if (is(typeof(to!string(t))));"
Thanks! Pawel
May 08 2016