Trouble with template parameter matching
[code] void func1(N)( const N name ) if( is(N: string) || is(N: char[]) ) { func2( name ); } void func2(N)( const N name ) if( is(N: string) || is(N: char[]) ) {} void main(){ char[] blah = ['b', 'l', 'a', 'h']; func1( blah ); //func1( "blah" );// this works } [/code] [result] test.d(4): Error: template test.func2 cannot deduce function from argument types !()(const(char[])), candidates are: test.d(7):test.func2(N)(const N name) if (is(N : string) || is(N : char[])) [/result] When func1 is called with blah variable, I assume that N is char[]. From there, when func2 is called by func1, name should be const(char[]). But since func2 defined name parameter with const, shouldn't the compiler accept const part of const(char[]) as func2's const, and accept N as char[] still? Otherwise, it is being weirdly recursive, and requires casting.
Re: Trouble with template parameter matching
On Sunday, 2 August 2015 at 08:08:05 UTC, tcak wrote: [code] void func1(N)( const N name ) if( is(N: string) || is(N: char[]) ) { func2( name ); } [...] This seems like the reasonable behavior to me. Perhaps you should use Unqual? http://dlang.org/phobos/std_traits.html#Unqual
Randomisation of array order
I was planning to use a dynamic array of indices to represent a deck of cards, and was wondering if there was any easy way to "shuffle" the arrays contents? I checked the library docs, but came to the conclusion that sorting arrays is a much more common operation :) If anyone has a suggestion on a good way to implement this, I'd appreciate it. I don't need you to code it for me ( although I wouldn't turn it down if you've already done it), just a suggestion of what to do would be appreciated
Re: Randomisation of array order
...And then I realised that I hadn't looked inside std.random. Question solved, because I am a dumbass.
Re: Randomisation of array order
On Sunday, 2 August 2015 at 09:24:12 UTC, Matt wrote: I was planning to use a dynamic array of indices to represent a deck of cards, and was wondering if there was any easy way to "shuffle" the arrays contents? I checked the library docs, but came to the conclusion that sorting arrays is a much more common operation :) If anyone has a suggestion on a good way to implement this, I'd appreciate it. I don't need you to code it for me ( although I wouldn't turn it down if you've already done it), just a suggestion of what to do would be appreciated Have you checked std.random ? import std.random, std.stdio; void main() { auto arr = [1, 2, 3, 4, 5]; arr.randomShuffle; arr.writeln; }
Re: How to run opengl tutorials
Yes, I was so excited about Dlang that i forgot to paste the error: Here's the link to imagescreen http://prntscr.com/7zwe6h
Re: How disruptive is the GC?
On Wednesday, 29 July 2015 at 09:25:50 UTC, Snape wrote: I'm in the early stages of building a little game with OpenGL (in D) and I just want to know the facts about the GC before I decide to either use it or work around it. Lots of people have said lots of things about it, but some of that information is old, so as of today, what effect does the GC have on the smooth operation of a real-time application? Is it pretty noticeable with any use of the GC or only if you're deallocating large chunks at a time? I'd like to add that you can tell the runtime (and therefore the GC) to ignore a thread: http://dlang.org/phobos/core_thread.html#.thread_detachThis This allows you to implement real-time tasks in that thread, and it will never be interrupted by the GC. You can still use the GC in other threads. Of course, you need to make sure not to call into the GC from the detached thread (easy by using @nogc), and to keep at least one reference to GC data used in the detached thread in a normal thread.
Re: How disruptive is the GC?
I'm writing a game engine in D. Try to minimize allocations and that's will be OK. I'm using delegates and all the phobos stuff. I allocate only in few places at every frame. So i can reach 1K fps on a complicated scene. GC is not a problem. DMD optimizes so ugly that all the math is very, very slow. DMD gives me about 200 fps, when with LDC i can reach 1k.
Re: Struct that destroys its original handle on copy-by-value
On 02/08/15 03:38, Dicebot via Digitalmars-d-learn wrote: On Saturday, 1 August 2015 at 17:50:28 UTC, John Colvin wrote: I'm not sure how good an idea it is to totally enforce a range to be non-copyable, even if you could deal with the function call chain problem. Even in totally save-aware code, there can still be valid assignment of a range type. I'm pretty sure a lot of phobos ranges/algorithms would be unusable. This is exactly why I proposed to Joe design with destructive copy originally - that would work with any algorithms expecting implicit pass by value but prevent from actual double usage. Sadly, this does not seem to be implementable in D in any reasonable way. Yup. This work is follow-up on a really creative bunch of suggestions Dicebot made to me on our flight back from DConf, and which we followed up on at the recent Berlin meetup. The design principle of "destructive copy" is great -- it really cuts through a bunch of potential nastinesses around random number generation -- but it really doesn't look like it's straightforwardly possible :-(
Re: store template value
Oh, neat. This saves the day :) 2015-08-01 23:22 GMT+02:00 Ali Çehreli : > On 08/01/2015 08:37 AM, maarten van damme via Digitalmars-d-learn wrote: > >> I have a class that creates a task in it's constructor. How do I store >> this >> created task as one of it's value members and later on call .yieldForce()? >> >> > Tasks can be created with a function pointer 'function parameter' as well. > (This has already been added to "Programming in D" but it is not available > on the web site yet.) > > I learned the exact type by the help of pragma(msg) below and used it to > create MyTask and myTasks: > > import std.parallelism; > > double foo(int i) > { > return i * 1.5; > } > > double bar(int i) > { > return i * 2.5; > } > > void main() > { > auto tasks = [ task(&foo, 1), >task(&bar, 2) ];// ← compiles > > pragma(msg, typeof(tasks[0])); > > alias MyTask = Task!(run, double function(int), int)*; > > MyTask[] myTasks; > myTasks ~= task(&foo, 1); > myTasks ~= task(&bar, 2); > } > > Ali > >
Re: Trouble with template parameter matching
On Sunday, 2 August 2015 at 08:08:05 UTC, tcak wrote: [code] void func1(N)( const N name ) if( is(N: string) || is(N: char[]) ) { func2( name ); } void func2(N)( const N name ) if( is(N: string) || is(N: char[]) ) {} void main(){ char[] blah = ['b', 'l', 'a', 'h']; func1( blah ); //func1( "blah" );// this works } [/code] [result] test.d(4): Error: template test.func2 cannot deduce function from argument types !()(const(char[])), candidates are: test.d(7):test.func2(N)(const N name) if (is(N : string) || is(N : char[])) [/result] When func1 is called with blah variable, I assume that N is char[]. Yup. From there, when func2 is called by func1, name should be const(char[]). Yup. But since func2 defined name parameter with const, shouldn't the compiler accept const part of const(char[]) as func2's const, and accept N as char[] still? Nope. When removing the top level const of `const(char[])`, it becomes `const(char)[]`. Test for that in your template constraint and it works.
Dynamic bindings to global variables
Hello I wrote static bindings to a C library. I want to also offer a version(Dynamic) of the bindings. I follow and use derelict-util to get the address of function pointers. In the library I bind, there is also global variables. here's one example in the static bindings: extern __gshared wl_interface wl_display_interface; (wl_interface is a struct, not a pointer alias) How would one make dynamic binding of this? Is it possible to retrieve the address of the symbol and use std.conv.emplace? thank you
Re: Dynamic bindings to global variables
On Sunday, 2 August 2015 at 15:31:48 UTC, remi thebault wrote: Hello I wrote static bindings to a C library. I want to also offer a version(Dynamic) of the bindings. I follow and use derelict-util to get the address of function pointers. In the library I bind, there is also global variables. here's one example in the static bindings: extern __gshared wl_interface wl_display_interface; (wl_interface is a struct, not a pointer alias) How would one make dynamic binding of this? Is it possible to retrieve the address of the symbol and use std.conv.emplace? You have to declare it as a pointer, then retrieve the pointer in the same way you do the function pointers, via the system loader (GetProcAddress/dlsym).
Re: Dynamic bindings to global variables
On Sunday, 2 August 2015 at 15:38:34 UTC, Mike Parker wrote: You have to declare it as a pointer, then retrieve the pointer in the same way you do the function pointers, via the system loader (GetProcAddress/dlsym). and how would you make this source compatible with the static binding version? I guess it is not possible extern(C): version(Dynamic) { __gshared wl_interface *wl_display_interface; } else { extern __gshared wl_interface wl_display_interface; } // use case: some_func expects a pointer to wl_interface // the C version is using addressof operator, (as do the static D bindings) some_func(&wl_display_interface);
Is Key Type?
is there a trait in D or Phobos which will tell you if a type can be used as a key for an associative array? For example, where T is some type: static assert(isKeyType!T) int[T] hashTable = ...
Re: Is Key Type?
On Sunday, 2 August 2015 at 17:55:16 UTC, Xinok wrote: is there a trait in D or Phobos which will tell you if a type can be used as a key for an associative array? For example, where T is some type: static assert(isKeyType!T) int[T] hashTable = ... import std.stdio; enum isKeyType(T) = __traits(compiles, int[T]); class Foo {} struct Bar {} void main(string[] args) { // Success assert(isKeyType!(int)); assert(isKeyType!(string)); assert(isKeyType!(Foo)); assert(isKeyType!(Bar)); // Fail assert(isKeyType!(void)); assert(isKeyType!(delegate(){})); assert(isKeyType!(function(){})); }
Re: Dynamic bindings to global variables
On Sunday, 2 August 2015 at 16:20:29 UTC, remi thebault wrote: On Sunday, 2 August 2015 at 15:38:34 UTC, Mike Parker wrote: You have to declare it as a pointer, then retrieve the pointer in the same way you do the function pointers, via the system loader (GetProcAddress/dlsym). and how would you make this source compatible with the static binding version? I guess it is not possible extern(C): version(Dynamic) { __gshared wl_interface *wl_display_interface; } else { extern __gshared wl_interface wl_display_interface; } // use case: some_func expects a pointer to wl_interface // the C version is using addressof operator, (as do the static D bindings) some_func(&wl_display_interface); I'll manage this through static @property-ies thanks for your help Rémi
Using rdmd to create shared object files
Hello, I am trying to use rdmd to create shared object files. The command that I am using is "rdmd --build-only -shared -fPIC -defaultlib= foo.d" This creates a file called "foo" - wich is not exactly what I expectd. However "dmd -shared -fPIC -defaultlib= foo.d " creates a file called "foo.so" - that is what i expect and need. The command ill be actually using will be similar to this: "find ../script -name *.d -exec dmd -I../../deps/ -I../../source/ -fPIC -shared -debug -g -defaultlib= {} \;" The amount of files that will be compiled by this are really likely going to increase over time, so using rdmd here would be nice in terms of compile time. The issues i have is that rdmd dosnt create .so files but (at least on my linux) creates them without a file exenstion. I could rename them with -of but that would increase this ugly find-command even more. The second thing (wich isnt such a big issue): I have to use "--build-only" for rdmd because it will try to run the newly created shared object. Also, not so on-topic to be asked here: Is there a nicer solution for the "all .d files in this directory and the ones below"? I remeber its possible to do something like "dmd ./**/*.d" but I cant get that to work... Thanks Malte
Re: store template value
On 08/02/2015 05:15 AM, maarten van damme via Digitalmars-d-learn wrote: > Oh, neat. This saves the day :) Awesome! :) However, that solution depends on the implementation details of std.parallelism. It is possible to do the same thing by inheriting from an 'interface' and "hiding" the templated type under there. The following program need not spell out the template instance: import std.parallelism; double foo(int i) { return i * 1.5; } double bar(int i) { return i * 2.5; } /* This is what the user code will need to know about. */ interface MyTask { void executeInNewThread(); void yieldForce(); } /* This subclass hides the actual templated Task type. */ class MyTaskImpl(TaskType) : MyTask { TaskType t; this(TaskType t) { this.t = t; } /* These two are to satisfy the interface requirements. */ void executeInNewThread() { t.executeInNewThread(); } void yieldForce() { t.yieldForce(); } } /* A convenience function to hide the Task template instance. */ auto newMyTask(TaskType)(TaskType taskObject) { return new MyTaskImpl!TaskType(taskObject); } void main() { /* Populate */ MyTask[] myTasks; myTasks ~= newMyTask(task!foo(1)); myTasks ~= newMyTask(task!bar(2)); import std.algorithm; /* Execute */ myTasks.each!(t => t.executeInNewThread); /* Wait */ myTasks.each!(t => t.yieldForce); } Ali
Re: Array start index
On Saturday, 1 August 2015 at 23:02:51 UTC, bachmeier wrote: But what type of programming are you doing? Even after decades of programming and trying out dozens of languages, zero-based indexing still gets me at times when the arrays I work with represent vectors and matrices. Especially when porting code from other languages that use one-based indexing. One of the nice things about D is that it gives you the tools to easily make the change if you want. Adding 1-indexed arrays to the language fixes nothing. Just write your 1-indexed array type and if you enjoy using it, publish it as a library. Who knows, if demand is high it may even end up in phobos.
!in operator
Is my understanding below correct? Does any documentation need updating? Operator precedence table lists !in as an operator: http://wiki.dlang.org/Operator_precedence Operator overloading documentation does not mention it: http://dlang.org/operatoroverloading.html#binary However, 'a !in b' seems to be lowered to '!(a in b)'. It is possible to define "!in" but it is never called: struct S { bool opBinaryRight(string op)(int i) const if (op == "in") { import std.stdio; writeln("in"); return true; } bool opBinaryRight(string op)(int i) const if (op == "!in") { // Never called assert(false); return false; } } void main() { auto s = S(); assert(42 in s); assert(!(42 !in s)); } The "in" overload gets called twice: in in Ali