Re: get number of items in DList

2014-07-14 Thread pgtkda via Digitalmars-d-learn
On Friday, 11 July 2014 at 14:48:26 UTC, H. S. Teoh via 
Digitalmars-d-learn wrote:
On Fri, Jul 11, 2014 at 10:23:58AM -0300, Ary Borenszweig via 
Digitalmars-d-learn wrote:

On 7/11/14, 4:46 AM, bearophile wrote:
>pgtkda:
>
>>How can i get the number of items which are currently hold 
>>in a

>>DList?
>
>Try (walkLength is from std.range):
>
>mydList[].walkLength
>
>Bye,
>bearophile

So the doubly linked list doesn't know it's length? That seems 
a bit

inefficient...


It should be relatively simple to write a wrapper that *does* 
keep track

of length.

The main problem, though, comes from list splicing: given two 
arbitrary

points in the list, if you splice out the section of the list in
between, there's no easy way to know how many items lie in 
between, so
you'll have to walk the list to recompute the length then. 
Which sorta

defeats the purpose of having a linked list. :)


T


I just wanted to use a DList because you can easily add new items.
The walkLength property works perfectly, thank you.


Re: new properties for basic types

2014-07-14 Thread Philippe Sigaud via Digitalmars-d-learn
> Hmm.
> So how do I use stuff like this:
>
> template defaultInit(T)
> {
> static if (!is(typeof({ T v = void; })))// inout(U)
> @property T defaultInit(T v = T.init);
> else
> @property T defaultInit();
> }
>
> (this is from std.traits - ok, it's private, but anyway)

It should be invoked as `defaultInit!SomeType`


> Because I have seen nowhere anything like defaultInit!T (or T.defaultInit)
> and don't understand why here the attribute @property is used.
> Why does it make a difference, and how?

@property allows you to call a function without the parenthesis (), to
imitate a field in a struct or class.
In this particular case, I don't know what defaultInit is used for. It
seems to compile to a forward declaration of a function, but I don't
know what for.

I cannot find it on my copy of std.traits. What DMD version are you using?


Re: Compile time regex matching

2014-07-14 Thread Philippe Sigaud via Digitalmars-d-learn
On Mon, Jul 14, 2014 at 3:19 PM, Artur Skawina via Digitalmars-d-learn
 wrote:
> On 07/14/14 13:42, Philippe Sigaud via Digitalmars-d-learn wrote:
>> asserts get an entire copy of the parse tree. It's a bit wasteful, but
>> using 'immutable' directly does not work here, but this is OK:
>>
>> enum res = MyRegex("abcabcdefFOOBAR"); // compile-time parsing
>> immutable result = res; // to avoid copying the enum value everywhere
>
>static immutable result = MyRegex("abcabcdefFOOBAR"); // compile-time 
> parsing

Ah, static!


>
>> The static asserts then works (not the toString, though). Maybe
(snip diff)

I'll push that to the repo, thanks! I should sprinkle some const and
pure everywhere...

> [completely untested; just did a git clone and fixed the two
>  errors the compiler was whining about. Hmm, did pegged get
>  faster? Last time i tried (years ago) it was unusably slow;
>  right now, compiling your example, i didn't notice the extra
>  multi-second delay that was there then.]

It's still slower than some handcrafted parsers. At some time, I could
get it on par with std.regex (between 1.3 and 1.8 times slower), but
that meant losing some other properties. I have other parsing engines
partially implemented, with either a larger specter of grammars or
better speed (but not both!). I hope the coming holidays will let me
go back to it.


Re: is there a way to pause a program and resume with just a key press (or enter key)

2014-07-14 Thread H. S. Teoh via Digitalmars-d-learn
On Tue, Jul 15, 2014 at 02:49:55AM +, WhatMeWorry via Digitalmars-d-learn 
wrote:
> Sorry if this is an incredibly naive question.
> 
> I prefer to pragmatically pause my programs periodically so that I can
> peruse output statements. Ideally, I'd like to continue by just hitting any
> old key. My feeble attempt below requires I enter at least one character and
> then the enter key.
> 
> char ignore;
> writeln("Enter to continue");
> readf(" %s", &ignore);
> 
> Is there a way to continue with any old key press? or just the enter key?
[...]

I don't know about Windows, but on Linux, you can just press ctrl-s and
ctrl-q to pause/resume the console. (This is a Linux terminal function,
not specific to D.)


T

-- 
Ruby is essentially Perl minus Wall.


is there a way to pause a program and resume with just a key press (or enter key)

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

Sorry if this is an incredibly naive question.

I prefer to pragmatically pause my programs periodically so that 
I can peruse output statements. Ideally, I'd like to continue by 
just hitting any old key. My feeble attempt below requires I 
enter at least one character and then the enter key.


char ignore;
writeln("Enter to continue");
readf(" %s", &ignore);

Is there a way to continue with any old key press? or just the 
enter key?


Thanks in advance.


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 +, 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.




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

2014-07-14 Thread Ali Çehreli via Digitalmars-d-learn

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 +, 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



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

2014-07-14 Thread Ali Çehreli via Digitalmars-d-learn

On 07/14/2014 04:04 PM, H. S. Teoh via Digitalmars-d-learn wrote:

> On Mon, Jul 14, 2014 at 09:12:30PM +, 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:

/*
  Converting static arrays forwards to their dynamic counterparts.
 */
T toImpl(T, S)(ref S s)
if (isRawStaticArray!S)
{
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. :) )


Ali



Re: Generating Strings with Random Contents

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

Nordlöw:


https://github.com/nordlow/justd/blob/master/random_ex.d#L53


Isn't @trusted mostly for small parts of Phobos code? I suggest 
to avoid using @trusted in most cases.


Bye,
bearophile


Re: Generating Strings with Random Contents

2014-07-14 Thread Nordlöw

On Monday, 14 July 2014 at 22:45:29 UTC, Nordlöw wrote:
So I guess we need something more than just isValidCodePoint 
right?


Here's a first try:

