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

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: 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

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

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: 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

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: 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

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

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: 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

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:

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 = xb8 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.

Re: SImple C++ code to D

2014-07-14 Thread Alexandre via Digitalmars-d-learn
immutable ubyte[5] stub = xb8 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 = xb8 01 4c cd 21.representation; ? Is it a compile time value? On

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 = xb8 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

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: 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;

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 (a-is-actually-a-B) {

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 {

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:

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.

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

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

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

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

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

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

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: 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

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

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 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 ?

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

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,

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.

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

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

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

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

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

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

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

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: 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

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

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: 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

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: 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: 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

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.

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

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 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: 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 = ;

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

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 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: 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

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:

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

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.

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

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 digitalmars-d-learn@puremagic.com 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,

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