www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Err compile time interpreter my function!

reply "Zaher Dirkey" <zaherdirkey yahoo.com> writes:
Hi, i need help to find strange error in my project

https://github.com/zaher/d-sard

if you have time to compile it and test.

src/sard/classes.d

class SardObjects(T: SardObject): SardObject
  ....
     void afterAdd(T object){
       debug{
         //writeln(this.classinfo.name ~ ".add:" ~ 
object.classinfo.name);
         writeln("This is not compiled line");
       }
     }

I added "writeln" to the function afterAdd in base class/template 
the compiler stop compiler

it work if i commented all adding objects in function created in 
class SrdEnvironment line about 1515
but there is object used the same add function in scanner.d/class 
SrdLexical: SardLexical line 817, it is work there, if i moved 
controls and operators from Envirement to Lexical it compiled, 
only in Envirement not work.

I can't recognize the error there, why compiler need to interpret 
it in compiler time?

-----
Error: fprintf cannot be interpreted at compile time, because it 
has no available source 
code	C:\D\dmd2\src\phobos\std\stdio.d	2537	
called from here: enforce(fprintf((*stdout._p).handle, 
"%.*s\x0a", cast(int)_param_0.length, 
cast(immutable(char)*)_param_0) >= 0, delegate const(char)[]() => 
null, 
"C:\\D\\dmd2\\windows\\bin\\..\\..\\src\\phobos\\std\\stdio.d", 
2537u)	C:\D\dmd2\src\phobos\std\stdio.d	2537	
called from here: writeln("This is not compiled 
line")	W:\home\d\lib\sard\src\sard\classes.d	94	
called from here: 
this.afterAdd(object)	W:\home\d\lib\sard\src\sard\classes.d	100	
called from here: 
super.add(c)	W:\home\d\lib\sard\src\sard\objects.d	1109	
called from here: __withSym.add("(", 
cast(SardControl)9)	W:\home\d\lib\sard\src\sard\objects.d	1515	
called from here: 
this.created()	W:\home\d\lib\sard\src\sard\classes.d	68	
called from here: 
super.this()	W:\home\d\lib\sard\src\sard\objects.d	1550	
----
Dec 26 2014
parent reply "Tobias Pankrath" <tobias pankrath.net> writes:
---
class C
{
      int a = foo(); // foo is called at compile time
}
---

Reduced test case:

---
class S
{
     int a = compileTime(1);
     int b = compileTime(2);
}

int compileTime(int i) { import std.stdio; writeln("ct"); return
i; }
---

This works as expected. To initialize your fields at runtime
(using a constructor) will solve your problem.
Dec 26 2014
parent "Zaher Dirkey" <zaherdirkey yahoo.com> writes:
Thank you.

I understand it as functiosn, but not classes, i fixed it by 
moving creating "env" from declaration of the "SardRun" class 
into a function.

https://github.com/zaher/d-sard/commit/9fbf3df40373d89152b00c02d9de352d64c12077#diff-8b265cbc4eda6c1f724e9f726b0da853

I feel i will fail in the same error in the future, i want to 
understand it


On Friday, 26 December 2014 at 08:55:36 UTC, Tobias Pankrath 
wrote:
 ---
 class C
 {
      int a = foo(); // foo is called at compile time
 }
 ---

 Reduced test case:

 ---
 class S
 {
     int a = compileTime(1);
     int b = compileTime(2);
 }

 int compileTime(int i) { import std.stdio; writeln("ct"); return
 i; }
 ---

 This works as expected. To initialize your fields at runtime
 (using a constructor) will solve your problem.
Dec 26 2014