www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Warnings/What should I know?

reply "DDD" <dcb854d0bfb1 f98b7c56a69c.anonbox.net> writes:
I'm learning D. I'm curious about surprises I may get. I 
typically use C++, C# and javascript
Oct 17 2013
next sibling parent reply "monarch_dodra" <monarchdodra gmail.com> writes:
On Friday, 18 October 2013 at 06:13:38 UTC, DDD wrote:
 I'm learning D. I'm curious about surprises I may get. I 
 typically use C++, C# and javascript
Off the top of my head, I think the biggest one is that D doesn't offer "default constructors". Instead, it only has a default value initialization. EG: S s; S s = S.init; S s = S(); S s = S(5); The first 3 lines are more or less equivalent. They will all simply initialize s to S.init, which is a compile time known value. S(5) will actually call a constructor. This can bite you in the ass if you write something like: struct S { this(int i = 5) {} } S s = S(); //Does *not* call the constructor with the value 5. Also, if you are coming from C++, know that D bans internal pointers. This is actually a sweet deal, because it means the D (unlike C++), is free to move objects around without ever having to explicitly duplicate them. It simply "bitcopies" the object, and never destroys the original. If you thought C++'s "move" semantics where sweet, they are nothing compared to D's straight up move. Finally, D doesn't have "copy constructor". It has something a bit sweeter called "postblit". Basically, you first bitcopy the object you want to duplicate, and then, a function called "postblit" gets called, which does the work that is required (should any be required). What's nice about this is that the "target object" and "source object" never actually communicate. Finally, if you are coming from C++, then "slices" will be new. It is important to understand what these do and don't do: http://dlang.org/d-array-article.html If you make the wrong assumptions about them, they *will* backfire on you.
Oct 17 2013
next sibling parent reply "simendsjo" <simendsjo gmail.com> writes:
On Friday, 18 October 2013 at 06:56:49 UTC, monarch_dodra wrote:
(...)
 struct S
 {
     this(int i = 5)
     {}
 }
 S s = S(); //Does *not* call the constructor with the value 5.
I didn't know that. In that case, I think struct ctors with only optional parameters should be illegal - how is it possible to call it? Struct ctors without parameters is already illegal due to .init.
Oct 18 2013
parent Jonathan M Davis <jmdavisProg gmx.com> writes:
On Friday, October 18, 2013 09:02:49 simendsjo wrote:
 On Friday, 18 October 2013 at 06:56:49 UTC, monarch_dodra wrote:
 (...)
 
 struct S
 {
 
     this(int i = 5)
     {}
 
 }
 S s = S(); //Does *not* call the constructor with the value 5.
I didn't know that. In that case, I think struct ctors with only optional parameters should be illegal - how is it possible to call it? Struct ctors without parameters is already illegal due to .init.
I think that there's a bug report on that somewhere, but I'd have to go digging for it. The only way that S() is anything other than S.init is if you overloaded opCall and made it static, in which case, S() becomes a call to that. - Jonathan M Davis
Oct 18 2013
prev sibling next sibling parent "Maxim Fomin" <maxim maxim-fomin.ru> writes:
On Friday, 18 October 2013 at 06:56:49 UTC, monarch_dodra wrote:
 On Friday, 18 October 2013 at 06:13:38 UTC, DDD wrote:
 I'm learning D. I'm curious about surprises I may get. I 
 typically use C++, C# and javascript
Off the top of my head, I think the biggest one is that D doesn't offer "default constructors". Instead, it only has a default value initialization. EG: S s; S s = S.init; S s = S(); S s = S(5); The first 3 lines are more or less equivalent.
Not always actually.
 This can bite you in the ass if you write something like:

 struct S
 {
     this(int i = 5)
     {}
 }
 S s = S(); //Does *not* call the constructor with the value 5.
One can bite in the ass even more in case of struct S { disable this(); } and using S.init or void foo () { int i; struct S { int bar() { return i; } } and also using S.init
 Finally, D doesn't have "copy constructor". It has something a 
 bit sweeter called "postblit". Basically, you first bitcopy the 
 object you want to duplicate, and then, a function called 
 "postblit" gets called, which does the work that is required 
 (should any be required). What's nice about this is that the 
 "target object" and "source object" never actually communicate.
How much are there threads asking what is wrong with the language when there are const/immutable structs and postblit :) ?
Oct 18 2013
prev sibling parent reply "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
On Fri, Oct 18, 2013 at 08:56:45AM +0200, monarch_dodra wrote:
 On Friday, 18 October 2013 at 06:13:38 UTC, DDD wrote:
I'm learning D. I'm curious about surprises I may get. I typically
use C++, C# and javascript
Off the top of my head, I think the biggest one is that D doesn't offer "default constructors". Instead, it only has a default value initialization.
[...] I thought classes do have default ctors? T -- The day Microsoft makes something that doesn't suck is probably the day they start making vacuum cleaners... -- Slashdotter
Oct 18 2013
parent "Adam D. Ruppe" <destructionator gmail.com> writes:
On Friday, 18 October 2013 at 17:09:18 UTC, H. S. Teoh wrote:
 I thought classes do have default ctors?
yeah but you still have to new them. Class c; // c is null, unlike C++ Class c =new Class(); // cool
Oct 18 2013
prev sibling next sibling parent "simendsjo" <simendsjo gmail.com> writes:
On Friday, 18 October 2013 at 06:13:38 UTC, DDD wrote:
 I'm learning D. I'm curious about surprises I may get. I 
 typically use C++, C# and javascript
Some stuff from the top of my head. Remember that you're asking for gotchas and surprises rather than the nice features of D :) * D has built-in support for three different character types. UTF-8, 16 and 32. The default, char and string, is UTF-8. This means .length gives the number of bytes, not necessarily the number of symbols. * Private protection is private to the module, not to the type. This means a private class method can be called from anywhere in the module. * Array slices are great, but they require some understanding on how the underlying runtime handles them. See the array slice tutorial. * D has compile-time reflection rather than runtime. There are some features related to runtime reflection, but it's quite minimal. It's possible to get runtime reflection by using compile-time reflection though. * Templates in D is actually useful, so use them :) For example, in C#, generic type constraints isn't part of the signature, so you cannot overload generic functions. In D, this is not a problem. You can look at std.typecons to get a glimse of how powerful D is (warning: it still blows my mind, so it's not for the faint of hart, and you should probably delay this - most D code is actually very nice and readable and not at all this complex). * D doesn't have a preprocessor. You can solve much of the same stuff by using string mixins, template mixins and debug and version statements. Note that version statements is quite minimal and doesn't support stuff like && or || by design. * The GC isn't as fast as in other languages (yet?), so if you are experiencing performance problems in a tight loop, try disabling the GC during that loop. * Not a gotcha, but you should look into D specific features like transitive const/immutable, pure, nothrow, safe, DbC, unittest, -cov, final switch, scope guards, templates, foreach etc. These change the way you code and is idiomatic D, so it's nice to learn them early on.
Oct 18 2013
prev sibling parent "Jesse Phillips" <Jesse.K.Phillips+D gmail.com> writes:
On Friday, 18 October 2013 at 06:13:38 UTC, DDD wrote:
 I'm learning D. I'm curious about surprises I may get. I 
 typically use C++, C# and javascript
D doesn't have the library count other popular languages have. And there is a graveyard of projects/libraries which is more noticeable than more popular languages. The language implementation isn't flushed out/implemented 100%. You're probably after specifics, but that is mostly subjective. Pretty much any difference from a familiar language will be a surprise.
Oct 18 2013