www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - std.conv.parse for user defined structs/objects

reply "Ilya Yaroshenko" <ilyayaroshenko gmail.com> writes:
Hello!

Can we introduce std.conv.parse for user defined structs/classes?

I think we can define inner static method:

-----------
struct UserStruct
{
     static typeof(this) parseImpl(Range)(Range range)
     if(...)
     {
         typeof(this) ret;
         ...
         return ret;
     }
}
-----------
or something like that.

And then use it in standard parse function.

The reason is common parse, formattedRead and io/stdio.readf  
functions that can be used for user defined types (and for 
voldemort user defined types too!).


If DIP is preferred in this case, please tell me about.

Best Regards,
Ilya
Dec 27 2013
next sibling parent reply "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
On Fri, Dec 27, 2013 at 07:38:59PM +0000, Ilya Yaroshenko wrote:
 Hello!
 
 Can we introduce std.conv.parse for user defined structs/classes?
 
 I think we can define inner static method:
 
 -----------
 struct UserStruct
 {
     static typeof(this) parseImpl(Range)(Range range)
     if(...)
     {
         typeof(this) ret;
         ...
         return ret;
     }
 }
 -----------
 or something like that.

I've been thinking about this too. It shouldn't be too hard to add this to std.conv (I'd call it "fromString", though, by analogy with "toString" -- "parseImpl" looks a bit ugly).
 And then use it in standard parse function.
 
 The reason is common parse, formattedRead and io/stdio.readf
 functions that can be used for user defined types (and for voldemort
 user defined types too!).
 
 
 If DIP is preferred in this case, please tell me about.

I generally support this idea, though it might be a good idea to discuss the exact design here first. T -- Food and laptops don't mix.
Dec 27 2013
parent Dmitry Olshansky <dmitry.olsh gmail.com> writes:
06-Jan-2014 08:19, H. S. Teoh пишет:
 On Fri, Dec 27, 2013 at 08:43:25PM +0000, Ilya Yaroshenko wrote:
 I've been thinking about this too. It shouldn't be too hard to add
 this to std.conv (I'd call it "fromString", though, by analogy with
 "toString" -- "parseImpl" looks a bit ugly).

Is "fromString" good in case of InputRange?

Good point. Maybe "parse"?

Parse from an input range... That's going to be tricky and inefficient.
 	class T {
 		static T parse(R)(R inputRange)
 			if (is(ElementType!R : dchar))
 		{
 			T result;
 			... // parse inputRange
 			return result;
 		}
 	}


 T

-- Dmitry Olshansky
Jan 06 2014
prev sibling next sibling parent "Ilya Yaroshenko" <ilyayaroshenko gmail.com> writes:
 I've been thinking about this too. It shouldn't be too hard to 
 add this
 to std.conv (I'd call it "fromString", though, by analogy with
 "toString" -- "parseImpl" looks a bit ugly).

Is "fromString" good in case of InputRange?
Dec 27 2013
prev sibling next sibling parent "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
On Fri, Dec 27, 2013 at 08:43:25PM +0000, Ilya Yaroshenko wrote:
 
I've been thinking about this too. It shouldn't be too hard to add
this to std.conv (I'd call it "fromString", though, by analogy with
"toString" -- "parseImpl" looks a bit ugly).

Is "fromString" good in case of InputRange?

Good point. Maybe "parse"? class T { static T parse(R)(R inputRange) if (is(ElementType!R : dchar)) { T result; ... // parse inputRange return result; } } T -- To err is human; to forgive is not our policy. -- Samuel Adler
Jan 05 2014
prev sibling parent Jacob Carlborg <doob me.com> writes:
On 2013-12-27 20:38, Ilya Yaroshenko wrote:
 Hello!

 Can we introduce std.conv.parse for user defined structs/classes?

 I think we can define inner static method:

 -----------
 struct UserStruct
 {
      static typeof(this) parseImpl(Range)(Range range)
      if(...)
      {
          typeof(this) ret;
          ...
          return ret;
      }
 }
 -----------
 or something like that.

 And then use it in standard parse function.

 The reason is common parse, formattedRead and io/stdio.readf functions
 that can be used for user defined types (and for voldemort user defined
 types too!).

Isn't this starting to look like serialization? -- /Jacob Carlborg
Jan 06 2014