Thread overview
std.conv.parse for user defined structs/objects
Dec 27, 2013
Ilya Yaroshenko
Dec 27, 2013
H. S. Teoh
Dec 27, 2013
Ilya Yaroshenko
Jan 06, 2014
H. S. Teoh
Jan 06, 2014
Dmitry Olshansky
Jan 06, 2014
Jacob Carlborg
December 27, 2013
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
December 27, 2013
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.
December 27, 2013
> 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?
January 06, 2014
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
January 06, 2014
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
January 06, 2014
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