https://github.com/nordlow/justd/blob/master/random_ex.d#L53


Re: Generating Strings with Random Contents

2014-07-14 Thread Brad Anderson via Digitalmars-d-learn

On Monday, 14 July 2014 at 22:32:25 UTC, bearophile wrote:

Brad Anderson:


Alternative:

randomSample(lowercase, 10, lowercase.length).writeln;


From randomSample docs:

Selects a random subsample out of r, containing exactly n 
elements. The order of elements is the same as in the original 
range.<


Bye,
bearophile


Hmm, good catch. Not the behavior I expected.


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

2014-07-14 Thread H. S. Teoh via Digitalmars-d-learn
On Mon, Jul 14, 2014 at 09:12:30PM +, 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()[]);

Or, if you like to use UFCS (Uniform Function Call Syntax):

trg = srcAsProp()[].to!string;

You might want to file an enhancement request to improve the error
message, though -- it's not very helpful as it stands.


T

-- 
Trying to define yourself is like trying to bite your own teeth. -- Alan Watts


Re: Generating Strings with Random Contents

2014-07-14 Thread Nordlöw

On Monday, 14 July 2014 at 22:39:15 UTC, bearophile wrote:
Several combinations of unicode chars are not meaningful/valid 
(like pairs of ligatures). Any thing that has to work correctly 
with Unicode is complex.


So I guess we need something more than just isValidCodePoint 
right?


Re: Generating Strings with Random Contents

2014-07-14 Thread Nordlöw

On Monday, 14 July 2014 at 22:39:08 UTC, Nordlöw wrote:

might be were to start.


Is it really this simple?

bool isValidCodePoint(dchar c)
{
return c < 0xD800 || (c >= 0xE000 && c < 0x11);
}


Re: Generating Strings with Random Contents

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

Nordlöw:

I believe defining a complete random sampling of all code units 
in dchar is a good start right? This can then be reused to 
lazily convert while filling in a string and wstring.


Several combinations of unicode chars are not meaningful/valid 
(like pairs of ligatures). Any thing that has to work correctly 
with Unicode is complex.


Bye,
bearophile


Re: Generating Strings with Random Contents

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

Nordlöw:

I was specifically interested in something that exercises 
(random samples) potentially _all_ code points for string, 
wstring and dstring (all code units that is).


That's harder. Generating all uints and then testing if it's a 
Unicode dchar seems possible.


Bye,
bearophile


Re: Generating Strings with Random Contents

2014-07-14 Thread Nordlöw

On Monday, 14 July 2014 at 22:35:59 UTC, Nordlöw wrote:

On Monday, 14 July 2014 at 22:32:51 UTC, Nordlöw wrote:

I believe defining a complete random sampling of all code units 
in dchar is a good start right? This can then be reused to 
lazily convert while filling in a string and wstring.


isValidCodePoint()

at

http://dlang.org/phobos/std_encoding.html

might be were to start.


Re: Generating Strings with Random Contents

2014-07-14 Thread Nordlöw

On Monday, 14 July 2014 at 22:32:51 UTC, Nordlöw wrote:

I believe defining a complete random sampling of all code units 
in dchar is a good start right? This can then be reused to lazily 
convert while filling in a string and wstring.


Re: Generating Strings with Random Contents

2014-07-14 Thread Nordlöw

On Monday, 14 July 2014 at 22:21:36 UTC, bearophile wrote:

Nordlöw:

Is there a natural way of generating/filling a 
string/wstring/dstring of a specific length with random 
contents?


Do you mean something like this?


import std.stdio, std.random, std.ascii, std.range, std.conv;

string genRandomString(in size_t len) {
return len
   .iota
   .map!(_ => lowercase[uniform(0, $)])
   .text;
}

void main() {
import std.stdio;

10.genRandomString.writeln;
}


Bye,
bearophile


I was specifically interested in something that exercises (random 
samples) potentially _all_ code points for string, wstring and 
dstring (all code units that is).


Re: Generating Strings with Random Contents

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

Brad Anderson:


Alternative:

randomSample(lowercase, 10, lowercase.length).writeln;


From randomSample docs:

Selects a random subsample out of r, containing exactly n 
elements. The order of elements is the same as in the original 
range.<


Bye,
bearophile


Re: DUB git master hang

2014-07-14 Thread Nordlöw

On Monday, 14 July 2014 at 22:27:48 UTC, Nordlöw wrote:

and then it hangs with same behaviour.


It finally got through...hmm maybe I'm on a slow 3g-network 
currently...


Re: DUB git master hang

2014-07-14 Thread Nordlöw

On Monday, 14 July 2014 at 22:10:01 UTC, Nordlöw wrote:

On Monday, 14 July 2014 at 22:08:58 UTC, Nordlöw wrote:

dub upgrade also hangs.


also at 12 percent cpu usage.


After removal of ~/.dub I did

du[per:/home/per/justd] master(+13/-4,1) ± dub
Fetching logger 0.1.0 (getting selected version)...
Placing logger 0.1.0 to /home/per/.dub/packages/...
Fetching ae-graphics 0.0.2 (getting selected version)...
Placing ae-graphics 0.0.2 to /home/per/.dub/packages/...
Fetching vibe-d ~master (getting selected version)...
Placing vibe-d ~master to /home/per/.dub/packages/...
Fetching libevent ~master (getting selected version)...
Placing libevent ~master to /home/per/.dub/packages/...
Fetching derelict-util 1.0.2 (getting selected version)...
Placing derelict-util 1.0.2 to /home/per/.dub/packages/...
Fetching derelict-assimp3 1.0.0 (getting selected version)...
Placing derelict-assimp3 1.0.0 to /home/per/.dub/packages/...
Fetching openssl ~master (getting selected version)...
Placing openssl ~master to /home/per/.dub/packages/...
Fetching libev ~master (getting selected version)...
Placing libev ~master to /home/per/.dub/packages/...
Fetching gfm ~master (getting selected version)...
Placing gfm ~master to /home/per/.dub/packages/...
Fetching derelict-fi 1.0.0 (getting selected version)...
Placing derelict-fi 1.0.0 to /home/per/.dub/packages/...
Fetching derelict-sdl2 1.2.3 (getting selected version)...
Placing derelict-sdl2 1.2.3 to /home/per/.dub/packages/...
Fetching derelict-enet 1.3.10 (getting selected version)...
Placing derelict-enet 1.3.10 to /home/per/.dub/packages/...
Fetching derelict-gl3 1.0.3 (getting selected version)...
Placing derelict-gl3 1.0.3 to /home/per/.dub/packages/...

