www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Objects(from classes) at Compile time? no gc

reply "Taylor Hillegeist" <taylorh140 gmail.com> writes:
The subject says it all really. i have this example:

import core.memory;

class fruit{
   int value=5;
   public int getvalue(){
     return value;
   }
}

int main(string[] args) {
     GC.disable;
     static fruit myfruit;
     return myfruit.getvalue();
}

Most of the smart people will see that i want the program to 
return 5 but I did something dumb and didn't put in the "new" 
statement?

So my question is in longer words "Can I create instances of 
objects at compile time?" and if not "why not, i could build 
something (roughly)equivalent out of structs and functions and 
have it at compile time?"
May 15 2014
next sibling parent "Tolga Cakiroglu" <tcak pcak.com> writes:
On Friday, 16 May 2014 at 04:59:46 UTC, Taylor Hillegeist wrote:
 The subject says it all really. i have this example:

 import core.memory;

 class fruit{
   int value=5;
   public int getvalue(){
     return value;
   }
 }

 int main(string[] args) {
     GC.disable;
     static fruit myfruit;
     return myfruit.getvalue();
 }

 Most of the smart people will see that i want the program to 
 return 5 but I did something dumb and didn't put in the "new" 
 statement?

 So my question is in longer words "Can I create instances of 
 objects at compile time?" and if not "why not, i could build 
 something (roughly)equivalent out of structs and functions and 
 have it at compile time?"
First of all, that "static" keyword is meaningless there. You can remove it. Secondly, the "getvalue" method is not defined as "static". So, it requires a object. It is not bound to class. Thirdly, when you define "fruit myfruit", the variable "myfruit" is still "null". It doesn't point anywhere in memory. Therefore, the variable "value" is no where in memory. And, thereby there is no spoon, I mean 5.
May 15 2014
prev sibling next sibling parent =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 05/15/2014 09:59 PM, Taylor Hillegeist wrote:
 The subject says it all really. i have this example:

 import core.memory;

 class fruit{
    int value=5;
    public int getvalue(){
      return value;
    }
 }

 int main(string[] args) {
      GC.disable;
      static fruit myfruit;
      return myfruit.getvalue();
 }

 Most of the smart people will see that i want the program to return 5
 but I did something dumb and didn't put in the "new" statement?

 So my question is in longer words "Can I create instances of objects at
 compile time?" and if not "why not, i could build something
 (roughly)equivalent out of structs and functions and have it at compile
 time?"
Here are two ways of achieving it. Although f0 is constructed by new, I don't think that new is executed at run time (because it would conflict with 'static const'). f1 is definitely not using the GC because it is placed on a storage that the module owns: class Fruit { int value; this (int value) { this.value = value; } public int getvalue() const { return value; } } static const f0 = new Fruit(42); ubyte[__traits(classInstanceSize, Fruit)] storage; static const Fruit f1; static this() { import std.conv; f1 = emplace!Fruit(storage[], 43); } void main() { assert(f0.getvalue() == 42); assert(f1.getvalue() == 43); } Ali
May 15 2014
prev sibling next sibling parent reply Jacob Carlborg <doob me.com> writes:
On 16/05/14 06:59, Taylor Hillegeist wrote:
 The subject says it all really. i have this example:

 import core.memory;

 class fruit{
    int value=5;
    public int getvalue(){
      return value;
    }
 }

 int main(string[] args) {
      GC.disable;
      static fruit myfruit;
      return myfruit.getvalue();
 }

 Most of the smart people will see that i want the program to return 5
 but I did something dumb and didn't put in the "new" statement?

 So my question is in longer words "Can I create instances of objects at
 compile time?" and if not "why not, i could build something
 (roughly)equivalent out of structs and functions and have it at compile
 time?"
If you create an immutable instance it's possible to create it at compile time: int main(string[] args) { GC.disable; immutable fruit myfruit = new immutable(fruit); pragma(msg, myfruit.getvalue); // will print 5 at compile time return myfruit.getvalue(); } Although, I don't know if it will allocate it during runtime as well. -- /Jacob Carlborg
May 15 2014
parent reply "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Fri, 16 May 2014 02:31:18 -0400, Jacob Carlborg <doob me.com> wrote:

 On 16/05/14 06:59, Taylor Hillegeist wrote:
 The subject says it all really. i have this example:

 import core.memory;

 class fruit{
    int value=5;
    public int getvalue(){
      return value;
    }
 }

 int main(string[] args) {
      GC.disable;
      static fruit myfruit;
      return myfruit.getvalue();
 }

 Most of the smart people will see that i want the program to return 5
 but I did something dumb and didn't put in the "new" statement?

 So my question is in longer words "Can I create instances of objects at
 compile time?" and if not "why not, i could build something
 (roughly)equivalent out of structs and functions and have it at compile
 time?"
