www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Clunky syntax

reply "ixid" <nuaccount gmail.com> writes:
Why is the new syntax so clunky?

thing myThing = new thing;

When it could be:

new thing myThing;
May 07 2012
parent reply "bearophile" <bearophileHUGS lycos.com> writes:
ixid:

 Why is the new syntax so clunky?

 thing myThing = new thing;

 When it could be:

 new thing myThing;
Sometimes you want to write: class Foo {} class Bar : Foo {} void main() { Foo b = new Bar; } Otherwise there is 'auto' too: auto myThing = new thing; Bye, bearophile
May 07 2012
parent reply "ixid" <nuaccount gmail.com> writes:
 class Foo {}
 class Bar : Foo {}
 void main() {
    Foo b = new Bar;
 }
Thank you, could you explain what is happening in your example? Bar is inheriting from Foo, what are you getting when you create a parent of type sub-class compared to Bar b = new Bar; and Foo b = new Foo; ? Foo b = new Bar won't compile if you add members to Bar and access them.
May 07 2012
parent reply "Chris Cain" <clcain uncg.edu> writes:
On Monday, 7 May 2012 at 17:52:01 UTC, ixid wrote:
 Thank you, could you explain what is happening in your example? 
 Bar is inheriting from Foo, what are you getting when you 
 create a parent of type sub-class compared  to Bar b = new Bar; 
 and Foo b = new Foo; ? Foo b = new Bar won't compile if you add 
 members to Bar and access them.
Hello, Foo is the "interface" you'll have to bar. So, a bit of a bigger example: -=-=-=- import std.stdio; class Foo { void doStuff() {} } class Bar : Foo { void doStuff() { writeln("Hi"); } void doThings() { } } void main() { Foo f = new Bar; f.doStuff(); // prints "Hi" to the screen f.doThings(); // doesn't compile } -=-=-=- So, as you can see, if you have a Foo, you can't call "doThings" using it. However, if your Foo is actually a Bar underneath, then it'll use Bar's version of "doStuff". OOP isn't terribly hard, but I suggest reading up on it some to grasp the concepts (and especially so you can see the benefits). Here's a link that might help you get started on some of the fundamentals: http://en.wikipedia.org/wiki/Object-oriented_programming#Fundamental_features_and_concepts
May 07 2012
parent "Era Scarecrow" <rtcvb32 yahoo.com> writes:
On Monday, 7 May 2012 at 18:03:45 UTC, Chris Cain wrote:
 On Monday, 7 May 2012 at 17:52:01 UTC, ixid wrote:
 Thank you, could you explain what is happening in your  
 example? Bar is inheriting from Foo, what are you getting when 
  you create a parent of type sub-class compared  to Bar b = 
 new  Bar; and Foo b = new Foo; ? Foo b = new Bar won't compile 
 if  you add members to Bar and access them.
 Foo is the "interface" you'll have to bar.

 OOP isn't terribly hard, but I suggest reading up on it some to 
  grasp the concepts (and especially so you can see the 
 benefits).
D also takes the approach with classes and objects that is closer to java than C++: All methods are overridable for polymorphism by default. Using interfaces and safely up&down casting using the known interfaces is so much easier. I haven't done much with C++, but these types of things are more error and bug prone than D and java; Not to mention the STL gives me a total headache. I have a good mental idea of how to explain OOP and inheritance using a RPG-like game example, which would be less confusing than the usual animals and shapes (At least it is to me).
May 09 2012