Re: parameter pack to inputRange
On 05/05/2016 10:00 PM, Erik Smith wrote: Is there an existing way to adapt a parameter pack to an input range? I would like to construct an array with it. Example: void run(A...) (A args) { Array!int a(toInputRange(args)); } Inspired by my DConf 2016 talk ;) here is a fiber-based InputRange solution: import std.stdio; import std.concurrency; import std.container; alias GeneratorFiber = std.concurrency.Generator; void run(A...) (A args) { void asInputRange() { /* static */ foreach (arg; args) { yield(arg); } } auto range = new GeneratorFiber!int(); auto a = Array!int(range); writeln(a[]); } void main() { run(1, 2, 3); } Ali
Re: parameter pack to inputRange
On 05/05/2016 10:00 PM, Erik Smith wrote: Is there an existing way to adapt a parameter pack to an input range? I would like to construct an array with it. Example: void run(A...) (A args) { Array!int a(toInputRange(args)); } Just initialize an array with the arguments: void run(A...) (A args) { writeln([args]); } Ali
parameter pack to inputRange
Is there an existing way to adapt a parameter pack to an input range? I would like to construct an array with it. Example: void run(A...) (A args) { Array!int a(toInputRange(args)); }
Re: How the heck is onInvalidMemoryOperationError() nothrow?
On Friday, 6 May 2016 at 02:57:59 UTC, Jeremy DeHaan wrote: In core.exception, we have a lovely function called onInvalidMemoryOperationError(). This function is marked as nothrow (plus other annotations). This function literally does nothing except throwing an error. How can it be marked as nothrow? From the spec (https://dlang.org/spec/function.html#nothrow-functions): "Nothrow functions can only throw exceptions derived from class Error." Throwing an Error is, for many purposes, considered fundamentally different than throwing an Exception because Error objects aren't meant to be caught by user code. Throwing an Error is supposed to just be a way of crashing the program with a nice message and stack trace. A key benefit of this distinction, is that it enables the use of `assert()` statements in `nothrow` code.
How the heck is onInvalidMemoryOperationError() nothrow?
In core.exception, we have a lovely function called onInvalidMemoryOperationError(). This function is marked as nothrow (plus other annotations). This function literally does nothing except throwing an error. How can it be marked as nothrow?
Re: what's the right way to get char* from string?
On Thursday, 5 May 2016 at 07:49:46 UTC, aki wrote: extern (C) int strcmp(char* string1, char* string2); This signature of strcmp is incorrect. strcmp accepts const char* arguments [1], which in D would be written as const(char)*. The immutable(char)* values returned from toStringz are implicitly convertible to const(char)* and are therefore useable as-is as arguments to strcmp. import std.string; extern (C) int strcmp(const(char)* string1, const(char)* string2); auto v = strcmp(somestring1.toStringz, somestring2.toStringz); [1] http://linux.die.net/man/3/strcmp
Re: template auto instantiation when parameters empty
On 5/5/16 6:50 PM, Erik Smith wrote: Alias works at the cost of adding a 2nd type name: alias Res = Resource!(); auto res = Res.create The other problem is that the alias definition by itself instantiates, which I can't afford. I have createResource() now, it just doesn't fit well with the rest of the calling styles. There is also this approach, which might be slightly more idiomatic struct Resource(T) {} struct Policy {} auto create(alias T)() { T!Policy r; return r; } auto r = create!Resource; I believe factory external IFTI functions are quite idiomatic. They are all over phobos. -Steve
Re: template auto instantiation when parameters empty
On Thursday, 5 May 2016 at 16:12:40 UTC, Steven Schveighoffer wrote: On 5/5/16 12:10 AM, Erik Smith wrote: I want to have a struct template auto instantiate when the template parameters are defaulted or missing. Example: struct Resource(T=int) { static auto create() {return Resource(null);} this(string s) {} } auto resource = Resource.create; As a plain struct it works, but not as a template: struct Resource { // works struct Resource() { // fails struct Resource(T=int) { // fails At the call site, this works, but I'm hoping for a few less symbols: auto resource = Resource!().create; Any ideas? Instead of static method, use an external factory method: static auto createResource(T = int)() { return Resource!T(null); } And I wouldn't bother with making Resource's T have a default, as you'd still have to instantiate it with Resource!() to get the default. -Steve Alias works at the cost of adding a 2nd type name: alias Res = Resource!(); auto res = Res.create The other problem is that the alias definition by itself instantiates, which I can't afford. I have createResource() now, it just doesn't fit well with the rest of the calling styles. There is also this approach, which might be slightly more idiomatic struct Resource(T) {} struct Policy {} auto create(alias T)() { T!Policy r; return r; } auto r = create!Resource;
Re: template auto instantiation when parameters empty
On 5/5/16 12:10 AM, Erik Smith wrote: I want to have a struct template auto instantiate when the template parameters are defaulted or missing. Example: struct Resource(T=int) { static auto create() {return Resource(null);} this(string s) {} } auto resource = Resource.create; As a plain struct it works, but not as a template: struct Resource { // works struct Resource() { // fails struct Resource(T=int) { // fails At the call site, this works, but I'm hoping for a few less symbols: auto resource = Resource!().create; Any ideas? Instead of static method, use an external factory method: static auto createResource(T = int)() { return Resource!T(null); } And I wouldn't bother with making Resource's T have a default, as you'd still have to instantiate it with Resource!() to get the default. -Steve
Re: what's the right way to get char* from string?
On 5/5/16 3:36 PM, Steven Schveighoffer wrote: Only thing I can think of is.. um... horrible: char *toCharz(string s) { auto cstr = s.toStringz; return cstr[0 .. s.length + 1].dup.ptr; } Ignore this. What Jonathan said :) -Steve
Re: what's the right way to get char* from string?
On Thursday, 5 May 2016 at 11:35:09 UTC, Jonathan M Davis wrote: If you want a different mutability, then use the more general function std.utf.toUTFz. e.g. from the documentation: auto p1 = toUTFz!(char*)("hello world"); auto p2 = toUTFz!(const(char)*)("hello world"); auto p3 = toUTFz!(immutable(char)*)("hello world"); auto p4 = toUTFz!(char*)("hello world"d); auto p5 = toUTFz!(const(wchar)*)("hello world"); auto p6 = toUTFz!(immutable(dchar)*)("hello world"w); - Jonathan M Davis Ah! This can be a solution. Thanks Jonathan. -- aki.
Re: Show window maximized in DlangUI
On Thursday, 5 May 2016 at 14:10:14 UTC, Vadim Lopatin wrote: On Thursday, 5 May 2016 at 07:53:43 UTC, default0 wrote: Hi I'm writing a D Desktop application using DlangUI. I want the window it creates to start maximized (but not Fullscreen) and could not find any APIs in the documentation that seem to do this. Does anyone know how to do this? No such functionality yet. Could you please submit feature request on github? https://github.com/buggins/dlangui/issues/258 done :)
Re: Unique Enum Members
On Thursday, 5 May 2016 at 12:54:08 UTC, Nordlöw wrote: Is there a way to calculate unique enum members without using sort, such as *not* done in my current implementation: auto uniqueEnumMembers(T)() { import std.traits: EnumMembers; import std.algorithm: sort, uniq; return [EnumMembers!T].sort().uniq; } Preferrably both at compile-time and run-time. Sure, if you can hash the underlying type this should do the trick in linear time: http://melpon.org/wandbox/permlink/Rvq60fv7jOIPuhXz
Re: Show window maximized in DlangUI
On Thursday, 5 May 2016 at 07:53:43 UTC, default0 wrote: Hi I'm writing a D Desktop application using DlangUI. I want the window it creates to start maximized (but not Fullscreen) and could not find any APIs in the documentation that seem to do this. Does anyone know how to do this? No such functionality yet. Could you please submit feature request on github?
Re: what's the right way to get char* from string?
On 5/5/16 11:53 AM, pineapple wrote: On Thursday, 5 May 2016 at 07:49:46 UTC, aki wrote: Hello, When I need to call C function, often need to have char* pointer from string. This might help: import std.traits : isSomeString; import std.string : toStringz; extern (C) int strcmp(char* string1, char* string2); int strcmpD0(S)(in S lhs, in S rhs) if(is(S == string) || is(S == const(char)[])) { // Best return strcmp( cast(char*) toStringz(lhs), cast(char*) toStringz(rhs) ); } This is likely a correct solution, because strcmp does not modify any data in the string itself. Practically speaking, you can define strcmp as taking const(char)*. This is what druntime does: http://dlang.org/phobos/core_stdc_string.html#.strcmp int strcmpD1(S)(in S lhs, in S rhs) if(is(S == string) || is(S == const(char)[])) { // Works return strcmp( cast(char*) lhs.ptr, cast(char*) rhs.ptr ); } Note, this only works if the strings are literals. Do not use this mechanism in general. /+ int strcmpD2(S)(in S lhs, in S rhs) if(is(S == string) || is(S == const(char)[])) { // Breaks return strcmp( toStringz(lhs), toStringz(rhs) ); } +/ Given a possibility that you are calling a C function that may actually modify the data, there isn't a really good way to do this. Only thing I can think of is.. um... horrible: char *toCharz(string s) { auto cstr = s.toStringz; return cstr[0 .. s.length + 1].dup.ptr; } -Steve
Re: Async or event library
On Thursday, 5 May 2016 at 08:19:26 UTC, chmike wrote: Hello I have seen the wiki page https://wiki.dlang.org/Event_system and would like to know the current status. Is there a working group for this subject ? This is a topic I'm interested in and did some modest work on some years ago. [...] We has one: Collie, now is in develop, and use in our server. Use for TCP and http. It like facebook/wangle: https://github.com/putao-dev/collie
Unique Enum Members
Is there a way to calculate unique enum members without using sort, such as *not* done in my current implementation: auto uniqueEnumMembers(T)() { import std.traits: EnumMembers; import std.algorithm: sort, uniq; return [EnumMembers!T].sort().uniq; } Preferrably both at compile-time and run-time.
Re: what's the right way to get char* from string?
On Thu, 05 May 2016 07:49:46 + aki via Digitalmars-d-learnwrote: > Hello, > > When I need to call C function, often need to > have char* pointer from string. > > "Interfacing to C++" page: > https://dlang.org/spec/cpp_interface.html > have following example. > > extern (C) int strcmp(char* string1, char* string2); > import std.string; > int myDfunction(char[] s) > { > return strcmp(std.string.toStringz(s), "foo"); > } > > but this is incorrect because toStringz() returns immutable > pointer. > One way is to write mutable version of toStringz() > > char* toStringzMutable(string s) @trusted pure nothrow { > auto copy = new char[s.length + 1]; > copy[0..s.length] = s[]; > copy[s.length] = 0; > return copy.ptr; > } > > But I think this is common needs, > why it is not provided by Phobos? > (or tell me if it has) If you want a different mutability, then use the more general function std.utf.toUTFz. e.g. from the documentation: auto p1 = toUTFz!(char*)("hello world"); auto p2 = toUTFz!(const(char)*)("hello world"); auto p3 = toUTFz!(immutable(char)*)("hello world"); auto p4 = toUTFz!(char*)("hello world"d); auto p5 = toUTFz!(const(wchar)*)("hello world"); auto p6 = toUTFz!(immutable(dchar)*)("hello world"w); - Jonathan M Davis
Re: what's the right way to get char* from string?
On Thursday, 5 May 2016 at 07:49:46 UTC, aki wrote: Hello, When I need to call C function, often need to have char* pointer from string. This might help: import std.traits : isSomeString; import std.string : toStringz; extern (C) int strcmp(char* string1, char* string2); int strcmpD0(S)(in S lhs, in S rhs) if(is(S == string) || is(S == const(char)[])) { // Best return strcmp( cast(char*) toStringz(lhs), cast(char*) toStringz(rhs) ); } int strcmpD1(S)(in S lhs, in S rhs) if(is(S == string) || is(S == const(char)[])) { // Works return strcmp( cast(char*) lhs.ptr, cast(char*) rhs.ptr ); } /+ int strcmpD2(S)(in S lhs, in S rhs) if(is(S == string) || is(S == const(char)[])) { // Breaks return strcmp( toStringz(lhs), toStringz(rhs) ); } +/ void main(){ import std.stdio; writeln(strcmpD0("foo", "bar")); // Best writeln(strcmpD1("foo", "bar")); // Works //writeln(strcmpD2("foo", "bar")); // Breaks }
Re: Strange stack variable corruption error after calling extern(C) function
On Wednesday, 4 May 2016 at 17:53:32 UTC, cc wrote: The OS is Win64 though the program is being compiled as 32-bit and I'm using the 32-bit distributed DLL. fmod.dll: PE32 executable (DLL) (GUI) Intel 80386, for MS Windows Tried int and long as the return type, same issue both ways. Tried void too just in case, same thing though. Could you please post the definition of FMOD_RESULT. Its possible that the create sound function returns it on stack and not inside a register. This is usually the case if FMOD_RESULT is defined as a struct in C/C++. But its hard to say. In your case I would actually look at the disassembly and step through it to see where its going wrong and messing up the stack.
Re: Async or event library
Event loops needs to be thread local not per process. So many API's such as WinAPI for e.g. GUI's have this requirement in it that its just not worth fighting over.
Re: Async or event library
I would like to add that the switchable TLS is only a half backed solution. It would't work in a multi core context where threads are truly executing in parallel. Two such threads might get the same TLS context which would invalidate its implicit predicate. Another strategy would be to forbit use of TLS with threads using the event loop. But this might break existing code.
Async or event library
Hello I have seen the wiki page https://wiki.dlang.org/Event_system and would like to know the current status. Is there a working group for this subject ? This is a topic I'm interested in and did some modest work on some years ago. At the bottom of the wiki page there is an innocent question regarding TLS which is quite devastating. A worker thread pool system would not support affinity between threads and callback context. Unfortunately, D relies on Thread Local Storage for semi global data. This would be error prone. I saw such error case with people using TLS with Corba. One way out of this apparent deadlock is if D would provide its own TLS that can be switched between threads. This would allow to preserve affinity between threads and callback execution context. Unfortunately, it would introduce an overhead to access the data in the local storage due to the required indirection. It would also require that the compiler is adapted. When fibers and multithreading support is built in the language, as in Go, the compiler can do the magic. I would like to underline that the server side of software development is the easiset side to conquer because the client side as to many different GUIs and execution contexts. But it requieres that the performances are on par with other languages like C, C++, Go or Java.
Show window maximized in DlangUI
Hi I'm writing a D Desktop application using DlangUI. I want the window it creates to start maximized (but not Fullscreen) and could not find any APIs in the documentation that seem to do this. Does anyone know how to do this?
what's the right way to get char* from string?
Hello, When I need to call C function, often need to have char* pointer from string. "Interfacing to C++" page: https://dlang.org/spec/cpp_interface.html have following example. extern (C) int strcmp(char* string1, char* string2); import std.string; int myDfunction(char[] s) { return strcmp(std.string.toStringz(s), "foo"); } but this is incorrect because toStringz() returns immutable pointer. One way is to write mutable version of toStringz() char* toStringzMutable(string s) @trusted pure nothrow { auto copy = new char[s.length + 1]; copy[0..s.length] = s[]; copy[s.length] = 0; return copy.ptr; } But I think this is common needs, why it is not provided by Phobos? (or tell me if it has) Thanks, aki
Re: template auto instantiation when parameters empty
On Wednesday, 4 May 2016 at 22:10:16 UTC, Erik Smith wrote: Any ideas? Using an alias could be a solution.