Thread overview
[Issue 1301] New: CTFE fails for ImportExpressions
Jun 30, 2007
d-bugmail
Jun 30, 2007
d-bugmail
Jun 30, 2007
d-bugmail
Jul 02, 2007
Max Samukha
June 30, 2007
http://d.puremagic.com/issues/show_bug.cgi?id=1301

           Summary: CTFE fails for ImportExpressions
           Product: D
           Version: 1.017
          Platform: PC
        OS/Version: Windows
            Status: NEW
          Severity: major
          Priority: P2
         Component: DMD
        AssignedTo: bugzilla@digitalmars.com
        ReportedBy: samukha@voliacable.com


I believe this should to work, as 'path' is known at compile time:

char[] foo(char[] path)
{
    char[] t = import(path); //Error: file name argument must be a string, not
(path)

    return "";
}

void main(char[][] args)
{
   mixin(foo("template"));
}


-- 

June 30, 2007
http://d.puremagic.com/issues/show_bug.cgi?id=1301


fvbommel@wxs.nl changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |INVALID




------- Comment #1 from fvbommel@wxs.nl  2007-06-30 09:37 -------
Functions to be used in CTFE must still be compilable as normal functions. Yours doesn't meet that criterion. Try something like:
---
char[] foo(char[] path)()
{
    char[] t = import(path); //Error: file name argument must be a string, not
(path)

    return "";
}

void main(char[][] args)
{
   mixin(foo!("template"));
}
---


-- 

June 30, 2007
http://d.puremagic.com/issues/show_bug.cgi?id=1301





------- Comment #2 from samukha@voliacable.com  2007-06-30 12:27 -------
Oops. Thanks for pointing that out.


-- 

June 30, 2007
d-bugmail@puremagic.com wrote:
> http://d.puremagic.com/issues/show_bug.cgi?id=1301
> 
> 
> 
> 
> 
> ------- Comment #2 from samukha@voliacable.com  2007-06-30 12:27 -------
> Oops. Thanks for pointing that out.
> 
> 

One thing I think should work is hiding the import() in a template, and sending that value off to the CTF.

template T_Import (char[] file) {
  const T_Import = import(file);
}

template T_Foo (char[] file) {
  const T_Foo = `foo("`~T_Import!(file)~`")` ;
}

char[] foo (char[] data) {
  // ...
}

void main (char[][] args) {
  mixin(T_Foo!("template"));
}

I've used something similar to preload image and font data in an SDL based program.  It does work -- and the extra T_Import template maintains one copy of a given file being embedded.  (Otherwise fonts at least would sometimes result in two or three copies.  Bad bad disc usage, that.)

-- Chris Nicholson-Sauls
July 02, 2007
On Sat, 30 Jun 2007 13:00:44 -0500, Chris Nicholson-Sauls <ibisbasenji@gmail.com> wrote:

>One thing I think should work is hiding the import() in a template, and sending that value off to the CTF.
>
>template T_Import (char[] file) {
>   const T_Import = import(file);
>}
>
>template T_Foo (char[] file) {
>   const T_Foo = `foo("`~T_Import!(file)~`")` ;
>}
>
>char[] foo (char[] data) {
>   // ...
>}
>
>void main (char[][] args) {
>   mixin(T_Foo!("template"));
>}
>
>I've used something similar to preload image and font data in an SDL based program.  It does work -- and the extra T_Import template maintains one copy of a given file being embedded.  (Otherwise fonts at least would sometimes result in two or three copies.  Bad bad disc usage, that.)
>
>-- Chris Nicholson-Sauls

Thanks for the idea.