Author Topic: D language - class  (Read 15223 times)

Krice

  • (Banned)
  • Rogueliker
  • ***
  • Posts: 2316
  • Karma: +0/-2
    • View Profile
    • Email
D language - class
« on: November 09, 2014, 11:47:49 AM »
Any D language experts here? I want to ask about D's class everywhere I go. Is there a reason why you need to dynamic alloc an instance of a class? What's the problem with this:

MyClass a(1); //this is not possible in D

auto a = new MyClass(1); //this is

koiwai

  • Rogueliker
  • ***
  • Posts: 99
  • Karma: +0/-0
    • View Profile
Re: D language - class
« Reply #1 on: November 11, 2014, 08:08:30 PM »
I think, the first syntax is not allowed in D to make it's grammar context free. Although, I'm not familar with D well enough to tell exactly why it is not allowed..

Also, it seems that even when creating it with new, you may allocate it in the stack rather than in the heap:
 http://www.digitalmars.com/d/archives/digitalmars/D/D2.0_Stack_allocated_classes_55100.html

Krice

  • (Banned)
  • Rogueliker
  • ***
  • Posts: 2316
  • Karma: +0/-2
    • View Profile
    • Email
Re: D language - class
« Reply #2 on: November 12, 2014, 10:18:18 AM »
It's explained in the tutorial that D's classes are reference types, not value types. I kind of get it, but not completely...

http://ddili.org/ders/d.en/class.html

I guess they try to emphasize that new is creating a object. Then again, aren't built-in types like int objects as well? int a=1; is a object a of type int. But when you try to create a class with Class a(1); it doesn't work. I don't get it.
« Last Edit: November 12, 2014, 10:22:02 AM by Krice »

rust

  • Rogueliker
  • ***
  • Posts: 70
  • Karma: +0/-0
    • View Profile
    • Email
Re: D language - class
« Reply #3 on: November 12, 2014, 02:49:08 PM »
Actually, int a = 1; is the same syntax as MyClass a = new MyClass(1); MyClass just doesn't have a literal. In C# writing int a = new int(); is possible too, but obviously doesn't give any benefit.

Krice

  • (Banned)
  • Rogueliker
  • ***
  • Posts: 2316
  • Karma: +0/-2
    • View Profile
    • Email
Re: D language - class
« Reply #4 on: November 13, 2014, 11:24:47 AM »
So what C++ is doing when you create a class instance without new?

Cfyz

  • Rogueliker
  • ***
  • Posts: 194
  • Karma: +0/-0
    • View Profile
    • Email
Re: D language - class
« Reply #5 on: November 13, 2014, 05:45:44 PM »
Quote from: Krice
Is there a reason why you need to dynamic alloc an instance of a class?
I believe you are mixing high-level concepts with their language implementation. In C++ indeed there is little to none difference between what is called a struct and what is called a class. That does not required to be the case in other languages. In case of D, a reference-counted entity is called 'class object' and it makes little sense asking why you usually need dynamic allocation. Dynamic allocation is usually required for reference-counting and if such entity couldn't be made reference-counted it couldn't be called 'class object' in the first place.

Quote from: Krice
What's the problem with this:
MyClass a(1); //this is not possible in D
auto a = new MyClass(1); //this is
This is just how D syntax happens to be. You can't expect for any arbitrary syntax to be allowed in the language. And actually, having 'new' keyword in the statement does not necessary affect stack/heap allocation.

Krice

  • (Banned)
  • Rogueliker
  • ***
  • Posts: 2316
  • Karma: +0/-2
    • View Profile
    • Email
Re: D language - class
« Reply #6 on: November 13, 2014, 07:34:53 PM »
Yes, some guy thought it is because garbage collection. I guess it makes sense? Well I'm not too worried about it, but I don't know why they don't explain it better.

LindaJeanne

  • Newcomer
  • Posts: 22
  • Karma: +0/-0
    • View Profile
Re: D language - class
« Reply #7 on: November 14, 2014, 02:15:40 PM »
Yes, some guy thought it is because garbage collection. I guess it makes sense? Well I'm not too worried about it, but I don't know why they don't explain it better.

You mean explain why the syntax is different from Language X? Probably because Language X could be C, C++, C#, Java, Ada, Lisp, SmallTalk, Python, Fortran, Haskel, COBOL, WhiteSpace, BrainF*ck, BASIC.... no language is going to explain every syntactical difference between itself and every other language.

