View mode: basic / threaded / horizontal-split · Log in · Help
January 30, 2012
Compile time filesystem access?
Is D capable of accessing the filesystem at compile time, for instance, to
load and parse an XML DOM, or some other structural metadata, which may be
used to generate the associative struct and its members?
I can think of many other uses for the technology too. It seems extremely
powerful, and I'm sure it's been discussed :)
January 30, 2012
Re: Compile time filesystem access?
On Monday, 30 January 2012 at 13:23:19 UTC, Manu wrote:
> Is D capable of accessing the filesystem at compile time, for 
> instance, to
> load and parse an XML DOM, or some other structural metadata, 
> which may be
> used to generate the associative struct and its members?
> I can think of many other uses for the technology too. It seems 
> extremely
> powerful, and I'm sure it's been discussed :)

That's what the import("filename") expression is for:
http://dlang.org/expression.html#ImportExpression

You'll need to approve paths for inclusion with the -J switch.
January 30, 2012
Re: Compile time filesystem access?
On 30 January 2012 15:30, Vladimir Panteleev <vladimir@thecybershadow.net>wrote:

> On Monday, 30 January 2012 at 13:23:19 UTC, Manu wrote:
>
>> Is D capable of accessing the filesystem at compile time, for instance, to
>> load and parse an XML DOM, or some other structural metadata, which may be
>> used to generate the associative struct and its members?
>> I can think of many other uses for the technology too. It seems extremely
>> powerful, and I'm sure it's been discussed :)
>>
>
> That's what the import("filename") expression is for:
> http://dlang.org/expression.**html#ImportExpression<http://dlang.org/expression.html#ImportExpression>
>
> You'll need to approve paths for inclusion with the -J switch.
>

Magic! :)
January 30, 2012
Re: Compile time filesystem access?
Be careful, though, the import expression (understandably) expects a 
compile-time string, which means that you can't directly use it from 
CTFE (e.g. to open additional files included by the IDL file you are 
parsing), but instead have to go back to compile-time land and then pass 
the contents back to CTFE in convoluted ways…

David



On 1/30/12 2:41 PM, Manu wrote:
> On 30 January 2012 15:30, Vladimir Panteleev
> <vladimir@thecybershadow.net <mailto:vladimir@thecybershadow.net>> wrote:
>
>     On Monday, 30 January 2012 at 13:23:19 UTC, Manu wrote:
>
>         Is D capable of accessing the filesystem at compile time, for
>         instance, to
>         load and parse an XML DOM, or some other structural metadata,
>         which may be
>         used to generate the associative struct and its members?
>         I can think of many other uses for the technology too. It seems
>         extremely
>         powerful, and I'm sure it's been discussed :)
>
>
>     That's what the import("filename") expression is for:
>     http://dlang.org/expression.__html#ImportExpression
>     <http://dlang.org/expression.html#ImportExpression>
>
>     You'll need to approve paths for inclusion with the -J switch.
>
>
> Magic! :)
January 30, 2012
Re: Compile time filesystem access?
On 30 January 2012 15:41, Manu <turkeyman@gmail.com> wrote:

> On 30 January 2012 15:30, Vladimir Panteleev <vladimir@thecybershadow.net>wrote:
>
>> On Monday, 30 January 2012 at 13:23:19 UTC, Manu wrote:
>>
>>> Is D capable of accessing the filesystem at compile time, for instance,
>>> to
>>> load and parse an XML DOM, or some other structural metadata, which may
>>> be
>>> used to generate the associative struct and its members?
>>> I can think of many other uses for the technology too. It seems extremely
>>> powerful, and I'm sure it's been discussed :)
>>>
>>
>> That's what the import("filename") expression is for:
>> http://dlang.org/expression.**html#ImportExpression<http://dlang.org/expression.html#ImportExpression>
>>
>> You'll need to approve paths for inclusion with the -J switch.
>>
>
> Magic! :)
>

Here's another one I'm endlessly wishing I had in C.
I want to know if a library is present, and automatically disable non-vital
features if it isn't.
It shits me to tears when I can't build something because a non-vital
dependant lib is not available for a given platform or just not wanted.
January 30, 2012
Re: Compile time filesystem access?
Hear hear! How I understand what you mean!
"I just wanna freakin' look at it, but build a legitimate magical
mystery toy with endless possibilities!"

