digitalmars.D.learn - What's wrong with this code?
- Sean Eskapp (16/16) Jan 08 2011 I had some code that was segfaulting, so I rewrote the basic idea as a
- Michal Minich (11/17) Jan 08 2011 1) write if (left is null) instead if checking for null. Equality
- Steven Schveighoffer (9/14) Jan 10 2011 Actually, this is no longer true. For comparing two classes or
- =?iso-8859-2?B?VG9tZWsgU293afFza2k=?= (19/22) Jan 08 2011 This looks fishy:
- Sean Eskapp (1/1) Jan 08 2011 Tomek got it right. Fixed by copying the objects, rather than using poin...
- bearophile (51/53) Jan 08 2011 Two versions:
- Michal Minich (2/3) Jan 08 2011 Probably by mistake, both are the same.
- bearophile (28/29) Jan 08 2011 You are right, I am sorry :-)
- Dmitry Olshansky (6/7) Jan 11 2011 Or more precisely, in Phobos.
- bearophile (5/8) Jan 11 2011 Let's create ecosystem-wide name requirements for D code, then! :-) The ...
- spir (9/17) Jan 11 2011 Anyway, it's wrong there is no style guide for D in general:
I had some code that was segfaulting, so I rewrote the basic idea as a fibonacci function, and lo and behold, it still segfaults. Why, and how to fix? begin 644 main.d M.PT*"0T*"71H:7,H*0T*"7L-" D);&5F="`](')I9VAT(#T ;G5L;#L-" E] M=&AI<RYL969T(#T M871E*"D *R!R:6=H="Y%=F%L=6%T92 I.PT*"7T-"GT-" T*1FEB($)A<BAI M;G0 ;BD-"GL-" EI9BAN(#T ` end
Jan 08 2011
On Sat, 08 Jan 2011 20:34:39 +0000, Sean Eskapp wrote:if(left == null)1) write if (left is null) instead if checking for null. Equality operator is rewritten to a.opEquals(b), which you don't want if you checking for null.this() { left = right = null; }2) default constructor as you written it is not needed, fields are always initialized to default value of their type, which is in this case null.private const Fib* left, right;3) Classes in D are reference types. It should be private const Fib left, right; 4) second constructor - Classes in D are reference types, & operator is not needed (see point 3), parameter left and right are already references to class.
Jan 08 2011
On Sat, 08 Jan 2011 15:46:01 -0500, Michal Minich <michal.minich gmail.com> wrote:On Sat, 08 Jan 2011 20:34:39 +0000, Sean Eskapp wrote:Actually, this is no longer true. For comparing two classes or interfaces, a == b becomes .opEquals(a, b) which is defined in object.di and properly handles null references. However, if checking for null, a is null is going to be more correct and as efficient as possible (a == b should be inlined to the same thing, but only if you have inlining enabled). -Steveif(left == null)1) write if (left is null) instead if checking for null. Equality operator is rewritten to a.opEquals(b), which you don't want if you checking for null.
Jan 10 2011
Sean Eskapp napisa=B3(a):I had some code that was segfaulting, so I rewrote the basic idea as a=fibonacci function, and lo and behold, it still segfaults. Why, and ho=w =to fix?This looks fishy: class Fib { private const Fib* left, right; ... this(in Fib left, in Fib right) { this.left =3D &left; this.right =3D &right; } Are you sure you want a pointer to class? Classes have reference semanti= cs = in D (like Java). Structs are value types, though. Anyway, it looks like= = the ctor takes the address of the reference placed on the stack. -- = Tomek
Jan 08 2011
Tomek got it right. Fixed by copying the objects, rather than using pointers. Thanks!
Jan 08 2011
Sean Eskapp:I had some code that was segfaulting, so I rewrote the basic idea as a fibonacci function, and lo and behold, it still segfaults. Why, and how to fix?Two versions: struct Fib { private const Fib* left, right; this(in Fib* left=null, in Fib* right=null) { this.left = left; this.right = right; } const int evaluate() { if (left is null) return 1; else return left.evaluate() + right.evaluate(); } } Fib* bar(int n) { if (n == 0 || n == 1) return new Fib(); else return new Fib(bar(n - 1), bar(n - 2)); } void main() { auto x = bar(5); assert(x.evaluate() == 8); } -------------------- struct Fib { private const Fib* left, right; this(in Fib* left=null, in Fib* right=null) { this.left = left; this.right = right; } const int evaluate() { if (left is null) return 1; else return left.evaluate() + right.evaluate(); } } Fib* bar(int n) { if (n == 0 || n == 1) return new Fib(); else return new Fib(bar(n - 1), bar(n - 2)); } void main() { auto x = bar(5); assert(x.evaluate() == 8); } Bye, bearophile
Jan 08 2011
On Sat, 08 Jan 2011 16:39:34 -0500, bearophile wrote:Two versions:Probably by mistake, both are the same.
Jan 08 2011
Michal Minich:Probably by mistake, both are the same.You are right, I am sorry :-) In D the name of functions starts with lowercase. class Fib { private const Fib left, right; this(in Fib left=null, in Fib right=null) { this.left = left; this.right = right; } const int evaluate() { if (left is null) return 1; else return left.evaluate() + right.evaluate(); } } Fib bar(int n) { if (n == 0 || n == 1) return new Fib(); else return new Fib(bar(n - 1), bar(n - 2)); } void main() { auto x = bar(5); assert(x.evaluate() == 8); } Bye, bearophile
Jan 08 2011
On 09.01.2011 1:43, bearophile wrote:In D the name of functions starts with lowercase.Or more precisely, in Phobos. There is no such requirement in D, I may suggest you stop using such a general and assertive posts, so not to confuse anyone. -- Dmitry Olshansky
Jan 11 2011
Dmitry Olshansky:Or more precisely, in Phobos. There is no such requirement in D, I may suggest you stop using such a general and assertive posts, so not to confuse anyone.Let's create ecosystem-wide name requirements for D code, then! :-) The Phobos ones may be a good starting point. This topic is neglected, it needs a much wider discussion. Bye, bearophile
Jan 11 2011
On 01/11/2011 01:11 PM, bearophile wrote:Dmitry Olshansky:Anyway, it's wrong there is no style guide for D in general: http://www.digitalmars.com/d/2.0/dstyle.html The issue is: how can one expect programmers to follow guidelines the core itself language (esp builtin names) constantly breaks? Denis _________________ vita es estrany spir.wikidot.comOr more precisely, in Phobos. There is no such requirement in D, I may suggest you stop using such a general and assertive posts, so not to confuse anyone.Let's create ecosystem-wide name requirements for D code, then! :-) The Phobos ones may be a good starting point. This topic is neglected, it needs a much wider discussion. Bye, bearophile
Jan 11 2011