Bug 1301 – CTFE fails for ImportExpressions

Status
RESOLVED
Resolution
INVALID
Severity
major
Priority
P2
Component
dmd
Product
D
Version
D1 (retired)
Platform
x86
OS
Windows
Creation time
2007-06-30T09:25:00Z
Last change time
2014-02-16T15:22:55Z
Assigned to
bugzilla
Creator
samukha

Comments

Comment #0 by samukha — 2007-06-30T09:25:41Z
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")); }
Comment #1 by fvbommel — 2007-06-30T09:37:39Z
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")); } ---
Comment #2 by samukha — 2007-06-30T12:27:08Z
Oops. Thanks for pointing that out.
Comment #3 by ibisbasenji — 2007-06-30T13:05:43Z
[email protected] wrote: > http://d.puremagic.com/issues/show_bug.cgi?id=1301 > > > > > > ------- Comment #2 from [email protected] 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
Comment #4 by samukha — 2007-07-02T09:45:29Z
On Sat, 30 Jun 2007 13:00:44 -0500, Chris Nicholson-Sauls <[email protected]> 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.