Re: The Mystery of the Misbehaved Malloc
On Saturday, 30 July 2016 at 05:21:26 UTC, ag0aep6g wrote: On 07/30/2016 07:00 AM, 岩倉 澪 wrote: auto mem = malloc(2^^31); 2^^31 is negative. 2^^31-1 is the maximum positive value of an int, so 2^^31 wraps around to int.min. Try 2u^^31. bah, I'm an idiot! CASE CLOSED. Thanks for the help :P
WindowsCE toolchain
Hello! I'm used small device(Electronic dictionary) which installed Windows CE 6.0. How can i found D compiler(ldc or dmd) for Windows CE?(I just wonder. not serious.) regards,
Re: The Mystery of the Misbehaved Malloc
On 07/30/2016 07:00 AM, 岩倉 澪 wrote: auto mem = malloc(2^^31); 2^^31 is negative. 2^^31-1 is the maximum positive value of an int, so 2^^31 wraps around to int.min. Try 2u^^31.
The Mystery of the Misbehaved Malloc
So I ran into a problem earlier - trying to allocate 2GB or more on Windows would fail even if there was enough room. Mentioned it in the D irc channel and a few fine folks pointed out that Windows only allows 2GB for 32-bit applications unless you pass a special flag which may or may not be a good idea. I think to myself, "Easy solution, I'll just compile as 64-bit!" But alas, my 64-bit executable suffers the same problem. I boiled it down to a simple test: void main() { import core.stdc.stdlib : malloc; auto mem = malloc(2^^31); assert(mem); import core.stdc.stdio : getchar; getchar(); } I wrote this test with the C functions so that I can do a direct comparison with a C program compiled with VS 2015: #include #include #include #include int main(int argc, char *argv[]) { void *ptr = malloc((size_t)pow(2, 31)); assert(ptr); getchar(); return 0; } I compile the D test with: `ldc2 -m64 -test.d` I compile the C test with: `CL test.c` `file` reports "PE32+ executable (console) x86-64, for MS Windows" for both executables. When the C executable runs, I see the allocation under "commit change" in the Resource Monitor. When the D executable runs, the assertion fails! The D program is able to allocate up to 2^31 - 1 before failing. And yes, I do have enough available memory to make a larger allocation. Can you help me solve this mystery?
Re: Does D have object wrappers for primitives?
On Friday, 29 July 2016 at 20:25:16 UTC, Cauterite wrote: On Friday, 29 July 2016 at 20:13:34 UTC, stunaep wrote: I have some java code I need to convert and at one point it uses an Object[] array to store various ints, longs, and strings. Java has built in Integer and Long classes that wrap the primitives in an object and strings are already objects. No, but with a template you could easily make your own: class Boxed(T) { T _v; alias _v this; this(in T v) immutable {_v = v;}; }; auto i = new Boxed!int(6); Thank you. This is just what I needed. I am curious though as to why this doesn't work with strings. It would work if I removed immutable from the Boxed constructor but I thought strings were immutable. I get a compiler error 'not callable using a mutable object'. Even marking a string with the immutable keyword has the same result.
Re: C's void func() vs. void func(void).
On Friday, 29 July 2016 at 18:24:52 UTC, ag0aep6g wrote: On 07/29/2016 02:15 PM, Mike Parker wrote: And if it is a cross-platform library that is stdcall on Windows and cdecl elsewhere: extern(C) void fun(); extern(System), no? Yeah, that's what I had intended.
Re: Why D isn't the next "big thing" already
On Tuesday, 26 July 2016 at 15:11:00 UTC, llaine wrote: Hi guys, I'm using D since a few month now and I was wondering why people don't jump onto it that much and why it isn't the "big thing" already. Everybody is into javascript nowadays, but IMO even for doing web I found Vibe.d more interesting and efficient than node.js for example. I agree that you have to be pragmatic and choose the right tools for the right jobs but I would be interested to have other opinion on thoses questions. I think we need more frameworks like vibe.d to build things with them. Currently there is not much so only a class of programmers will find the language useful. Another thing is that the language is not marketed well enough. Someone need to handle marketing of the language, like real marketing. Most people are still unaware of D.
Re: Does D have object wrappers for primitives?
On 07/29/2016 01:40 PM, Cauterite wrote: > On Friday, 29 July 2016 at 20:26:47 UTC, Ali Çehreli wrote: >> >> I was going to suggest Algebraic because it allows arrays of mixed >> primitive types (wrapped in Algebraic): >> >> https://dlang.org/phobos/std_variant.html#.Algebraic >> >> Ali > > It could work, but keep in mind Algebraic is a structure, not an object. Also, I've later noticed that your Box was a class, so it would allow "arrays of mixed primitive types" as well. Yes, Algebraic is not a struct but Java not having structs doesn't mean that the original code really needed classes either. :) Ali
Re: Does D have object wrappers for primitives?
On Friday, 29 July 2016 at 20:26:47 UTC, Ali Çehreli wrote: I was going to suggest Algebraic because it allows arrays of mixed primitive types (wrapped in Algebraic): https://dlang.org/phobos/std_variant.html#.Algebraic Ali It could work, but keep in mind Algebraic is a structure, not an object.
Re: Does D have object wrappers for primitives?
On 07/29/2016 01:25 PM, Cauterite wrote: On Friday, 29 July 2016 at 20:13:34 UTC, stunaep wrote: I have some java code I need to convert and at one point it uses an Object[] array to store various ints, longs, and strings. Java has built in Integer and Long classes that wrap the primitives in an object and strings are already objects. No, but with a template you could easily make your own: class Boxed(T) { T _v; alias _v this; this(in T v) immutable {_v = v;}; }; auto i = new Boxed!int(6); I was going to suggest Algebraic because it allows arrays of mixed primitive types (wrapped in Algebraic): https://dlang.org/phobos/std_variant.html#.Algebraic Ali
Re: Does D have object wrappers for primitives?
On Friday, 29 July 2016 at 20:13:34 UTC, stunaep wrote: I have some java code I need to convert and at one point it uses an Object[] array to store various ints, longs, and strings. Java has built in Integer and Long classes that wrap the primitives in an object and strings are already objects. No, but with a template you could easily make your own: class Boxed(T) { T _v; alias _v this; this(in T v) immutable {_v = v;}; }; auto i = new Boxed!int(6);
Does D have object wrappers for primitives?
I have some java code I need to convert and at one point it uses an Object[] array to store various ints, longs, and strings. Java has built in Integer and Long classes that wrap the primitives in an object and strings are already objects.
Re: string mixin and alias
On Friday, 29 July 2016 at 18:39:23 UTC, Jesse Phillips wrote: On Friday, 29 July 2016 at 18:34:56 UTC, Jesse Phillips wrote: Here the generateCode() is mixed in to the context of foo(), which is fine if your code is performing actions but is no good if you're creating declarations that you want to use in the context of main(). Here is a fully functioning example: --- string generateCode(string s){return s;} void main() { int a, b; enum s = "a = 2 + 3; b = 4 + a;"; foo!(s)(a, b); assert(a == 5); assert(b == 9); } void foo(alias s)(ref int a, ref int b) { mixin(generateCode(s)); } --- Thanks for all the answers. Kind regards Andre
Re: Use dup on Containers with const Elements
On 7/29/16 3:00 PM, Q. Schroll wrote: Cases to consider: Arrays and AAs with const(T) Elements, where T is a value or a reference type respectively. [snip] Questions: (1) Why do I have to specify the type here? Why does inference fail? (2) Why not just S[S]? The copy of a const S is a S so why is the copy of a (const S, const S)-pair not just (S, S)? array.dup has the meaning to copy the original but make the elements mutable. At least, that's what it was when it was handled by the compiler/runtime. So the reason for 1 is that you can't convert const(C) to just C without a cast, so you must specify the type. I'm not certain about AA, as I don't remember how dup was defined on them. -Steve
Use dup on Containers with const Elements
Cases to consider: Arrays and AAs with const(T) Elements, where T is a value or a reference type respectively. class C { } struct S { } const(S)[] varr; const(C)[] carr; const(S)[S] vaav; const(C)[S] caav; const(S)[C] vaac; const(C)[C] caac; pragma(msg, typeof(varr.dup)); pragma(msg, typeof(carr.dup!(const C))); // (1) pragma(msg, typeof(vaav.dup)); pragma(msg, typeof(caav.dup)); pragma(msg, typeof(vaac.dup)); pragma(msg, typeof(caac.dup)); yields S[] // ok const(C)[] // ok const(S)[S] // (2) const(C)[S] // ok const(S)[C] // ok const(C)[C] // ok Questions: (1) Why do I have to specify the type here? Why does inference fail? (2) Why not just S[S]? The copy of a const S is a S so why is the copy of a (const S, const S)-pair not just (S, S)?
Re: string mixin and alias
On Friday, 29 July 2016 at 18:34:56 UTC, Jesse Phillips wrote: Here the generateCode() is mixed in to the context of foo(), which is fine if your code is performing actions but is no good if you're creating declarations that you want to use in the context of main(). Here is a fully functioning example: --- string generateCode(string s){return s;} void main() { int a, b; enum s = "a = 2 + 3; b = 4 + a;"; foo!(s)(a, b); assert(a == 5); assert(b == 9); } void foo(alias s)(ref int a, ref int b) { mixin(generateCode(s)); } ---
Re: string mixin and alias
D won't let you hide the mixin, the keyword is there specifically to indicate code is being injected in that location. This isn't what you are looking for, but you can do something like this: - string generateCode(string s){return "";} void main() { enum s = "a = 2 + 3; b = 4 + a;"; foo!(s); } void foo(alias s)() { mixin(generateCode(s)); } - Here the generateCode() is mixed in to the context of foo(), which is fine if your code is performing actions but is no good if you're creating declarations that you want to use in the context of main().
Re: C's void func() vs. void func(void).
On 07/29/2016 02:15 PM, Mike Parker wrote: And if it is a cross-platform library that is stdcall on Windows and cdecl elsewhere: extern(C) void fun(); extern(System), no?
Re: When I should to call destroy?
On Friday, July 29, 2016 13:18:00 Suliman via Digitalmars-d-learn wrote: > Use the `destroy` function to finalize an object by calling its > destructor. The memory of the object is not immediately > deallocated, instead the GC will collect the memory of the object > at an undetermined point after finalization: > > class Foo { int x; this() { x = 1; } } > Foo foo = new Foo; > destroy(foo); > assert(foo.x == int.init); // object is still accessible > > > But I can't understand if D have GC it should remove objects when > their life is finished. When I should to call `destroy`? What > would be if I will not call it? destroy is for when you need to explicitly call the destructor on an object. It destroys it without freeing the memory. There are some rare cases where that is extremely useful, but odds are, you'll never need it. - Jonathan M Davis
FunctionTypeOf behaves unexpectedly for function pointers?
This failure seems curious and I haven't been able to understand why it occurs, or whether it might be intentional. For all other callable types, including functions and delegates and types implementing opCall, the assertion passes. import std.traits : FunctionTypeOf; void function() func; // Error: static assert (is(void() == void function())) is false static assert(is(FunctionTypeOf!func == typeof(func)));
Re: When I should to call destroy?
On Friday, 29 July 2016 at 13:18:00 UTC, Suliman wrote: Use the `destroy` function to finalize an object by calling its destructor. The memory of the object is not immediately deallocated, instead the GC will collect the memory of the object at an undetermined point after finalization: class Foo { int x; this() { x = 1; } } Foo foo = new Foo; destroy(foo); assert(foo.x == int.init); // object is still accessible But I can't understand if D have GC it should remove objects when their life is finished. When I should to call `destroy`? What would be if I will not call it? Destroy will call the destructors, but memory will not actually be deallocated before the next GC cycle. It is used because the GC cycle does not guarantee to run the destructors, so using destroy the destructor is ran immediately (guaranteed). Then, on next GC iteration, the memory will be freed. Until then, referencing it will not cause segfault, but will cause undefined behaviour, as after a destructor the state of the object is undefined (I think).
Re: When I should to call destroy?
On Friday, 29 July 2016 at 13:18:00 UTC, Suliman wrote: But I can't understand if D have GC it should remove objects when their life is finished. When I should to call `destroy`? What would be if I will not call it? You should call destroy when you want to call the destructor deterministically, see https://en.wikipedia.org/wiki/Resource_management_%28computing%29 - compare with automatic resource management.
Re: When I should to call destroy?
On Friday, 29 July 2016 at 13:18:00 UTC, Suliman wrote: But I can't understand if D have GC it should remove objects when their life is finished. When I should to call `destroy`? What would be if I will not call it? `destroy` is mainly about running destructors deterministically. From the source comments: "It's used to destroy an object so that any cleanup which its destructor or finalizer does is done and so that it no longer references any other objects." For example, your object might allocate a large amount of manually-managed memory, and you don't want that allocation laying around until the next GC cycle, so you can use `destroy` to finalise your object as early as possible to release that manually-managed allocation. If you really want to deallocate the object's own memory, you can check out core.memory.GC.query() / core.memory.GC.free().
When I should to call destroy?
Use the `destroy` function to finalize an object by calling its destructor. The memory of the object is not immediately deallocated, instead the GC will collect the memory of the object at an undetermined point after finalization: class Foo { int x; this() { x = 1; } } Foo foo = new Foo; destroy(foo); assert(foo.x == int.init); // object is still accessible But I can't understand if D have GC it should remove objects when their life is finished. When I should to call `destroy`? What would be if I will not call it?
Re: C's void func() vs. void func(void).
On Friday, 29 July 2016 at 12:20:17 UTC, Mike Parker wrote: Though, I should add the caveat that you need to ensure the definition of the C function does not specify any parameters. AFAIK, this is legal: // foo.h void func(); // foo.c void func(int a, int b) { ... } In which case you would want to include the parameters in your binding. Thanks, good to know.
Re: string mixin and alias
On Friday, 29 July 2016 at 12:22:54 UTC, Andre Pany wrote: It is more or less syntax sugar. In the main function instead of writing "mixin(generateCode(s));" I want to write "foo(s);". So, the mixin statement is hidden while the functionality of mixin stays. Kind regards André As far as I know, there's no way to hide away the `mixin` statement in a template or alias. You could do something like this, if you really wanted to, but really the only difference is that it uses fewer parentheses. string generateCode(string s){return "";} enum foo(string s) = generateCode(s); void main(){ enum s = "int a = 2 + 3; int b = 4 + a;"; mixin foo!s; }
Re: string mixin and alias
On 7/29/16 2:38 AM, Andre Pany wrote: Hi, is there a way to alias a string mixin? Neither foo nor foo2 compiles. import std.meta : Alias; alias foo = (s) => Alias!(mixin(generateCode(s))); s is a runtime parameter. You can't mixin stuff that is only available at runtime. alias foo2(string s) = Alias!(mixin(generateCode(s))); This would work if the string is "main", let's say. string generateCode(string s){return "";} void main() { enum s = "a = 2 + 3; b = 4 + a;"; This does not result in a symbol, an alias needs a symbol. -Steve
Re: C's void func() vs. void func(void).
On Friday, 29 July 2016 at 12:15:22 UTC, Mike Parker wrote: Yes, this is correct as long as the calling convention is not stdcall or something else: Though, I should add the caveat that you need to ensure the definition of the C function does not specify any parameters. AFAIK, this is legal: // foo.h void func(); // foo.c void func(int a, int b) { ... } In which case you would want to include the parameters in your binding.
Re: string mixin and alias
On Friday, 29 July 2016 at 12:11:44 UTC, pineapple wrote: On Friday, 29 July 2016 at 06:38:17 UTC, Andre Pany wrote: Hi, is there a way to alias a string mixin? Neither foo nor foo2 compiles. import std.meta : Alias; alias foo = (s) => Alias!(mixin(generateCode(s))); alias foo2(string s) = Alias!(mixin(generateCode(s))); string generateCode(string s){return "";} void main() { enum s = "a = 2 + 3; b = 4 + a;"; foo(s); foo2(s); } Kind regards André It's not clear what you're trying to accomplish. What would you expect this code to do? It is more or less syntax sugar. In the main function instead of writing "mixin(generateCode(s));" I want to write "foo(s);". So, the mixin statement is hidden while the functionality of mixin stays. Kind regards André
Re: C's void func() vs. void func(void).
On Friday, 29 July 2016 at 10:57:37 UTC, ciechowoj wrote: In C, a function `void func()` doesn't declare a function without arguments, instead it declares a function that takes unspecified number of arguments. The correct way to declare a function that takes no arguments is to use the `void` keyword: `void func(void)`. What is the correct way to refer to such a function (`void func()`) from D bindings? If I assume that the unspecified number of arguments (for some particular function) is equal to zero, is `extern (C) void func()` a correct D binding to the both functions `void func()` and `void func(void)` declared in C? Specifically, I'm concerned about calling convention issues. Yes, this is correct as long as the calling convention is not stdcall or something else: extern(C) void func(); If you're dealing with stdcall: extern(Windows) void func(); And if it is a cross-platform library that is stdcall on Windows and cdecl elsewhere: extern(C) void fun();
Re: string mixin and alias
On Friday, 29 July 2016 at 06:38:17 UTC, Andre Pany wrote: Hi, is there a way to alias a string mixin? Neither foo nor foo2 compiles. import std.meta : Alias; alias foo = (s) => Alias!(mixin(generateCode(s))); alias foo2(string s) = Alias!(mixin(generateCode(s))); string generateCode(string s){return "";} void main() { enum s = "a = 2 + 3; b = 4 + a;"; foo(s); foo2(s); } Kind regards André It's not clear what you're trying to accomplish. What would you expect this code to do?
Re: Question about destructor of database and multiple use access
So my last variant is right? How can I inspect code, to better understand how GC works? Also where I can find idiomatic code with comments? Just to read for better understand design solutions.
C's void func() vs. void func(void).
In C, a function `void func()` doesn't declare a function without arguments, instead it declares a function that takes unspecified number of arguments. The correct way to declare a function that takes no arguments is to use the `void` keyword: `void func(void)`. What is the correct way to refer to such a function (`void func()`) from D bindings? If I assume that the unspecified number of arguments (for some particular function) is equal to zero, is `extern (C) void func()` a correct D binding to the both functions `void func()` and `void func(void)` declared in C? Specifically, I'm concerned about calling convention issues.
Re: Why D isn't the next "big thing" already
On Thursday, 28 July 2016 at 15:16:20 UTC, Gorge Jingale wrote: On Wednesday, 27 July 2016 at 10:41:54 UTC, ketmar wrote: On Wednesday, 27 July 2016 at 10:39:52 UTC, NX wrote: Lack of production quality tools like? no, "refactoring" and other crap is not "production quality tools", they are only useful to pretend that you are doing something useful, so you will look busy for your boss. Guys please, I'm just trying to do something constructive here.
Re: Can't Compile Global Semaphores?
On Monday, 21 March 2016 at 13:19:34 UTC, denizzzka wrote: On Monday, 27 July 2015 at 20:12:10 UTC, John Colvin wrote: Yes, but then core.sync.semaphore doesn't support being shared, so... I don't really understand how https://github.com/D-Programming-Language/druntime/blob/master/src/core/sync/semaphore.d#L356 is managing to avoid this Since that time is something cleared up? Faced with the same problem And same for me also; I really need shared semaphores, for implementing complex synchronization queues (they have to be nested inside shared classes). That's rather disappointing to offer semaphores without thread sharing mechanisms beyond __gshared. Is there anything we could do to solve this? best NB thanks to all D contributors
Re: Dealing with unicode
On Friday, 29 July 2016 at 06:32:24 UTC, Fabian wrote: I'm trying to add support for unicode to my app in D and having issues. string str = "Pokémon No"; writeln(str); this outputs: Pok├®mon No what I want to do is change the funky character such that the string reads: Pok\u00e9mon No as \u00e9 == é how can i do this in D? Strings are UTF-8 in D - but the console might need to be configured to display them correctly. If you're on Windows, this will do it: SetConsoleOutputCP(CP_UTF8);
Re: string mixin and alias
On 29/07/2016 6:38 PM, Andre Pany wrote: Hi, is there a way to alias a string mixin? Neither foo nor foo2 compiles. import std.meta : Alias; alias foo = (s) => Alias!(mixin(generateCode(s))); alias foo2(string s) = Alias!(mixin(generateCode(s))); string generateCode(string s){return "";} void main() { enum s = "a = 2 + 3; b = 4 + a;"; foo(s); foo2(s); } Kind regards André Well those string mixins are not evaluating out to a symbol. So that could be a rather big problem for the Alias template.
Re: Dealing with unicode
On 29/07/2016 6:32 PM, Fabian wrote: I'm trying to add support for unicode to my app in D and having issues. string str = "Pokémon No"; writeln(str); this outputs: Pok├®mon No what I want to do is change the funky character such that the string reads: Pok\u00e9mon No as \u00e9 == é how can i do this in D? Your terminal doesn't support utf-8. Prints fine using dpaste and gnome-terminal.
string mixin and alias
Hi, is there a way to alias a string mixin? Neither foo nor foo2 compiles. import std.meta : Alias; alias foo = (s) => Alias!(mixin(generateCode(s))); alias foo2(string s) = Alias!(mixin(generateCode(s))); string generateCode(string s){return "";} void main() { enum s = "a = 2 + 3; b = 4 + a;"; foo(s); foo2(s); } Kind regards André
Dealing with unicode
I'm trying to add support for unicode to my app in D and having issues. string str = "Pokémon No"; writeln(str); this outputs: Pok├®mon No what I want to do is change the funky character such that the string reads: Pok\u00e9mon No as \u00e9 == é how can i do this in D?