Template method and type resolution of return type

2014-04-19 Thread matovitch via Digitalmars-d-learn
Hi everyone ! Let's say I have a struct : struct Test { immutable (ubyte)[] data; T get(T)() { return *(cast(T*)(&(data[0]))); } } This code will work : import std.stdio; void main() { Test t; t.data = [152, 32, 64, 28, 95];

Re: Template method and type resolution of return type

2014-04-19 Thread matovitch via Digitalmars-d-learn
On Saturday, 19 April 2014 at 17:49:54 UTC, bearophile wrote: matovitch: struct Test { immutable (ubyte)[] data; T get(T)() { return *(cast(T*)(&(data[0]))); } It's better to return const/immutable data. Otherwise the program gives undefined r

Re: Template method and type resolution of return type

2014-04-19 Thread matovitch via Digitalmars-d-learn
On Saturday, 19 April 2014 at 18:46:28 UTC, matovitch wrote: On Saturday, 19 April 2014 at 17:49:54 UTC, bearophile wrote: matovitch: I did'nt know that one. This last sentence is misleading, let be clear : I know almost nothing when it comes to D...but I'm willing to learn ! ;-)

Re: Template method and type resolution of return type

2014-04-19 Thread matovitch via Digitalmars-d-learn
On Saturday, 19 April 2014 at 18:46:28 UTC, matovitch wrote: On Saturday, 19 April 2014 at 17:49:54 UTC, bearophile wrote: matovitch: struct Test { immutable (ubyte)[] data; T get(T)() { return *(cast(T*)(&(data[0]))); } It's better to return

Re: Template method and type resolution of return type

2014-04-20 Thread matovitch via Digitalmars-d-learn
On Sunday, 20 April 2014 at 00:55:31 UTC, David Held wrote: On 4/19/2014 3:31 PM, Andrej Mitrovic via Digitalmars-d-learn wrote: [...] struct S { int get() { return 0; } T get(T)() { return T.init; } } void main() { S s; float x = s.get(); // which overload? (currently int ge

Re: Template method and type resolution of return type

2014-04-20 Thread matovitch via Digitalmars-d-learn
On Sunday, 20 April 2014 at 08:28:07 UTC, monarch_dodra wrote: On Sunday, 20 April 2014 at 07:52:08 UTC, matovitch wrote: struct S { ubyte get() { return 0 ; } float get() { return 0.; } } void main() { S s; float x = s.get(); // does'nt know which overload, does'nt compile. } W

dmd segfaults

2014-05-31 Thread matovitch via Digitalmars-d-learn
Hi ! Does anybody knows why dmd segfaults on this code ? Should I report this as a bug ? import std.stdio; enum LiftingGender { PREDICT, UPDATE, } struct Test(float[][] coeffs, int[] offsets, LiftingGender gender) { immutable float[][] coeffs = coeffs;

Re: dmd segfaults

2014-05-31 Thread matovitch via Digitalmars-d-learn
On Saturday, 31 May 2014 at 17:01:23 UTC, bearophile wrote: matovitch: Does anybody knows why dmd segfaults on this code ? Should I report this as a bug ? Please report this minimized case to Bugzilla: struct Foo(int[] arr) { const int[] arr = arr; } void main() { Foo!([0]) foo; }

Re: dmd segfaults

2014-05-31 Thread matovitch via Digitalmars-d-learn
In fact it segfauls on any template parameter if it has the same name as the immutable member (at least it's coherent). Something as simple as : struct Foo(int i) { immutable int i = i; } void main() { Foo!5 foo; writeln(foo); } I am suprised that nobody tried this before. BTW I a

Re: dmd segfaults

2014-05-31 Thread matovitch via Digitalmars-d-learn
I remembered my psswd don't take my last sentence into account (i will filed this).

Re: dmd segfaults

2014-05-31 Thread matovitch via Digitalmars-d-learn
I updated the issue. Strangely if done in the main everything is fine : Error: undefined identifier i

Multiple alias this failed workaround...obscure error message

2014-06-11 Thread matovitch via Digitalmars-d-learn
I was looking for a workaround to multiple alias this (or opImplicitCast) the following trick doesn't work (why shouldn't it ?). The error message is quite obscure to me. import std.stdio; class A(Derived) { alias cast(ref Derived)(this).x this; } class B : A!B { float x; } class C

Re: Multiple alias this failed workaround...obscure error message

2014-06-11 Thread matovitch via Digitalmars-d-learn
On Wednesday, 11 June 2014 at 20:53:21 UTC, Jonathan M Davis via Digitalmars-d-learn wrote: I don't believe that it's legal to use a cast in an alias declaration, and that's certainly what the error seems to be indicating. Also, using ref in a cast is definitely illegal regardless of where the

Re: Multiple alias this failed workaround...obscure error message

2014-06-11 Thread matovitch via Digitalmars-d-learn
If I quote de documentation : "Any casting of a class reference to a derived class reference is done with a runtime check to make sure it really is a downcast. null is the result if it isn't. Note: This is equivalent to the behavior of the dynamic_cast operator in C++." I explicitly kept tra

Re: A nice D coding pattern

2014-11-25 Thread matovitch via Digitalmars-d-learn
On Monday, 24 November 2014 at 22:50:33 UTC, bearophile wrote: In some D programs I'm using this coding pattern: You can see an example of this pattern that I've used here: http://rosettacode.org/wiki/Solve_a_Hopido_puzzle#D Bye, bearophile Awesome gist and great pattern ! Sometimes your foru

D's type classes pattern ?

2015-03-24 Thread matovitch via Digitalmars-d-learn
Hi, It's been a long time since I coded some d code... sorry I take the lazy way asking for advices. :D Lets say I want to implement some generic algorithm. I would like to checks the types passed to my algorithm implements a specific interface. interface IStuff(Stuff) { void foo(); } cla

Re: D's type classes pattern ?

2015-03-24 Thread matovitch via Digitalmars-d-learn
More like : import std.stdio; interface IStuff(Stuff) { void foo(); } class TypeClass(T, I) : I(T) { alias this stuff; T stuff; } void myAwesomeAlgo(Stuff) (TypeClass!(Stuff, IStuff) stuff) { stuff.foo(); } struct MyStuff { void foo() { writeln("Hello World

Re: D's type classes pattern ?

2015-03-24 Thread matovitch via Digitalmars-d-learn
Well, just follow that link to the code...it almost compile : http://dpaste.com/3JNP0QD.

Re: D's type classes pattern ?

2015-03-24 Thread matovitch via Digitalmars-d-learn
well, alias this is the issue here. interface I { void foo(); } struct S { void foo() {} } class C : I { S s; alias this s; } don't compile...if you have any idea to do otherwise I am greatly interested. Thanks !

Re: BigInt and xor

2015-03-24 Thread matovitch via Digitalmars-d-learn
On Tuesday, 24 March 2015 at 15:45:36 UTC, Dennis Ritchie wrote: Tell me, please, how can I replace this code? import std.conv : to; import std.bigint : BigInt; import std.string : format; import std.stdio : writeln; void main() { BigInt[10] bitArr; ulong n = 18_446_724_073_70

Re: D's type classes pattern ?

2015-03-24 Thread matovitch via Digitalmars-d-learn
To resume my goal (last lonely message I promise), how can you statically check a type implement an interface without making this type inherit the interface (otherwise std.traits would do it) ? ps : it seems to me that this is exactly what the haskell compiler do with type classes <- layman o

Re: D's type classes pattern ?

2015-03-24 Thread matovitch via Digitalmars-d-learn
Wait no ! In that case my type will have to inherit the interface...I don't want that, checking without inheriting...I know thats weird.

Re: D's type classes pattern ?

2015-03-24 Thread matovitch via Digitalmars-d-learn
On Tuesday, 24 March 2015 at 16:44:54 UTC, weaselcat wrote: On Tuesday, 24 March 2015 at 15:51:00 UTC, matovitch wrote: Hi, It's been a long time since I coded some d code... sorry I take the lazy way asking for advices. :D Lets say I want to implement some generic algorithm. I would like to

Re: BigInt and xor

2015-03-24 Thread matovitch via Digitalmars-d-learn
On Tuesday, 24 March 2015 at 17:28:50 UTC, Dennis Ritchie wrote: On Tuesday, 24 March 2015 at 16:35:04 UTC, Ivan Kazmenko wrote: What exactly is not working? Everything works. I'm just a little forgotten properties of the operation xor. I just wanted to xor 1 each digit in the number of typ

Re: D's type classes pattern ?

2015-03-25 Thread matovitch via Digitalmars-d-learn
Thanks for the precisions on template constraint and template specialization...Indeed wath I want to do look like isInputRange constraint. Haskell have something like : //(Pseudo D-Haskell) void foo(InputRange R)(R r); //D equivalent void foo(R)(R r) if (isInputRange(R)); Except they call the

Re: D's type classes pattern ?

2015-03-25 Thread matovitch via Digitalmars-d-learn
On Wednesday, 25 March 2015 at 08:55:14 UTC, bearophile wrote: matovitch: I am curious to know how isInputRange is implemented since I wanted to do kind of the same but I am afraid it's full of (ugly) traits and template trickeries where haskell type classes are quite neat and essentially a d

reinterpret_cast float to uint

2015-03-29 Thread matovitch via Digitalmars-d-learn
Hi, floats are stored on 32 bits using ieee754...and I would like (for some obscure reason) to reinterpret a such float into a 32 bits uint (i.e without altering the memory). A simple : import std.stdio; void main() { float f = 0.5; uint i = cast(uint)(f); writeln(i); } doesn't

Re: reinterpret_cast float to uint

2015-03-29 Thread matovitch via Digitalmars-d-learn
On Sunday, 29 March 2015 at 13:39:47 UTC, matovitch wrote: Hi, floats are stored on 32 bits using ieee754...and I would like (for some obscure reason) to reinterpret a such float into a 32 bits uint (i.e without altering the memory). A simple : import std.stdio; void main() { float f =

Re: reinterpret_cast float to uint

2015-03-29 Thread matovitch via Digitalmars-d-learn
On Sunday, 29 March 2015 at 14:50:24 UTC, ketmar wrote: On Sun, 29 Mar 2015 13:45:10 +, matovitch wrote: you can also use unions. Good idea ! In my case I think it was better to cast, but this could be helpful another time thanks ! :)

rvalue based copy

2015-03-30 Thread matovitch via Digitalmars-d-learn
Hi, Surely I am misunderstanding something. I got something like this : struct S { void opAssign(const ref s) { //... } } S genS() { S s; //... return s; } main() { S s; s = genS(); } DMD says : ...opAssign (ref const(S) point) is not callable using a

Re: rvalue based copy

2015-03-30 Thread matovitch via Digitalmars-d-learn
void opAssign(const ref s) should be void opAssign(const ref S s) btw and btw bis, I should probably make it const ref SopAssign(const ref S s) :/ I stop flooding there.

Re: rvalue based copy

2015-03-30 Thread matovitch via Digitalmars-d-learn
The title should be assignement not copy.

Re: rvalue based copy

2015-03-30 Thread matovitch via Digitalmars-d-learn
On Monday, 30 March 2015 at 17:14:27 UTC, Adam D. Ruppe wrote: On Monday, 30 March 2015 at 17:09:14 UTC, matovitch wrote: (I am gessing ref argument explitly means no rvalue) That's right. I'd first say don't use ref, just use "const S" and it will work and probably do what you need efficient

Re: rvalue based copy

2015-03-30 Thread matovitch via Digitalmars-d-learn
On Monday, 30 March 2015 at 17:21:53 UTC, Steven Schveighoffer wrote: Annoying as this is (and blatantly awkward), it saves you from having to implement twice: void opAssign(T)(auto ref const T s) if(is(T == S)) {...} Yep, this seems awkward to me too thought according to Adam one can do :

Re: Mapping with partial

2015-03-30 Thread matovitch via Digitalmars-d-learn
(it's not on line 79 obviously, you got me :D)

Mapping with partial

2015-03-30 Thread matovitch via Digitalmars-d-learn
Hi again, I have this simple toy code : import point; import std.random; import std.algorithm; import std.functional; void getRandomPoint(R)(R randVar, ref Point p) { p.x = randVar; p.y = randVar; p.z = randVar; } void main() { Point[500] points; auto randVar = un

Re: Mapping with partial

2015-03-30 Thread matovitch via Digitalmars-d-learn
On Monday, 30 March 2015 at 18:23:32 UTC, Adam D. Ruppe wrote: On Monday, 30 March 2015 at 18:07:18 UTC, matovitch wrote: kmeans_example.d(79): Error: template std.algorithm.iteration.map That error is easy: use points[].map!(test) instead of points.map. Since points is a static array, it i

Re: Mapping with partial

2015-03-30 Thread matovitch via Digitalmars-d-learn
On Monday, 30 March 2015 at 18:34:19 UTC, Adam D. Ruppe wrote: On Monday, 30 March 2015 at 18:29:32 UTC, matovitch wrote: I tried importing std.range and points.array works too. Aye, that would work too, but the slice I think is more efficient as I'm pretty sure... not completely sure, but I

Re: Mapping with partial

2015-03-30 Thread matovitch via Digitalmars-d-learn
Well I have a bit of a similar problem with foreach. If I use classic T[] range, I can do : foreach(int i, auto t, myRange)... But if I use an Array!T (from std.container) I get : cannot infer argument types, expected 1 argument, not 2 Even if I add the brackets []. Any idea ? Thanks for your

Re: Mapping with partial

2015-03-30 Thread matovitch via Digitalmars-d-learn
On Monday, 30 March 2015 at 19:08:24 UTC, anonymous wrote: On Monday, 30 March 2015 at 18:37:53 UTC, matovitch wrote: On Monday, 30 March 2015 at 18:34:19 UTC, Adam D. Ruppe wrote: [...] Aye, that would work too, but the slice I think is more efficient as I'm pretty sure... not completely sure

Re: Mapping with partial

2015-03-30 Thread matovitch via Digitalmars-d-learn
Language ref -> Array -> Slice "An array slice does not copy the data, it is only another reference to it." So the total slice of a static array is a range using the underlying memory of the static array isnt it ?

Re: Mapping with partial

2015-03-30 Thread matovitch via Digitalmars-d-learn
Thanks. On a sader note, I found a respons'less thread about my second question : http://forum.dlang.org/thread/mailman.2247.1353945423.5162.digitalmars-d-le...@puremagic.com "where std.container.Array is concerned: how come I can't use a foreach(i, x; myArray) formulation? I.e. one where th

Re: Mapping with partial

2015-03-30 Thread matovitch via Digitalmars-d-learn
On Monday, 30 March 2015 at 19:31:54 UTC, anonymous wrote: On Monday, 30 March 2015 at 19:03:05 UTC, matovitch wrote: Well I have a bit of a similar problem with foreach. If I use classic T[] range, I can do : foreach(int i, auto t, myRange)... But if I use an Array!T (from std.container) I g

What ?

2015-03-30 Thread matovitch via Digitalmars-d-learn
Hi again again, ulong u = 1 << 63; Raise : Error: shift by 63 is outside the range 0..31 This is a bug isn't it, the ulong are supposed to be on 64 bits ? I guess it's time I go to bed. Have a nice night !

Re: What ?

2015-03-30 Thread matovitch via Digitalmars-d-learn
On Monday, 30 March 2015 at 22:34:55 UTC, Vladimir Panteleev wrote: On Monday, 30 March 2015 at 22:34:00 UTC, matovitch wrote: Hi again again, ulong u = 1 << 63; Raise : Error: shift by 63 is outside the range 0..31 This is a bug isn't it, the ulong are supposed to be on 64 bits ? I guess i

Auto ref function : How is this possible ?

2015-04-11 Thread matovitch via Digitalmars-d-learn
Hi, I just learn about auto ref functions and tried this : import std.stdio; auto ref foo(int i, ref float f) { if (i < f) { return i; } else { return f; } } void main() { int i = 1; float f1 = 1.

Re: Auto ref function : How is this possible ?

2015-04-11 Thread matovitch via Digitalmars-d-learn
(you can remove the ref stuff)

Re: Auto ref function : How is this possible ?

2015-04-11 Thread matovitch via Digitalmars-d-learn
Ok this explain it : http://dlang.org/function.html#auto-functions. It should return a float.

Re: Auto ref function : How is this possible ?

2015-04-11 Thread matovitch via Digitalmars-d-learn
In fact I am now thinking it's great...I tried with string instead of float and got a clear error message. I should have read the spec more thoroughly.

Why is indexed foreach restricted to build in array ?

2015-04-11 Thread matovitch via Digitalmars-d-learn
Hello, The question is in the title. It should be possible for a finite random access ranges to perform an indexed foreach no ? I mean like : foreach(size_t i = 0, auto ref x; R) { /*...*/ } Why are other foreach statements overloadable but this one ? Thanks in advance.

Re: Why is indexed foreach restricted to build in array ?

2015-04-11 Thread matovitch via Digitalmars-d-learn
On Saturday, 11 April 2015 at 10:53:46 UTC, Jakob Ovrum wrote: On Saturday, 11 April 2015 at 10:50:17 UTC, matovitch wrote: Hello, The question is in the title. It should be possible for a finite random access ranges to perform an indexed foreach no ? I mean like : foreach(size_t i = 0, aut

Re: Why is indexed foreach restricted to build in array ?

2015-04-11 Thread matovitch via Digitalmars-d-learn
On Saturday, 11 April 2015 at 11:24:32 UTC, John Colvin wrote: On Saturday, 11 April 2015 at 11:03:28 UTC, matovitch wrote: On Saturday, 11 April 2015 at 10:53:46 UTC, Jakob Ovrum wrote: On Saturday, 11 April 2015 at 10:50:17 UTC, matovitch wrote: Hello, The question is in the title. It shoul

Re: Why is indexed foreach restricted to build in array ?

2015-04-11 Thread matovitch via Digitalmars-d-learn
On Saturday, 11 April 2015 at 14:01:07 UTC, John Colvin wrote: What OS are you on? Ubuntu 14.10.

Re: Auto ref function : How is this possible ?

2015-04-13 Thread matovitch via Digitalmars-d-learn
Thanks for the tip ! I was looking at something like this.