and then it hangs with same behaviour.


Re: Generating Strings with Random Contents

2014-07-14 Thread Brad Anderson via Digitalmars-d-learn

On Monday, 14 July 2014 at 22:27:57 UTC, Brad Anderson wrote:


Alternative:

randomSample(lowercase, 10, lowercase.length).writeln;


std.ascii should really be using std.encoding.AsciiString. Then
that length wouldn't be necessary.


Re: Generating Strings with Random Contents

2014-07-14 Thread Brad Anderson via Digitalmars-d-learn

On Monday, 14 July 2014 at 22:21:36 UTC, bearophile wrote:

Nordlöw:

Is there a natural way of generating/filling a 
string/wstring/dstring of a specific length with random 
contents?


Do you mean something like this?


import std.stdio, std.random, std.ascii, std.range, std.conv;

string genRandomString(in size_t len) {
return len
   .iota
   .map!(_ => lowercase[uniform(0, $)])
   .text;
}

void main() {
import std.stdio;

10.genRandomString.writeln;
}


Bye,
bearophile


Alternative:

randomSample(lowercase, 10, lowercase.length).writeln;


Re: Generating Strings with Random Contents

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

Nordlöw:

Is there a natural way of generating/filling a 
string/wstring/dstring of a specific length with random 
contents?


Do you mean something like this?


import std.stdio, std.random, std.ascii, std.range, std.conv;

string genRandomString(in size_t len) {
return len
   .iota
   .map!(_ => lowercase[uniform(0, $)])
   .text;
}

void main() {
import std.stdio;

10.genRandomString.writeln;
}


Bye,
bearophile


Generating Strings with Random Contents

2014-07-14 Thread Nordlöw
Is there a natural way of generating/filling a 
string/wstring/dstring of a specific length with random contents?


Re: DUB git master hang

2014-07-14 Thread Nordlöw

On Monday, 14 July 2014 at 22:08:58 UTC, Nordlöw wrote:

dub upgrade also hangs.


also at 12 percent cpu usage.


Re: DUB git master hang

2014-07-14 Thread Nordlöw

On Monday, 14 July 2014 at 21:50:51 UTC, Nordlöw wrote:

My dub built from git master has suddenly started to hang on


dub upgrade also hangs.


Re: Undo struct slicing by type-punning

2014-07-14 Thread Ali Çehreli via Digitalmars-d-learn

On 07/14/2014 02:34 PM, ponce wrote:

> On Monday, 14 July 2014 at 18:43:36 UTC, Ali Çehreli wrote:
>> On 07/14/2014 10:35 AM, ponce wrote:
>>
>> > Ok, solved it, I just use pointer casts and it seems to work
>> when the
>> > struct is sliced.
>>
>> I think there is a terminology issue here. Slicing cannot be undone;
>> once the object is sliced, the non-A parts are gone.
>>
>
> Well they are not really gone if the struct is passed by-ref,

Oh, it's not slicing in that case... but wait! There is a problem here. :)

In C++, both structs and classes are value types and they both support 
inheritance. To observe slicing in C++, one needs pass-by-value and 
inheritance, which is available by structs and classes.


In D, structs don't support inheritance and classes don't support 
pass-by-value. However... Enter 'alias this' and we have D's version of 
slicing.


> you can recover the "gone" parts: http://dpaste.dzfl.pl/d64863fd4c6d

Aggreed but in D's case the non-A parts are not gone; as long as A 
lives, we know that B is alive. This is different from C++ where due to 
the necessary pass-by-value, they are truly gone (the bits may still be 
there but a C++ code should not do as D does).


Ali



Re: DUB git master hang

2014-07-14 Thread Nordlöw

On Monday, 14 July 2014 at 21:47:20 UTC, Nordlöw wrote:

My dub built from git master has suddenly started to hang on


During hang:

- dub CPU system usage is about 12 percent
- No dmd process currently activate
- End of output from strace dub:

lstat("source/app.d", {st_mode=S_IFREG|0664, st_size=9211, ...})
= 0
lstat("src/", 0x7fff0b0672a0)   = -1 ENOENT (No such file
or directory)
lstat("views", {st_mode=S_IFDIR|0775, st_size=0, ...}) = 0
lstat("source/", {st_mode=S_IFDIR|0775, st_size=22, ...}) = 0
lstat("source/app.d", {st_mode=S_IFREG|0664, st_size=9211, ...})
= 0
lstat("src/", 0x7fff0b0672a0)   = -1 ENOENT (No such file
or directory)
lstat("views", {st_mode=S_IFDIR|0775, st_size=0, ...}) = 0
lstat("source/", {st_mode=S_IFDIR|0775, st_size=22, ...}) = 0
lstat("source/app.d", {st_mode=S_IFREG|0664, st_size=9211, ...})
= 0
lstat("src/", 0x7fff0b0672a0)   = -1 ENOENT (No such file
or directory)
lstat("views", {st_mode=S_IFDIR|0775, st_size=0, ...}) = 0
lstat("source/", {st_mode=S_IFDIR|0775, st_size=22, ...}) = 0
lstat("source/app.d", {st_mode=S_IFREG|0664, st_size=9211, ...})
= 0
lstat("src/", 0x7fff0b0672a0)   = -1 ENOENT (No such file
or directory)
lstat("views", {st_mode=S_IFDIR|0775, st_size=0, ...}) = 0
lstat("source/", {st_mode=S_IFDIR|0775, st_size=22, ...}) = 0
lstat("source/app.d", {st_mode=S_IFREG|0664, st_size=9211, ...})
= 0
lstat("src/", 0x7fff0b0672a0)   = -1 ENOENT (No such file
or directory)
lstat("views", {st_mode=S_IFDIR|0775, st_size=0, ...}) = 0
lstat("source/", {st_mode=S_IFDIR|0775, st_size=22, ...}) = 0
lstat("source/app.d", {st_mode=S_IFREG|0664, st_size=9211, ...})
= 0
lstat("src/", 0x7fff0b0672a0)   = -1 ENOENT (No such file
or directory)
lstat("views", {st_mode=S_IFDIR|0775, st_size=0, ...}) = 0
lstat("source/", {st_mode=S_IFDIR|0775, st_size=22, ...}) = 0
lstat("source/app.d", {st_mode=S_IFREG|0664, st_size=9211, ...})
= 0
lstat("src/", 0x7fff0b0672a0)   = -1 ENOENT (No such file
or directory)
lstat("views", {st_mode=S_IFDIR|0775, st_size=0, ...}) = 0
lstat("source/", {st_mode=S_IFDIR|0775, st_size=22, ...}) = 0
lstat("source/app.d", {st_mode=S_IFREG|0664, st_size=9211, ...})
= 0
lstat("src/", 0x7fff0b0672a0)   = -1 ENOENT (No such file
or directory)
lstat("views", {st_mode=S_IFDIR|0775, st_size=0, ...}) = 0
lstat("source/", {st_mode=S_IFDIR|0775, st_size=22, ...}) = 0
lstat("source/app.d", {st_mode=S_IFREG|0664, st_size=9211, ...})
= 0
lstat("src/", 0x7fff0b0672a0)   = -1 ENOENT (No such file
or directory)
^R
--- SIGWINCH {si_signo=SIGWINCH, si_code=SI_KERNEL} ---
--- SIGWINCH {si_signo=SIGWINCH, si_code=SI_KERNEL} ---


DUB git master hang

2014-07-14 Thread Nordlöw
My dub built from git master has suddenly started to hang on most 
commands in my project.


What to do?

Is there some cleanup I can try?

/Per


Re: Undo struct slicing by type-punning

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

On Monday, 14 July 2014 at 18:43:36 UTC, Ali Çehreli wrote:
It is guaranteed by the language spec that yes, myFunction() 
takes an A by reference. However, you can't know where that A 
is coming from; so, the safety of that cast is up to you. 
Consider:


void foo(A a) // <-- Already sliced
{
myFunction(a);// <-- Will perform invalid cast
}



Indeed, the code I port do that because struct have a type-tag.



Re: Undo struct slicing by type-punning

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

On Monday, 14 July 2014 at 18:43:36 UTC, Ali Çehreli wrote:

On 07/14/2014 10:35 AM, ponce wrote:

> Ok, solved it, I just use pointer casts and it seems to work
when the
> struct is sliced.

I think there is a terminology issue here. Slicing cannot be 
undone; once the object is sliced, the non-A parts are gone.




Well they are not really gone if the struct is passed by-ref, you 
can recover the "gone" parts: http://dpaste.dzfl.pl/d64863fd4c6d


Re: DMDScript

2014-07-14 Thread Jason King via Digitalmars-d-learn


My idea is to use (at least test) DMDScript for server side JS.


I don't mean to sound like a D-hater here, but V8 has had about 2 
years more work on it than DMDScript.  At one time they were. 
IIRC, quite close performance-wise but lots of work and support 
by Google have probably levered past the advantages of being 
coded in D.  My 2c only.


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).


Re: reference to delegates and garbage collection

2014-07-14 Thread Adam D. Ruppe via Digitalmars-d-learn
I'm just guessing, but it looks to me that the delegate's pointer 
might be on the stack there and isn't overwritten when the one 
function returns, so the gc still thinks there might be an active 
pointer to it.


Re: Undo struct slicing by type-punning

2014-07-14 Thread Ali Çehreli via Digitalmars-d-learn

On 07/14/2014 10:35 AM, ponce wrote:

> Ok, solved it, I just use pointer casts and it seems to work when the
> struct is sliced.

I think there is a terminology issue here. Slicing cannot be undone; 
once the object is sliced, the non-A parts are gone.


> On Monday, 14 July 2014 at 13:23:57 UTC, ponce wrote:
>> Hi,
>>
>> I am porting C++ code that undo "Object Slicing" by casting
>> const-references:
>> http://en.wikipedia.org/wiki/Object_slicing
>>
>>
>> My translation in D Code
>> 
>>
>> struct A
>> {
>>  // stuff
>> }
>>
>> struct B
>> {
>>   A a;
>>   alias a this;
>>  // stuff
>> }
>>
>> void myFunction(ref const(A) a)
>> {
>> if ()
>> {
>> // here goes the converstion to a B reference
>> myFunction2(*cast(const(B)*)(&a)); // using pointer to do
>> type-punning
>> }
>> }
>>
>> 
>>
>>
>> To do this, I need to by guaranteed an object passed through ref
>> const(A) is never, ever passed by value. Is it safe to assume?

It is guaranteed by the language spec that yes, myFunction() takes an A 
by reference. However, you can't know where that A is coming from; so, 
the safety of that cast is up to you. Consider:


void foo(A a) // <-- Already sliced
{
myFunction(a);// <-- Will perform invalid cast
}

Ali



reference to delegates and garbage collection

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

Hello everybody. I am new to D, and I am trying to familiarize
with all the new (to me) features.
I have a question about a strange behavior I cannot understand.
(In case it matters, I am using gdc 2.065 for Windows, but the
same happens with dmd).

Example code :

class A {   
void delegate() del;

~this() { writeln("A Destructor called"); }   
}

class B {   
void fun() {}   
}

A makeA() { 
auto a = new A();
a.del = &(new B().fun);
return a;
}