On Mon, Jan 30, 2012 at 6:24 PM, Manu <turkeyman@gmail.com> wrote:
> On 30 January 2012 15:41, Manu <turkeyman@gmail.com> wrote:
>>
>> On 30 January 2012 15:30, Vladimir Panteleev <vladimir@thecybershadow.net>
>> wrote:
>>>
>>> On Monday, 30 January 2012 at 13:23:19 UTC, Manu wrote:
>>>>
>>>> Is D capable of accessing the filesystem at compile time, for instance,
>>>> to
>>>> load and parse an XML DOM, or some other structural metadata, which may
>>>> be
>>>> used to generate the associative struct and its members?
>>>> I can think of many other uses for the technology too. It seems
>>>> extremely
>>>> powerful, and I'm sure it's been discussed :)
>>>
>>>
>>> That's what the import("filename") expression is for:
>>> http://dlang.org/expression.html#ImportExpression
>>>
>>> You'll need to approve paths for inclusion with the -J switch.
>>
>>
>> Magic! :)
>
>
> Here's another one I'm endlessly wishing I had in C.
> I want to know if a library is present, and automatically disable non-vital
> features if it isn't.
> It shits me to tears when I can't build something because a non-vital
> dependant lib is not available for a given platform or just not wanted.



-- 
Bye,
Gor Gyolchanyan.
January 30, 2012
Re: Compile time filesystem access?
On Monday, 30 January 2012 at 14:24:32 UTC, Manu wrote:
> I want to know if a library is present, and automatically 
> disable non-vital features if it isn't.

I'd like it too... here's what I tried. It doesn't
work, though.

===
void libraryTest(string lib)() { mixin("import " ~ lib ~ ";"); }

template libraryExists(string lib) {
       enum libraryExists =  __traits(compiles, 
libraryTest!lib());
}

void main() {
       static if(libraryExists!"arsd.cgi") {
               import arsd.cgi;
               auto cgi = new Cgi();
               cgi.write("hello w/ cgi");
       } else {
               import std.stdio;
               writeln("hello w/ stdio");
       }
}
===


That's the idea, but I couldn't get it to actually work.
Imports are apparently done by the compiler before
anything else (which makes sense, really) and that breaks
the if compiles thing; it fails before it gets there. If
you move the libraryTest inline to the compiles thing, it
never uses the lib, so that's no good either.


Alas.
January 30, 2012
Re: Compile time filesystem access?
On 01/30/2012 07:18 AM, Adam D. Ruppe wrote:
> On Monday, 30 January 2012 at 14:24:32 UTC, Manu wrote:
>> I want to know if a library is present, and automatically disable
>> non-vital features if it isn't.
>
> I'd like it too... here's what I tried. It doesn't
> work, though.

I am afraid I miss the point but shouldn't module ctors help.. f.i.

module m;
import std.file;
static this()  // module ctor
{
	if ("/usr/share/include/my.lib".isDir)
		import my;
	else
		import other;
		
}

Not tested!
January 30, 2012
Re: Compile time filesystem access?
On 30 January 2012 18:11, bls <bizprac@orange.fr> wrote:

> On 01/30/2012 07:18 AM, Adam D. Ruppe wrote:
>
>> On Monday, 30 January 2012 at 14:24:32 UTC, Manu wrote:
>>
>>> I want to know if a library is present, and automatically disable
>>> non-vital features if it isn't.
>>>
>>
>> I'd like it too... here's what I tried. It doesn't
>> work, though.
>>
>
> I am afraid I miss the point but shouldn't module ctors help.. f.i.
>
> module m;
> import std.file;
> static this()  // module ctor
> {
>        if ("/usr/share/include/my.lib".**isDir)
>                import my;
>        else
>                import other;
>
> }
>
> Not tested!
>

That looks like a runtime test, and would still require both libs being
present to link. I'm talking about detection at compile time.

For example, I have a sound API here atm, the it supports mp3, vorbis,
flac, wma, etc, but not all of those libraries are available for all
platforms I build for.
What I need to do is know in advance what platforms have which libs
available, and then do a bunch of platform checks to decide which should or
shouldn't be present.
Further more, if a lib is *available* for a given platform, but I don't
want to use it, or don't want to install/build the lib, I need to edit the
code... If I could detect presence of the lib, I can just gracefully remove
the feature.
January 30, 2012
Module detection [Was: Compile time filesystem access?]
On 1/30/12 3:24 PM, Manu wrote:
> Here's another one I'm endlessly wishing I had in C.
> I want to know if a library is present, and automatically disable
> non-vital features if it isn't.
> It shits me to tears when I can't build something because a non-vital
> dependant lib is not available for a given platform or just not wanted.

This could be an option, if it worked: 
http://d.puremagic.com/issues/show_bug.cgi?id=7399

David
« First   ‹ Prev
1 2
Top | Discussion index | About this forum | D home