Trying to add automatic radix detection in std.conv.parse

2014-07-25 Thread klb via Digitalmars-d-learn
hello, I'd like to test if it's possible to add an automatic 
radix detection in std.conv, parse.


I've added...

if (s.length >= 2)
{
if (s[0..2] == "0x" || s[0..2] == "0X")
{
uint radix = 16;
Source nbr = s[2..$];
return .parse!Target(nbr, radix);
}
else if (s[0..2] == "0b" || s[0..2] == "0B")
{
uint radix = 2;
Source nbr = s[2..$];
return .parse!Target(nbr, radix);
}
}


...to the first parse version. The (maybe `naive`) idea is to 
redirect to the right parse version if the prefixes are found.


But it seems to break the purity of format() because I get , when 
compiling phobos, the following errors:



std\utf.d(71): Error: pure function 'std.utf.UTFException.this' 
cannot call impure function 'std.str

ing.format!(char, uint).format'
std\utf.d(71): Error: safe function 'std.utf.UTFException.this' 
cannot call system function 'std.str

ing.format!(char, uint).format'
std\uuid.d(1536): Error: pure function 
'std.uuid.UUIDParsingException.this' cannot call impure funct

ion 'std.string.format!(char, string, string, uint).format'


What's wrong ?


Re: Converting a POD struct to a class at compile-time ?

2014-07-16 Thread Klb via Digitalmars-d-learn

On Wednesday, 16 July 2014 at 18:09:10 UTC, Adam D. Ruppe wrote:

On Wednesday, 16 July 2014 at 17:43:03 UTC, Klb wrote:


   auto names = __traits(allMembers, S);


Error: static variable _names_field_0 cannot be read at 
compile time.


The problem there is names is a regular runtime variable and 
mixins need to use compile time stuff. If you make that enum 
instead of auto, it'll probably work.


Unfortunately I can't encapsulate and alias. Behind the question 
stands another idea: I do something with an interface 
implementer, as the "something" is not compat. with structs the 
idea was to generate a class as a string, with the interface and 
its default method...




Re: Converting a POD struct to a class at compile-time ?

2014-07-16 Thread Klb via Digitalmars-d-learn

On Wednesday, 16 July 2014 at 18:09:10 UTC, Adam D. Ruppe wrote:

On Wednesday, 16 July 2014 at 17:43:03 UTC, Klb wrote:


   auto names = __traits(allMembers, S);


Error: static variable _names_field_0 cannot be read at 
compile time.


The problem there is names is a regular runtime variable and 
mixins need to use compile time stuff. If you make that enum 
instead of auto, it'll probably work.


Nice ! It works. :)



Converting a POD struct to a class at compile-time ?

2014-07-16 Thread Klb via Digitalmars-d-learn
Hello, I'd like to know if it's possible, using CTFE, mixin etc 
to convert a POD struct to a class at compile time.


I've reached the step where the string to mix-in is generated but 
I cant mix it:


-
import std.stdio;
import std.traits, std.typetuple;

static private template genClassFromStruct(S) if (is(S == struct) 
&(__traits(isPOD, S)))

{
auto values = S.init.tupleof;
auto types = typeid(typeof(values));
auto names = __traits(allMembers, S);

string genClassFromStruct()
{
string members;
foreach(int i,t; RepresentationTypeTuple!S)
{
members ~= t.stringof ~ " " ~ names[i] ~ ";\r";
}

return
"class c" ~ S.stringof ~
"{" ~ ";\r"~
members ~
"}";
}
}

struct foo{ int a; float b;}
//mixin( genClassFromStruct!foo );

void main(string args[])
{
foo Foo;
//auto c = new cfoo;

writeln( genClassFromStruct!foo );
}
-

The problem appends when un-commenting the mixin:

Error: static variable _names_field_0 cannot be read at compile 
time.


what is exactly stack stomp "-gx" new switch ?

2014-07-15 Thread Klb via Digitalmars-d-learn

...and any example where this switch will be usefull ?


Re: syntax for calling to with a getter as source argument

2014-07-14 Thread Klb via Digitalmars-d-learn

On Tuesday, 15 July 2014 at 00:19:15 UTC, Ali Çehreli wrote:

On 07/14/2014 05:10 PM, Ali Çehreli wrote:

> On 07/14/2014 04:04 PM, H. S. Teoh via Digitalmars-d-learn
wrote:
>
>  > On Mon, Jul 14, 2014 at 09:12:30PM +0000, Klb via
Digitalmars-d-learn
> wrote:
>  >> hello what is the right syntax for this:
>  >>
>  >> 
>  >> import std.stdio, std.conv;
>  >>
>  >> void main(string args[])
>  >> {
>  >>  ubyte[3] src = [0, 1, 2];
>  >>  string trg = "";
>  >>
>  >>  @property ubyte[3] srcAsProp(){return src;}
>  >>
>  >>  // Error: template std.conv.to cannot deduce
function from
> argument types
>  >>  // !(string)(ubyte[3]), candidates are:
>  >>  trg = to!(string)(srcAsProp());
>  >> }
>  >> 
>  >>
>  >> In a real-world application I'd use an intermediate value
but I'd like
>  >> to know If it's possible...The strange fact is that it
doesn't trig an
>  >> error if src is a dyn. array. (if you remove the 3 from
[3] then it
>  >> passes).
>  >
>  > You need to slice the static array:
>  >
>  > trg = to!string(srcAsProp()[]);
>
> There seems to be an attempt in Phobos to support it without
needing an
> explicit slice.
>
>  From std/phobos/conv.d:

I meant phobos/std/conv.d there.

> /*
>Converting static arrays forwards to their dynamic
counterparts.
>   */
> T toImpl(T, S)(ref S s)
>  if (isRawStaticArray!S)

Ok, I think I see now (after two minutes after posting it :) ) 
why it doesn't work. The overload above takes by-ref, which 
does not bind to rvalues in D.


And the problem is, what srcAsProp() returns is an rvalue 
because static arrays are value-types and they get copied.


> {
>  return toImpl!(T, typeof(s[0])[])(s);
> }
>
> @safe pure nothrow unittest
> {
>  char[4] test = ['a', 'b', 'c', 'd'];
>  static assert(!isInputRange!(Unqual!(char[4])));
>  assert(to!string(test) == test);
> }
>
> I don't know why that overload does not take care of OP's
case. (I am be
> completely off here. :) )

I hope I have it now. :p

Ali


Thanks for this accurate explanation. The problem is quite clear 
now.




syntax for calling to with a getter as source argument

2014-07-14 Thread Klb via Digitalmars-d-learn

hello what is the right syntax for this:


import std.stdio, std.conv;

void main(string args[])
{
ubyte[3] src = [0, 1, 2];
string trg = "";

@property ubyte[3] srcAsProp(){return src;}

// Error: template std.conv.to cannot deduce function from 
argument types

// !(string)(ubyte[3]), candidates are:
trg = to!(string)(srcAsProp());
}


In a real-world application I'd use an intermediate value but I'd 
like to know If it's possible...The strange fact is that it 
doesn't trig an error if src is a dyn. array. (if you remove the 
3 from [3] then it passes).