void main() {

auto a = makeA();
 /* Magic line goes here, see below */
a = null;

foreach(i; 0..10) {
writeln("Collecting");
GC.collect();   
}

writeln("End");   
}

If I run this code, the instance of A is of course collected at
the first call to GC.collect().
But if I call the delegate (i.e. a.del() ) in the "magic line",
the instance is not destroyed until the end of the main. If I
"wrap" the call to a.del() in another function or method, the
instance is collected.

Can somebody explain why this happens? Is it just a strangeness
of the GC? Does calling the delegate keep a reference to a in the
current scope? Am I going crazy? I spent about half an hour
looking for a non-existent memory leak in my program because of
this behavior, so I'd like to have an explanation.

Thanks in advance!


Re: Implement Interface "dynamically"

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

http://dlang.org/phobos/std_typecons.html#.BlackHole
http://dlang.org/phobos/std_typecons.html#.WhiteHole
http://dlang.org/phobos/std_typecons.html#.AutoImplement

?


Re: Implement Interface "dynamically"

2014-07-14 Thread Gary Willoughby via Digitalmars-d-learn

There's some handy refection stuff in there too:
https://github.com/nomad-software/dunit/blob/master/source/dunit/reflection.d


Re: Implement Interface "dynamically"

2014-07-14 Thread Gary Willoughby via Digitalmars-d-learn

On Monday, 14 July 2014 at 14:45:01 UTC, Frustrated wrote:
Is there a way to take an interface and implement it 
generically? e.g., All functions are implemented either as 
throw and/or return defaults and properties are implemented as 
getter/setters.


This is for mocking up so I a simple way to create a class 
based off only the interface.


Essentially something similar to whitehole and blackhole except 
properties are useable.


In fact, I suppose it would be nice to have something analogous 
to them except properties are implemented. Then one could do


Blackhole!(Bluehole!C)

where Bluehole implements properties as getters and setters.


You can maybe get some ideas from this mocking code i wrote:
https://github.com/nomad-software/dunit/blob/master/source/dunit/mockable.d


Re: Undo struct slicing by type-punning

2014-07-14 Thread ponce via Digitalmars-d-learn
Ok, solved it, I just use pointer casts and it seems to work when 
the struct is sliced.



On Monday, 14 July 2014 at 13:23:57 UTC, ponce wrote:

Hi,

I am porting C++ code that undo "Object Slicing" by casting 
const-references:

http://en.wikipedia.org/wiki/Object_slicing


My translation in D Code


struct A
{
 // stuff
}

struct B
{
  A a;
  alias a this;
 // stuff
}

void myFunction(ref const(A) a)
{
if ()
{
// here goes the converstion to a B reference
myFunction2(*cast(const(B)*)(&a)); // using pointer to 
do type-punning

}
}




To do this, I need to by guaranteed an object passed through 
ref const(A) is never, ever passed by value. Is it safe to 
assume?




Re: SImple C++ code to D

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

Alexandre:


void InjectData(T)(ref T BaseSrc, string data)
{
memcpy(&BaseSrc, data.ptr, data.length);
}

It's possible to improve this function ?


You can add some modifiers (like @nogc for dmd 2.066), and the 
name of D functions starts with a lower case.


Bye,
bearophile


Re: SImple C++ code to D

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

void InjectData(T)(ref T BaseSrc, string data)
{
memcpy(&BaseSrc, data.ptr, data.length);
}

It's possible to improve this function ?

On Monday, 14 July 2014 at 15:45:19 UTC, bearophile wrote:

Alexandre:

Look the complete code: 
https://gist.github.com/bencz/3576dfc8a217a34c05a9


I know, has several things that can be improved


memcpy(&dosh.e_magic, "MZ".ptr, 2);
memcpy(&peh.Signature, "PE\0\0".ptr, 4);
memcpy(scth[1].Name.ptr, ".idata".ptr, 6);
memcpy(scth[2].Name.ptr, ".data".ptr, 5);
memcpy(&image[0x428], x"3820".ptr, 2);
memcpy(&image[0x430], x"3820".ptr, 2);
memcpy(&image[0x43a], "printf".ptr, 6);
memcpy(&image[0x448], "msvcrt.dll".ptr, 10);
memcpy(&image[0x201], x"00304000".ptr, 4);
memcpy(&image[0x207], x"30204000".ptr, 4);
memcpy(&image[0x600], "hello\n".ptr, 6);

Instead of this very bug-prone code, write yourself a little 
function to perform this more safely.


Bye,
bearophile




Re: SImple C++ code to D

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

Alexandre:

Look the complete code: 
https://gist.github.com/bencz/3576dfc8a217a34c05a9


I know, has several things that can be improved


memcpy(&dosh.e_magic, "MZ".ptr, 2);
memcpy(&peh.Signature, "PE\0\0".ptr, 4);
memcpy(scth[1].Name.ptr, ".idata".ptr, 6);
memcpy(scth[2].Name.ptr, ".data".ptr, 5);
memcpy(&image[0x428], x"3820".ptr, 2);
memcpy(&image[0x430], x"3820".ptr, 2);
memcpy(&image[0x43a], "printf".ptr, 6);
memcpy(&image[0x448], "msvcrt.dll".ptr, 10);
memcpy(&image[0x201], x"00304000".ptr, 4);
memcpy(&image[0x207], x"30204000".ptr, 4);
memcpy(&image[0x600], "hello\n".ptr, 6);

Instead of this very bug-prone code, write yourself a little 
function to perform this more safely.


Bye,
bearophile


Re: SImple C++ code to D

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

Soory, I not understand, why lose the optlink ?
Read the libs is simple, but, the linker do the brute force!

On Monday, 14 July 2014 at 15:17:06 UTC, Jason King wrote:

On Monday, 14 July 2014 at 14:50:36 UTC, Alexandre wrote:

Yes yes, I did it, I used the anonymous type

Look the complete code: 
https://gist.github.com/bencz/3576dfc8a217a34c05a9


I know, has several things that can be improved


Now that you've done that, can you build us a linker that reads 
COFF libs so we can lose optlink:)