Or perhaps I misunderstood what you wanted them to explain?

Krice

  • (Banned)
  • Rogueliker
  • ***
  • Posts: 2316
  • Karma: +0/-2
    • View Profile
    • Email
Re: D language - class
« Reply #8 on: November 14, 2014, 05:06:56 PM »
Or perhaps I misunderstood what you wanted them to explain?

It's in the first message of this thread. Why you can create class only using new in D. I think it doesn't make sense unless it's for garbage collection. But who knows.

LindaJeanne

  • Newcomer
  • Posts: 22
  • Karma: +0/-0
    • View Profile
Re: D language - class
« Reply #9 on: November 16, 2014, 02:40:04 PM »
It's in the first message of this thread.

Yes, I saw that  :D

Why you can create class only using new in D. I think it doesn't make sense unless it's for garbage collection. But who knows.

Which reads to me like "why doesn't D use C++'s syntax", but perhaps I'm still misunderstanding.

LindaJeanne

  • Newcomer
  • Posts: 22
  • Karma: +0/-0
    • View Profile
Re: D language - class
« Reply #10 on: November 16, 2014, 11:50:14 PM »
While the answer to your syntactical question is simply "because that's the way D syntax works",  the answer to your implementation question about class memory allocation may have more of an answer:

I was completely unfamiliar with D (other than being aware that it exists), so I did some investigation and found the following:

http://forum.dlang.org/thread/juspoa$5g9$1@digitalmars.com#post-juspoa:245g9:241:40digitalmars.com
http://dlang.org/deprecate.html#scope%20for%20allocating%20classes%20on%20the%20stack
http://wiki.dlang.org/Memory_Management
https://issues.dlang.org/show_bug.cgi?id=5270

Short answer seems to be: D used to allow classes to be allocated to the stack, but deprecated this option because it caused memory corruption in some circumstances.
« Last Edit: November 16, 2014, 11:54:44 PM by LindaJeanne »

LindaJeanne

  • Newcomer
  • Posts: 22
  • Karma: +0/-0
    • View Profile
Re: D language - class
« Reply #11 on: November 16, 2014, 11:53:26 PM »
(Derp -- hit "quote" instead of "Edit")

Krice

  • (Banned)
  • Rogueliker
  • ***
  • Posts: 2316
  • Karma: +0/-2
    • View Profile
    • Email
Re: D language - class
« Reply #12 on: November 17, 2014, 10:55:05 AM »
D used to allow classes to be allocated to the stack, but deprecated this option because it caused memory corruption in some circumstances.

It's somewhat confusing, because you can allocate struct as local, but I guess struct is very much different than class in D, because inheritance is only allowed for class etc. And it looks like you can use 'scope' for local instances of class.

simendsjo

  • Newcomer
  • Posts: 2
  • Karma: +0/-0
    • View Profile
    • Email
Re: D language - class
« Reply #13 on: February 24, 2015, 08:24:24 PM »
Any D language experts here? I want to ask about D's class everywhere I go. Is there a reason why you need to dynamic alloc an instance of a class? What's the problem with this:

MyClass a(1); //this is not possible in D

auto a = new MyClass(1); //this is

Sorry for the necromancy, but as it's not too old, I'll try to
answer it.

In D, as opposed to C++, struct and class are two very different
things. Structs are *always* POD and have value semantics while
classes have reference semantics. This means that you have to
choose you design up-front and think about whether you want
a struct or class. If you need polymorphism, use class. If you
want pass by reference everywhere (probably) use a class, and so
on.

Trying to force a class to a stack allocation can be a pretty
unsafe operation. E.g. If you have the class allocated on the
stack it means it's fixed size. This doesn't play nicely together
with inheritance, and will probably bite you it the ass. It will
also make it easy to pass stack memory to other methods that will
happily assume a class is heap allocated and thus safe to store
a reference to or alias into.

There are ways around this if you know that allocating the class
on the heap using the GC will slow down your program. The (really)
old way was using `scope`, which will probably be deprecated.
Luckily D is powerful enough to provide such fatures in the
library: http://dlang.org/phobos/std_typecons.html#.scoped
This will create a struct and emplace your data in an array.
Again, you're using fixed size memory, so don't try to do any
classish stuff like assigning the variable to another subclass.

In short: structs are PODs located on the stack, classes are
polymorphic types located on the heap.