Re: opAssign and const?

2012-05-03 Thread Jonathan M Davis
On Friday, May 04, 2012 08:52:49 Era Scarecrow wrote: > On Friday, 4 May 2012 at 06:48:40 UTC, Jonathan M Davis wrote: > > If you make the one which isn't a ref const as well, it'll > > probably work. > > Yeah I think so too. It will just be either a direct copy or I > cast the object as const a

Re: opAssign and const?

2012-05-03 Thread Era Scarecrow
On Friday, 4 May 2012 at 06:48:40 UTC, Jonathan M Davis wrote: If you make the one which isn't a ref const as well, it'll probably work. Yeah I think so too. It will just be either a direct copy or I cast the object as const and pass it through. Seems like extra work to me and should be hand

Re: Transforming a range back to the original type?

2012-05-03 Thread Jacob Carlborg
On 2012-05-03 00:34, bearophile wrote: The newly redesigned containers in Scala language are often able to do this, but this has required the use of a very advanced static type system, that is currently not in D (maybe there are ways to implement it with D templates, but it will require work to

Re: opAssign and const?

2012-05-03 Thread Jonathan M Davis
On Friday, May 04, 2012 08:32:40 Era Scarecrow wrote: > On Friday, 4 May 2012 at 06:15:21 UTC, Jonathan M Davis wrote: > > I believe that the issue is that x2 isn't const, so when the > > compiler decides which of the two overloads to use, it picks > > the one which doesn't use const. If the const

Re: opAssign and const?

2012-05-03 Thread Era Scarecrow
On Friday, 4 May 2012 at 06:32:41 UTC, Era Scarecrow wrote: opAssing's Hmmm suppose to be OpAssign. Nothing quite like a bug in your automatic text converter right?

Re: opAssign and const?

2012-05-03 Thread Era Scarecrow
On Friday, 4 May 2012 at 06:15:21 UTC, Jonathan M Davis wrote: I believe that the issue is that x2 isn't const, so when the compiler decides which of the two overloads to use, it picks the one which doesn't use const. If the const ref version were the only one, then it would work with x2, but s

Re: Transforming a range back to the original type?

2012-05-03 Thread Jacob Carlborg
On 2012-05-04 01:11, Stewart Gordon wrote: To sum it up, it can't be done in the general case. The range API doesn't know or care about the underlying data structure. That's half the point of it. The underlying data structure might not even exist. An example is a range used as a file stream, a r

Re: ref semantics with hashes

2012-05-03 Thread Andrej Mitrovic
On 5/3/12, Artur Skawina wrote: >alias p1.p2.p3.p4.p5.p6.hash hash3; >alias p1.p2?p1.p2.hash:p3.p4.hash hash4; >alias getfoo().hash hash5; I was under the impression that alias would only be used as a compile-time alias to a nested symbol. So this: alias p1.p2.p3.hash foo; test(foo);

Re: opAssign and const?

2012-05-03 Thread Jonathan M Davis
On Friday, May 04, 2012 07:49:29 Era Scarecrow wrote: > I have the following dilemma. Hopefully I have this right. > > struct X { >ref X opAssign(X x2); >ref X opAssign(ref X x2); > } > > X fn(); > > void func(){ >X x, x2; > >x = x2; //uses ref >x = fn(); //without ref > }

opAssign and const?

2012-05-03 Thread Era Scarecrow
I have the following dilemma. Hopefully I have this right. struct X { ref X opAssign(X x2); ref X opAssign(ref X x2); } X fn(); void func(){ X x, x2; x = x2; //uses ref x = fn(); //without ref } According to the book, this is how it is suppose to be. const is a added promise not

Re: How to prevent direct public creation of a struct?

2012-05-03 Thread Nick Sabalausky
"Brad Anderson" wrote in message news:jhsccvjskiqqzzqbd...@forum.dlang.org... > On Thursday, 3 May 2012 at 21:36:53 UTC, Nick Sabalausky wrote: >> I want to do something like this: >> >> -- >> module moduleFoo; >> >> // Only allow certain values of "str" >>

Re: Transforming a range back to the original type?

2012-05-03 Thread Stewart Gordon
On 02/05/2012 22:01, Jacob Carlborg wrote: Is there a general function for transforming a range back to the original type? If not, would it be possible to create one? To sum it up, it can't be done in the general case. The range API doesn't know or care about the underlying data structure.

Re: Mixins are not inherited?

2012-05-03 Thread Namespace
Very sad, i thougth that was possible. As regards to "instanceof" Try this code: unittest { class A { mixin TRef!(typeof(this)); } class B : A { } class C : B { } A a1 = new B(); A a2 = new C(); assert(instanceof!(A)(a1

Re: How to prevent direct public creation of a struct?

2012-05-03 Thread Jonathan M Davis
On Thursday, May 03, 2012 17:37:47 Nick Sabalausky wrote: > I want to do something like this: > > -- > module moduleFoo; > > // Only allow certain values of "str" > template foo(string str) > { > static assert(str == "a" || str == "b" || str == "c"); > immu

Re: Mixins are not inherited?

2012-05-03 Thread Jonathan M Davis
On Thursday, May 03, 2012 23:32:50 Namespace wrote: > By the following code i get these error messages: > > > Error: template RefTest.Ref.__unittest1.instanceof(T : Object,U : > Object) cannot deduce template function from argument types > !(A)(B) > > Error: template RefTest.Ref.__unittest1.

Re: How to prevent direct public creation of a struct?

2012-05-03 Thread Justin Whear
On Thu, 03 May 2012 17:37:47 -0400, Nick Sabalausky wrote: > The *key* thing here that I'm not sure how to do is: How do I disallow > this?: > > auto b = Foo("b"); // Error: I want to *force* the usage of "template > foo" The @disable annotation can do this, I believe: struct Foo { @disab

Re: How to prevent direct public creation of a struct?

2012-05-03 Thread Brad Anderson
On Thursday, 3 May 2012 at 21:36:53 UTC, Nick Sabalausky wrote: I want to do something like this: -- module moduleFoo; // Only allow certain values of "str" template foo(string str) { static assert(str == "a" || str == "b" || str == "c"); immutabl

How to prevent direct public creation of a struct?

2012-05-03 Thread Nick Sabalausky
I want to do something like this: -- module moduleFoo; // Only allow certain values of "str" template foo(string str) { static assert(str == "a" || str == "b" || str == "c"); immutable foo = Foo(str); } struct Foo { // Only "a", "b", and "c" sh

Mixins are not inherited?

2012-05-03 Thread Namespace
By the following code i get these error messages: Error: template RefTest.Ref.__unittest1.instanceof(T : Object,U : Object) cannot deduce template function from argument types !(A)(B) Error: template RefTest.Ref.__unittest1.instanceof does not match any function template declaration

Re: Fixed-size arrays and randomShuffle()

2012-05-03 Thread bearophile
Vidar Wahlberg: I'm not sure whether this counts as something that should be reported as a bug/improvement, It's a badly written function (with insufficient unit tests): http://d.puremagic.com/issues/show_bug.cgi?id=8026 Bye, bearophile

Re: Fixed-size arrays and randomShuffle()

2012-05-03 Thread Chris Cain
OK, I took a look at your example, and I saw the kind of performance you were seeing. I performed an investigation on what could cause such a disparity and came up with a conclusion. The answer is two things: First of all, as noted above, D's default generator is a mersenne twister RNG. You c

Re: ptrace (process trace system call) on Linux from D

2012-05-03 Thread Matej Nanut
Thank you for the reply, your recommendation works. I could however not find ptrace anywhere in druntime. There is only one mention of it in std.process, in a comment. But I do have some other questions: (1) Can I declare a ptrace function which calls the original ptrace? (I would like to

Re: cannot cast

2012-05-03 Thread Jonathan M Davis
On Thursday, May 03, 2012 10:54:47 Namespace wrote: > On Thursday, 3 May 2012 at 08:46:26 UTC, Chris Cain wrote: > > On Thursday, 3 May 2012 at 08:00:43 UTC, Namespace wrote: > >> So, you mean that if i declared any parameter as const, it > >> have to stay const all the time? > > > > Yes. const =

Re: string find and replace

2012-05-03 Thread Ary Manzana
On 5/3/12 11:01 PM, Ary Manzana wrote: On 5/3/12 9:30 PM, Iain wrote: On Thursday, 3 May 2012 at 14:22:57 UTC, Iain wrote: Forgive me if I am missing something obvious, but is there a simple option for finding all instances of a particular character in a string or char[] and replacing them with

Re: string find and replace

2012-05-03 Thread Ary Manzana
On 5/3/12 9:30 PM, Iain wrote: On Thursday, 3 May 2012 at 14:22:57 UTC, Iain wrote: Forgive me if I am missing something obvious, but is there a simple option for finding all instances of a particular character in a string or char[] and replacing them with another character? I can do this with

Re: Fixed-size arrays and randomShuffle()

2012-05-03 Thread Chris Cain
On a related note, how did you get the other random generators working? I tried to compile this and it gives me an error: -=-=-=- import std.random, std.stdio, std.datetime; void main() { int[] arr = new int[5_000_000]; foreach(i, ref e; arr) e = i; auto rng = MinstdRan

Re: Fixed-size arrays and randomShuffle()

2012-05-03 Thread Vidar Wahlberg
On 2012-05-03 17:31, Chris Cain wrote: You might want to post your code... Sure! D: --- import std.random; import std.stdio; void main() { auto iterations = 1000; int[] a; for (int i = 0; i < 42; ++i) a ~= i; for (int i = 0; i < iterations; ++

Re: Compute in one pass 3 tokens position

2012-05-03 Thread bioinfornatics
Le lundi 30 avril 2012 à 14:52 +0200, bioinfornatics a écrit : > Hi, > I would like to know how compute in on pass 3 tokens position in a > sequence. > curently i do: > File f = File( "reader.d", "r" ); > scope(exit) f.close(); > char[1024] buffer; > char[] content = f.rawRead(buffer); > char[sized

Re: Fixed-size arrays and randomShuffle()

2012-05-03 Thread Chris Cain
On Thursday, 3 May 2012 at 14:41:20 UTC, Vidar Wahlberg wrote: I tried those two as well. Still significantly slower than what I can achieve in Java. You might want to post your code... I wrote this code in D: -=-=-=- import std.random, std.stdio, std.datetime; void main() { int[] arr

Re: Compute in one pass 3 tokens position

2012-05-03 Thread bioinfornatics
Le lundi 30 avril 2012 à 14:52 +0200, bioinfornatics a écrit : > Hi, > I would like to know how compute in on pass 3 tokens position in a > sequence. > curently i do: > File f = File( "reader.d", "r" ); > scope(exit) f.close(); > char[1024] buffer; > char[] content = f.rawRead(buffer); > char[sized

Re: Passing array as const slows down code?

2012-05-03 Thread Artur Skawina
On 05/03/12 16:07, Steven Schveighoffer wrote: > On Wed, 02 May 2012 16:05:13 -0400, Joseph Rushton Wakeling > wrote: > >> On 30/04/12 16:03, Steven Schveighoffer wrote: >>> Try removing the ref and see if it goes back. That usage of ref should not >>> affect anything (if anything it should be s

Re: Fixed-size arrays and randomShuffle()

2012-05-03 Thread Vidar Wahlberg
On 2012-05-03 16:26, Dmitry Olshansky wrote: It's all about RNG used behind the scenes. Default one is Mersane Twister which (AFAIK) is not particularly fast. But has a period of 2^19937 elements. You should probably use XorShift or MinstdRand generator and a version of shuffle with 2nd parameter

Re: string find and replace

2012-05-03 Thread Iain
On Thursday, 3 May 2012 at 14:22:57 UTC, Iain wrote: Forgive me if I am missing something obvious, but is there a simple option for finding all instances of a particular character in a string or char[] and replacing them with another character? I can do this with std.regex, but it seems overk

Re: Fixed-size arrays and randomShuffle()

2012-05-03 Thread Dmitry Olshansky
On 03.05.2012 18:02, Ali Çehreli wrote: > A quick follow-up: > I've tried some various random number engines, but neither come even > close to the performance of whatever is used for Java's > "Collection.shuffle()" method. Perhaps someone can shed some light on this? I have no idea with th

string find and replace

2012-05-03 Thread Iain
Forgive me if I am missing something obvious, but is there a simple option for finding all instances of a particular character in a string or char[] and replacing them with another character? I can do this with std.regex, but it seems overkill, when all I want is the equivalent of PHP's str_re

Re: ref semantics with hashes

2012-05-03 Thread Steven Schveighoffer
On Wed, 02 May 2012 16:32:04 -0400, H. S. Teoh wrote: On Wed, May 02, 2012 at 09:38:35PM +0200, Andrej Mitrovic wrote: [...] So if the hash wasn't already initialized then the reference in the Foo struct is a reference to null, and if you duplicate that reference and add a key the old refere

Re: Access Violation in callback from sort

2012-05-03 Thread Steven Schveighoffer
On Wed, 02 May 2012 11:27:57 -0400, Jabb wrote: Just got the TDPL book and it's a great read! I learn best when typing out the code myself, so I decided to make a single VisualD project and put the different exercises in separate modules. I am having problems with sort in std.algorithms -

Re: Passing array as const slows down code?

2012-05-03 Thread Steven Schveighoffer
On Wed, 02 May 2012 16:05:13 -0400, Joseph Rushton Wakeling wrote: On 30/04/12 16:03, Steven Schveighoffer wrote: Try removing the ref and see if it goes back. That usage of ref should not affect anything (if anything it should be slower, since it's an extra level of indirection). Remo

Re: Fixed-size arrays and randomShuffle()

2012-05-03 Thread Ali Çehreli
On 05/03/2012 06:55 AM, Vidar Wahlberg wrote: > On 2012-05-03 15:34, Ali Çehreli wrote: >> Fixed-length arrays are value types. 'a' is copied to randomShuffle() so >> its copy is shuffled. Passing a slice of the whole array works: >> >> randomShuffle(a[]); > > True, it is however still not excepti

Re: Fixed-size arrays and randomShuffle()

2012-05-03 Thread ixid
Why would you not want it to shuffle a fixed array? That's a very frustrating silent failure.

Re: Transforming a range back to the original type?

2012-05-03 Thread Simen Kjaeraas
On Thu, 03 May 2012 13:17:40 +0200, Jacob Carlborg wrote: On 2012-05-03 09:43, Simen Kjaeraas wrote: In addition to std.array.array, as others have pointed out, there is also std.range.InputRangeObject. I'm not sure if I understand what InputRangeObject does. But I don't think it does wha

Re: Fixed-size arrays and randomShuffle()

2012-05-03 Thread Vidar Wahlberg
On 2012-05-03 15:34, Ali Çehreli wrote: Fixed-length arrays are value types. 'a' is copied to randomShuffle() so its copy is shuffled. Passing a slice of the whole array works: randomShuffle(a[]); True, it is however still not exceptionally newbie (or perhaps even user?) friendly (my question

Re: Fixed-size arrays and randomShuffle()

2012-05-03 Thread Ali Çehreli
On 05/03/2012 06:30 AM, Vidar Wahlberg wrote: May be that this works as intended, but it fooled me: --- import std.random; import std.stdio; void main() { int[5] a = 0; a[0] = 1; int[] b = [1, 0, 0, 0, 0]; randomShuffle(a); Fixed-length arrays are value types. 'a' is copied to randomShuffle() s

Fixed-size arrays and randomShuffle()

2012-05-03 Thread Vidar Wahlberg
May be that this works as intended, but it fooled me: --- import std.random; import std.stdio; void main() { int[5] a = 0; a[0] = 1; int[] b = [1, 0, 0, 0, 0]; randomShuffle(a); writeln(a); randomShuffle(b); writeln(b); } --- In DMD 2.0.59 t

Re: Transforming a range back to the original type?

2012-05-03 Thread Jacob Carlborg
On 2012-05-03 09:43, Simen Kjaeraas wrote: In addition to std.array.array, as others have pointed out, there is also std.range.InputRangeObject. I'm not sure if I understand what InputRangeObject does. But I don't think it does what I want. -- /Jacob Carlborg

Re: ref semantics with hashes

2012-05-03 Thread Artur Skawina
On 05/03/12 00:38, Andrej Mitrovic wrote: > In fact what would *actually* solve this problem (for me) is if/when > we have aliases implemented for this scenario: > > import std.stdio; > > struct Foo > { >int[] hash; > } > > void main() > { > Foo foo; > alias foo.hash hash2; > >

Re: extern and opaque structs

2012-05-03 Thread Jacob Carlborg
On 2012-05-03 07:00, James Miller wrote: I'm doing C bindings and I have an opaque struct and an extern'd variable of the type of that struct. The problem is that dmd is complaining that the struct has no definition (which is true). Making it a pointer works (expected) but i can't do that because

Re: cannot cast

2012-05-03 Thread Chris Cain
On Thursday, 3 May 2012 at 09:00:08 UTC, Chris Cain wrote: const = you (as in, your view of the data as you're working with it) can't change Actually, let me be even clearer with this... I mean _you_ cannot change it, but it might be changed by someone else's view, in which case it would appe

Re: cannot cast

2012-05-03 Thread Chris Cain
On Thursday, 3 May 2012 at 08:54:48 UTC, Namespace wrote: I thought that const = "cannot change directly" and immutable stands for "cannot change all the time". If not, why exist both storage classes beside? const = you (as in, your view of the data as you're working with it) can't change imm

Re: cannot cast

2012-05-03 Thread Namespace
On Thursday, 3 May 2012 at 08:46:26 UTC, Chris Cain wrote: On Thursday, 3 May 2012 at 08:00:43 UTC, Namespace wrote: So, you mean that if i declared any parameter as const, it have to stay const all the time? Yes. const = you can't change. Changing it is invalid behavior. Imagine const/immuta

Re: cannot cast

2012-05-03 Thread Chris Cain
On Thursday, 3 May 2012 at 08:00:43 UTC, Namespace wrote: So, you mean that if i declared any parameter as const, it have to stay const all the time? Yes. const = you can't change. Changing it is invalid behavior. Imagine const/immutable as bits in readonly memory and you'll have to right min

Re: Transforming a range back to the original type?

2012-05-03 Thread Jacob Carlborg
On 2012-05-02 23:40, Jonathan M Davis wrote: On Wednesday, May 02, 2012 23:01:21 Jacob Carlborg wrote: Is there a general function for transforming a range back to the original type? If not, would it be possible to create one? You mean that if you have something like auto range = getRangeFrom

Re: cannot cast

2012-05-03 Thread sclytrack
On 05/03/2012 09:33 AM, Namespace wrote: On Wednesday, 2 May 2012 at 22:38:36 UTC, Namespace wrote: Other, shorter example: [code] import std.stdio, std.traits; class A { int val; alias val this; T opCast(T : Object)() { writeln("FOO"); return to!(T)(this); } } class B : A { } T to(T : O

Re: cannot cast

2012-05-03 Thread Namespace
On Thursday, 3 May 2012 at 07:41:32 UTC, Simen Kjaeraas wrote: On Thu, 03 May 2012 00:38:35 +0200, Namespace wrote: I'm not very skillful in such "template" stories. Maybe someone can help me? The main problem here is your opCast is non-const. (it's always an indication of const problems w

Re: cannot cast

2012-05-03 Thread Namespace
If you want to restrict opCast, then use a template constraint, constraining it to what you want to work with it. Also, casting away const is generally a bad idea in D. Casting away const and mutating a variable is an _extremely_ bad idea. You _really_ shouldn't be doing it. So, the fact that y

Re: Transforming a range back to the original type?

2012-05-03 Thread Jacob Carlborg
On 2012-05-02 23:07, Matt Soucy wrote: On 05/02/2012 05:01 PM, Jacob Carlborg wrote: Is there a general function for transforming a range back to the original type? If not, would it be possible to create one? I believe std.array's array function does what you want. -Matt I was thinking of a

Re: cannot cast

2012-05-03 Thread Namespace
On Thursday, 3 May 2012 at 07:41:32 UTC, Simen Kjaeraas wrote: On Thu, 03 May 2012 00:38:35 +0200, Namespace wrote: I'm not very skillful in such "template" stories. Maybe someone can help me? The main problem here is your opCast is non-const. (it's always an indication of const problems w

Re: cannot cast

2012-05-03 Thread Jonathan M Davis
On Thursday, May 03, 2012 09:33:01 Namespace wrote: > On Wednesday, 2 May 2012 at 22:38:36 UTC, Namespace wrote: > > Other, shorter example: > > > > [code] > > import std.stdio, std.traits; > > > > class A { > > > > int val; > > > > alias val this; > > > > T opCast(T : Obje

Re: Transforming a range back to the original type?

2012-05-03 Thread Simen Kjaeraas
On Wed, 02 May 2012 23:01:21 +0200, Jacob Carlborg wrote: Is there a general function for transforming a range back to the original type? If not, would it be possible to create one? In addition to std.array.array, as others have pointed out, there is also std.range.InputRangeObject.

Re: cannot cast

2012-05-03 Thread Simen Kjaeraas
On Thu, 03 May 2012 00:38:35 +0200, Namespace wrote: I'm not very skillful in such "template" stories. Maybe someone can help me? The main problem here is your opCast is non-const. (it's always an indication of const problems when DMD says " is not callable using argument types ()") So

Re: cannot cast

2012-05-03 Thread Namespace
On Wednesday, 2 May 2012 at 22:38:36 UTC, Namespace wrote: Other, shorter example: [code] import std.stdio, std.traits; class A { int val; alias val this; T opCast(T : Object)() { writeln("FOO"); return to!(T)(th