Re: SImple C++ code to D

2014-07-14 Thread Jason King via Digitalmars-d-learn

On Monday, 14 July 2014 at 14:50:36 UTC, Alexandre wrote:

Yes yes, I did it, I used the anonymous type

Look the complete code: 
https://gist.github.com/bencz/3576dfc8a217a34c05a9


I know, has several things that can be improved


Now that you've done that, can you build us a linker that reads 
COFF libs so we can lose optlink:)


Re: SImple C++ code to D

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

Yes yes, I did it, I used the anonymous type

Look the complete code: 
https://gist.github.com/bencz/3576dfc8a217a34c05a9


I know, has several things that can be improved


Implement Interface "dynamically"

2014-07-14 Thread Frustrated via Digitalmars-d-learn
Is there a way to take an interface and implement it generically? 
e.g., All functions are implemented either as throw and/or return 
defaults and properties are implemented as getter/setters.


This is for mocking up so I a simple way to create a class based 
off only the interface.


Essentially something similar to whitehole and blackhole except 
properties are useable.


In fact, I suppose it would be nice to have something analogous 
to them except properties are implemented. Then one could do


Blackhole!(Bluehole!C)

where Bluehole implements properties as getters and setters.


Re: SImple C++ code to D

2014-07-14 Thread bearophile via Digitalmars-d-learn
Generally in D field names start with a lowercase (unless you 
need them with uppercase).


And user defined type names start with an upper case. This is 
useful, because if you write:


scth[0].Misc.virtualSize = 15;

You see immediately that Misc is not a value but a type. So it 
can't work.


Bye,
bearophile


Re: SImple C++ code to D

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

Alexandre:


BYTE[8] Name;


Generally in D field names start with a lowercase (unless you 
need them with uppercase).




Btw, my problem is, how to acess the union elements ?

I try this:
//...
scth[0].Misc.VirtualSize = 15;
//...

But, the compiler return that error:
main.d(151): Error: need 'this' for 'VirtualSize' of type 'uint'


The error message is not the best. It is saying you are not 
accessing data, just its definition. So you need to instantiate 
the union:


struct Foo {
ubyte[8] name;
union Bar {
ushort physicalAddress, virtualSize;
}
Bar b;
}

void main() {
Foo f;
f.b.physicalAddress = 10;
}


Or use an anonymous one:

struct Foo {
ubyte[8] name;
union {
ushort physicalAddress, virtualSize;
}
}

void main() {
Foo f;
f.physicalAddress = 10;
}

Bye,
bearophile


Re: SImple C++ code to D

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

bearophile, Thanks for all help!

As I said, I'm coming from C # and C + +, I need to learn 
"tricks" of D language...

'm reading this book: http://ddili.org/ders/d.en/

I have a struct with union...
struct IMAGE_SECTION_HEADER
{
BYTE[8] Name;
union Misc
{
DWORD PhysicalAddress,
VirtualSize;
}
DWORD VirtualAddress,
SizeOfRawData,
PointerToRawData,
PointerToRelocations,
PointerToLinenumbers;
WORD NumberOfRelocations,
NumberOfLinenumbers;
DWORD Characteristics;
}

( the  identation is wrong here... )
Btw, my problem is, how to acess the union elements ?

I try this:
//...
scth[0].Misc.VirtualSize = 15;
//...

But, the compiler return that error:
main.d(151): Error: need 'this' for 'VirtualSize' of type 'uint'

On Monday, 14 July 2014 at 13:00:21 UTC, bearophile wrote:

Alexandre:


I get a lot of problens, to convert 'strings' to UCHAR... :/


I suggest you to take a look at the D docs and understand what 
D fixed-sized arrays are, dynamic arrays, and strings (that are 
dynamic arrays).


Bye,
bearophile




Undo struct slicing by type-punning

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

Hi,

I am porting C++ code that undo "Object Slicing" by casting 
const-references:

http://en.wikipedia.org/wiki/Object_slicing


My translation in D Code


struct A
{
 // stuff
}

struct B
{
  A a;
  alias a this;
 // stuff
}

void myFunction(ref const(A) a)
{
if ()
{
// here goes the converstion to a B reference
myFunction2(*cast(const(B)*)(&a)); // using pointer to do 
type-punning

}
}




To do this, I need to by guaranteed an object passed through ref 
const(A) is never, ever passed by value. Is it safe to assume?


Re: Compile time regex matching

2014-07-14 Thread Artur Skawina via Digitalmars-d-learn
On 07/14/14 13:42, Philippe Sigaud via Digitalmars-d-learn wrote:
> asserts get an entire copy of the parse tree. It's a bit wasteful, but
> using 'immutable' directly does not work here, but this is OK:
> 
> enum res = MyRegex("abcabcdefFOOBAR"); // compile-time parsing
> immutable result = res; // to avoid copying the enum value everywhere   
 
   static immutable result = MyRegex("abcabcdefFOOBAR"); // compile-time parsing


> The static asserts then works (not the toString, though). Maybe

diff --git a/pegged/peg.d b/pegged/peg.d
index 98959294c40e..307e8a14b1dd 100644
--- a/pegged/peg.d
+++ b/pegged/peg.d
@@ -55,7 +55,7 @@ struct ParseTree
 /**
 Basic toString for easy pretty-printing.
 */