If you create an immutable instance it's possible to create it at compile time: int main(string[] args) { GC.disable; immutable fruit myfruit = new immutable(fruit); pragma(msg, myfruit.getvalue); // will print 5 at compile time return myfruit.getvalue(); } Although, I don't know if it will allocate it during runtime as well.
It will not. -Steve
May 16 2014
parent reply "Taylor Hillegeist" <taylorh140 gmail.com> writes:
On Friday, 16 May 2014 at 14:13:28 UTC, Steven Schveighoffer 
wrote:
 On Fri, 16 May 2014 02:31:18 -0400, Jacob Carlborg 
 <doob me.com> wrote:

 On 16/05/14 06:59, Taylor Hillegeist wrote:
 The subject says it all really. i have this example:

 import core.memory;

 class fruit{
   int value=5;
   public int getvalue(){
     return value;
   }
 }

 int main(string[] args) {
     GC.disable;
     static fruit myfruit;
     return myfruit.getvalue();
 }

 Most of the smart people will see that i want the program to 
 return 5
 but I did something dumb and didn't put in the "new" 
 statement?

 So my question is in longer words "Can I create instances of 
 objects at
 compile time?" and if not "why not, i could build something
 (roughly)equivalent out of structs and functions and have it 
 at compile
 time?"
If you create an immutable instance it's possible to create it at compile time: int main(string[] args) { GC.disable; immutable fruit myfruit = new immutable(fruit); pragma(msg, myfruit.getvalue); // will print 5 at compile time return myfruit.getvalue(); } Although, I don't know if it will allocate it during runtime as well.
It will not. -Steve
Does this work in GDC?
May 16 2014
parent reply "Marc =?UTF-8?B?U2Now7x0eiI=?= <schuetzm gmx.net> writes:
On Friday, 16 May 2014 at 22:48:08 UTC, Taylor Hillegeist wrote:
 Although, I don't know if it will allocate it during  runtime 
 as well.
It will not. -Steve
Does this work in GDC?
Can't test in GDC, but it does work in LDC, and I see no reason why it shouldn't, because this functionality is implemented in the frontend, which DMD, GDC and LDC share.
May 17 2014
parent "Taylor Hillegeist" <taylorh140 gmail.com> writes:
On Saturday, 17 May 2014 at 13:21:29 UTC, Marc Schütz wrote:
 On Friday, 16 May 2014 at 22:48:08 UTC, Taylor Hillegeist wrote:
 Although, I don't know if it will allocate it during  
 runtime as well.
It will not. -Steve
Does this work in GDC?
Can't test in GDC, but it does work in LDC, and I see no reason why it shouldn't, because this functionality is implemented in the frontend, which DMD, GDC and LDC share.
I've been trying to get a baremetal GDC working for the ARM cortex-M, I guess it isn't quite ready yet. I'm looking forward to DConf 2014 to seeing, "Tiny, Ubiquitous Machines Powered by D" by Michael V. Franklin. To see how his work on the cortex-M is going. I mostly work on safety critical applications DO-170C,(Nothing with D) but this language looks like it might have a great deal of potential in this area. Not that C isn't good, but there are things that make the code look cleaner. I was looking at the Interactive D compiler and how it associated the assembly code with the lines of D code, I guess you can do that through the link file(-S in gcc). But D has code coverage information, unit test that you don't have to remove from the production code. there is a great deal of good stuff there. more and more tools for various operation seem to pop up every day. good stuff.
May 19 2014
prev sibling parent Rikki Cattermole <alphaglosined gmail.com> writes:
On 16/05/2014 4:59 p.m., Taylor Hillegeist wrote:
 The subject says it all really. i have this example:

 import core.memory;

 class fruit{
    int value=5;
    public int getvalue(){
      return value;
    }
 }

 int main(string[] args) {
      GC.disable;
      static fruit myfruit;
      return myfruit.getvalue();
 }

 Most of the smart people will see that i want the program to return 5
 but I did something dumb and didn't put in the "new" statement?

 So my question is in longer words "Can I create instances of objects at
 compile time?" and if not "why not, i could build something
 (roughly)equivalent out of structs and functions and have it at compile
 time?"
Rules about CTFE, I have: 1. It must be valid D code 2. Source code must be available during compilation 3. If its not passed to a function, its not available 4. You don't care about performance 5. Templates are your friend I prefer to add pure to any function I intend to be CTFE'd. So yes you can use new for classes at CTFE, just don't expect it to be collected and finalized.
May 15 2014