-string toString(string tabs = "")
+string toString(string tabs = "") const
 {
 string result = name;
 
@@ -262,7 +262,7 @@ Position position(string s)
 /**
 Same as previous overload, but from the begin of P.input to p.end
 */
-Position position(ParseTree p)
+Position position(const ParseTree p)
 {
 return position(p.input[0..p.end]);
 }

[completely untested; just did a git clone and fixed the two
 errors the compiler was whining about. Hmm, did pegged get
 faster? Last time i tried (years ago) it was unusably slow;
 right now, compiling your example, i didn't notice the extra
 multi-second delay that was there then.]

artur


Re: SImple C++ code to D

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

Alexandre:


I get a lot of problens, to convert 'strings' to UCHAR... :/


I suggest you to take a look at the D docs and understand what D 
fixed-sized arrays are, dynamic arrays, and strings (that are 
dynamic arrays).


Bye,
bearophile


Re: SImple C++ code to D

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

Andrea Fontana:


Is there any counter-indication with this:

immutable ubyte[5] stub = x"b8 01 4c cd 21".representation;

?


See:
https://issues.dlang.org/show_bug.cgi?id=10454
https://issues.dlang.org/show_bug.cgi?id=5909



Is it a compile time value?


Generally you need module-level values or enums to be sure a 
value is compile-time. In this case it is probably not.


Bye,
bearophile


Re: SImple C++ code to D

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

immutable ubyte[5] stub = x"b8 01 4c cd 21".representation;


that is a Real-Mode Stub Program

On Monday, 14 July 2014 at 12:32:38 UTC, Andrea Fontana wrote:

Is there any counter-indication with this:

immutable ubyte[5] stub = x"b8 01 4c cd 21".representation;

?

Is it a compile time value?


On Monday, 14 July 2014 at 12:18:20 UTC, bearophile wrote:

Alexandre:


Look at line 114 of my code: http://dpaste.com/3B5WYGV


The indentations are messed up.



peh.Signature = ('\0' << 8) + ('\0' << 8) + ('E' << 8) + 'P';


You need shifts 8, 16, 24...



alias PIMAGE_DOS_HEADER = IMAGE_DOS_HEADER*;


I don't see much need for such aliases.



auto peh = cast(PIMAGE_NT_HEADERS32)&image[0x80];


I suggest to avoid magic constants like that 0x80, like I have 
avoided it here:

memcpy(&image[IMAGE_DOS_HEADER.sizeof], 

Bye,
bearophile




Re: DStyle: Braces on same line

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

On Sunday, 13 July 2014 at 17:24:40 UTC, Timon Gehr wrote:

but separate-line opening braces definitely make it easier
to see where scopes begin and end.


This is the only argument I have heard in favour of doing this, 
but it is not actually valid. This critique might apply to Lisp 
style.


It is perfectly valid and much more important :P


Re: SImple C++ code to D

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

I don't see much need for such aliases.

Ok, how I can reduce the number of aliaSs ?

I suggest to avoid magic constants like that 0x80, like I have 
avoided it here:

memcpy(&image[IMAGE_DOS_HEADER.sizeof],


Btw, I need to start that part of code in x80


Look, that is my C++ code base: http://dpaste.com/1MMZK4R
I get a lot of problens, to convert 'strings' to UCHAR... :/


Re: SImple C++ code to D

2014-07-14 Thread Andrea Fontana via Digitalmars-d-learn

Is there any counter-indication with this:

immutable ubyte[5] stub = x"b8 01 4c cd 21".representation;

?

Is it a compile time value?


On Monday, 14 July 2014 at 12:18:20 UTC, bearophile wrote:

Alexandre:


Look at line 114 of my code: http://dpaste.com/3B5WYGV


The indentations are messed up.



peh.Signature = ('\0' << 8) + ('\0' << 8) + ('E' << 8) + 'P';


You need shifts 8, 16, 24...



alias PIMAGE_DOS_HEADER = IMAGE_DOS_HEADER*;


I don't see much need for such aliases.



auto peh = cast(PIMAGE_NT_HEADERS32)&image[0x80];


I suggest to avoid magic constants like that 0x80, like I have 
avoided it here:

memcpy(&image[IMAGE_DOS_HEADER.sizeof], 

Bye,
bearophile




Re: DStyle: Braces on same line

2014-07-14 Thread Danyal Zia via Digitalmars-d-learn

On Sunday, 13 July 2014 at 16:10:31 UTC, Gary Willoughby wrote:
Here is the 'official' style that is followed by most people 
including me.


http://dlang.org/dstyle.html


Unrelated to my original question. I already read that before 
asking.


Re: SImple C++ code to D

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

Alexandre:


Look at line 114 of my code: http://dpaste.com/3B5WYGV


The indentations are messed up.



peh.Signature = ('\0' << 8) + ('\0' << 8) + ('E' << 8) + 'P';


You need shifts 8, 16, 24...



alias PIMAGE_DOS_HEADER = IMAGE_DOS_HEADER*;


I don't see much need for such aliases.



auto peh = cast(PIMAGE_NT_HEADERS32)&image[0x80];


I suggest to avoid magic constants like that 0x80, like I have 
avoided it here:

memcpy(&image[IMAGE_DOS_HEADER.sizeof], 

Bye,
bearophile


Re: new properties for basic types

2014-07-14 Thread Dominikus Dittes Scherkl via Digitalmars-d-learn
On Monday, 14 July 2014 at 11:28:15 UTC, Philippe Sigaud via 
Digitalmars-d-learn wrote:
Halas, that's not what the OP wants. He needs properties on the 
*type*

itself: int.foo instead of foo!int.

Yes, exactly.



So no, this is not possible.


Hmm.
So how do I use stuff like this:

template defaultInit(T)
{
static if (!is(typeof({ T v = void; })))// inout(U)
@property T defaultInit(T v = T.init);
else
@property T defaultInit();
}

(this is from std.traits - ok, it's private, but anyway)
Because I have seen nowhere anything like defaultInit!T (or 
T.defaultInit)

and don't understand why here the attribute @property is used.
Why does it make a difference, and how?


Re: SImple C++ code to D

2014-07-14 Thread Alexandre via Digitalmars-d-learn
And some other strange thing is, the generate struct in PE file 
is wrong.. look at imagens...


As it should be...: http://i.imgur.com/4z1T3jF.png

And how is being generated...: http://i.imgur.com/Oysokuh.png

My actual source is that: http://dpaste.com/2TZKWF5

On Monday, 14 July 2014 at 11:55:18 UTC, Alexandre wrote:

Look at line 114 of my code: http://dpaste.com/3B5WYGV

Have a more better way to make this conversion ?
*(DWORD*)"PE\0\0" ?




Re: SImple C++ code to D

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

Look at line 114 of my code: http://dpaste.com/3B5WYGV

Have a more better way to make this conversion ?
*(DWORD*)"PE\0\0" ?


Re: Compile time regex matching

2014-07-14 Thread Philippe Sigaud via Digitalmars-d-learn
> I am trying to write some code that uses and matches to regular expressions
> at compile time, but the compiler won't let me because matchFirst and
> matchAll make use of malloc().
>
> Is there an alternative that I can use that can be run at compile time?

You can try Pegged, a parser generator that works at compile-time
(both the generator and the generated parser).

https://github.com/PhilippeSigaud/Pegged

docs:

https://github.com/PhilippeSigaud/Pegged/wiki/Pegged-Tutorial

It's also on dub:

http://code.dlang.org/packages/pegged

It takes a grammar as input, not a single regular expression, but the
syntax is not too different.


  import pegged.grammar;

  mixin(grammar(`
  MyRegex:
  foo <- "abc"* "def"?
  `));

  void main()
  {
  enum result = MyRegex("abcabcdefFOOBAR"); // compile-time parsing

  // everything can be queried and tested at compile-time, if need be.
  static assert(result.matches == ["abc", "abc", "def"]);
  static assert(result.begin == 0);
  static assert(result.end == 9);

  pragma(msg, result.toString()); // parse tree
  }


It probably does not implement all those regex nifty features, but it
has all the usual Parsing Expression Grammars powers. It gives you an
entire parse result, though: matches, children, subchildren, etc. As
you can see, matches are accessible at the top level.

One thing to keep in mind, that comes from the language and not this
library: in the previous code, since 'result' is an enum, it'll be
'pasted' in place everytime it's used in code: all those static
asserts get an entire copy of the parse tree. It's a bit wasteful, but
using 'immutable' directly does not work here, but this is OK:

enum res = MyRegex("abcabcdefFOOBAR"); // compile-time parsing
immutable result = res; // to avoid copying the enum value everywhere

The static asserts then works (not the toString, though). Maybe
someone more knowledgeable than me on DMD internals could certify it
indeed avoid re-allocating those parse results.


Re: new properties for basic types

2014-07-14 Thread Philippe Sigaud via Digitalmars-d-learn
Halas, that's not what the OP wants. He needs properties on the *type*
itself: int.foo instead of foo!int.

So no, this is not possible.


Re: new properties for basic types

2014-07-14 Thread Puming via Digitalmars-d-learn
On Monday, 14 July 2014 at 10:28:30 UTC, Dominikus Dittes Scherkl 
wrote:
Is it possible to write custom properties for basic types, so 
that I can write e.g. "int.myProp" instead of "myProp!int()" 
[analogue to x.myProp instead of myProp(x)]?


yes, just define a funciton with the first parameter int:

```d

@property int triple(int x)
{
  return x * 3;
}

void main()
{
  int x = 4;
  assert(12 == x.triple);
}
```


Re: DMDScript

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

On Sunday, 13 July 2014 at 07:18:38 UTC, Jason King wrote:

On Friday, 11 July 2014 at 15:45:42 UTC, Chris wrote:
Tried to compile on linux, got this error message (I guess I 
can fix it):


dmd -c textgen.d
textgen.d(36): Error: cannot implicitly convert expression 
("DMDScript fatal runtime error: ") of type string to char[]

.. bunch more errors.

You might try
https://github.com/DmitryOlshansky/DMDScript which, according 
to it's notes has changes to make it compatible with D2.


Just tried this. First, I had to replace import std.ctype; with 
import core.stdc.ctype. No big deal, but now I get this


make -fposix.mak
dmd -lib -O -release -inline  -d  dmdscript/darguments.d 
dmdscript/darray.d dmdscript/dboolean.d dmdscript/ddate.d 
dmdscript/ddeclaredfunction.d dmdscript/derror.d 
dmdscript/dfunction.d dmdscript/dglobal.d dmdscript/dmath.d 
dmdscript/dnative.d dmdscript/dnumber.d dmdscript/dobject.d 
dmdscript/dregexp.d dmdscript/dstring.d dmdscript/errmsgs.d 
dmdscript/expression.d dmdscript/functiondefinition.d 
dmdscript/identifier.d dmdscript/ir.d dmdscript/irstate.d 
dmdscript/iterator.d dmdscript/lexer.d dmdscript/opcodes.d 
dmdscript/parse.d dmdscript/program.d dmdscript/property.d 
dmdscript/date.d dmdscript/dateparse.d dmdscript/datebase.d 
dmdscript/protoerror.d dmdscript/RandAA.d dmdscript/scopex.d 
dmdscript/outbuffer.d dmdscript/script.d dmdscript/statement.d 
dmdscript/symbol.d dmdscript/text.d dmdscript/regexp.d 
dmdscript/threadcontext.d dmdscript/utf.d dmdscript/value.d  
dmdscript/extending.d -ofdmdscriptlib.a
dmdscript/dregexp.d(487): Error: function 
dmdscript.dregexp.Dregexp.Call multiple overrides of same function
dmdscript/dobject.d(47): Error: cannot take address of 
thread-local variable foo at compile time

make: *** [dmdscriptlib.a] Error 1

My idea is to use (at least test) DMDScript for server side JS.


new properties for basic types

2014-07-14 Thread Dominikus Dittes Scherkl via Digitalmars-d-learn
Is it possible to write custom properties for basic types, so 
that I can write e.g. "int.myProp" instead of "myProp!int()" 
[analogue to x.myProp instead of myProp(x)]?


Re: std.algorithm.among

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

Ola Fosheim Grøstad:


On Sunday, 13 July 2014 at 20:55:25 UTC, bearophile wrote:
D doesn't carry the range of mutable variables across 
different expressions.


What is the reasoning behind this kind of special-casing?


Compiler simplicity and to avoid flow analysis. The value range 
is kept across const expressions since 2.066.


Bye,
bearophile