Thread overview
What is "stringImportPaths"
Dec 06
mrphobby
Dec 08
mrphobby
December 06
Can anyone explain what "stringImportPaths" is? I have seen this being used in dub.json files and I think I kind of know what it does, but I haven't been able to find a clear explanation in any documentation of what it does. It does not look like anything I'm familiar with from other languages.

I understand it can be used for resources but I have seen it being used with both text files and binary files so I'm a bit confused. The documentation says I can import "whatever", but that feels a bit weird since importing is a construct used for importing symbols, right?
December 06
On 12/06/2017 11:05 AM, mrphobby wrote:

> importing is a construct used for importing symbols, right?

That's the import statement. -J compiler switch is about the import expression:

  https://dlang.org/spec/expression.html#import_expressions

Ali

December 06
On Wednesday, 6 December 2017 at 19:05:24 UTC, mrphobby wrote:
> Can anyone explain what "stringImportPaths" is? I have seen this being used in dub.json files and I think I kind of know what it does, but I haven't been able to find a clear explanation in any documentation of what it does. It does not look like anything I'm familiar with from other languages.
>
> I understand it can be used for resources but I have seen it being used with both text files and binary files so I'm a bit confused. The documentation says I can import "whatever", but that feels a bit weird since importing is a construct used for importing symbols, right?

stringImportPaths are to -J what importPaths are to -I. In D you can import a string directly into your program, similarly to #include in C and C++. Imagine it as kind of a mixin(read("filename")) (which you can't do). For security concerns, dmd only looks for "filename" in directories passed in with -J.


A silly example:

foo.d:
import std.stdio;
void main() {
        mixin(`auto values = [` ~ import("foo.txt") ~ `];`);
        writeln(values);
}


foo.txt:
1, 2, 3, 4, 5

$ dmd -J. foo.d
$ ./foo
[1, 2, 3, 4, 5]


Atila
December 06
On Wednesday, 6 December 2017 at 20:17:55 UTC, Ali Çehreli wrote:
> On 12/06/2017 11:05 AM, mrphobby wrote:
>
> > importing is a construct used for importing symbols, right?
>
> That's the import statement. -J compiler switch is about the import expression:
>
>   https://dlang.org/spec/expression.html#import_expressions
>
> Ali

I went looking for that and didn't find it! I searched for "string imports" but apparently that doesn't quite work.
December 07
On 2017-12-06 20:05, mrphobby wrote:
> Can anyone explain what "stringImportPaths" is? I have seen this being used in dub.json files and I think I kind of know what it does, but I haven't been able to find a clear explanation in any documentation of what it does. It does not look like anything I'm familiar with from other languages.
> 
> I understand it can be used for resources but I have seen it being used with both text files and binary files so I'm a bit confused. The documentation says I can import "whatever", but that feels a bit weird since importing is a construct used for importing symbols, right?

There are two kinds of language constructs that uses the "import" keyword. One is the "Import Declaration" [1] which is the most common one and is used to import other symbols. The other language construct is the "Import Expression" [2], which is used to read a file at compile time and put its content into a string literal in your source code.

Anything specified to the "stringImportPaths" build setting will be sent to the compiler with the -J flag.

[1] https://dlang.org/spec/module.html#ImportDeclaration
[2] https://dlang.org/spec/expression.html#import_expressions

-- 
/Jacob Carlborg
December 08
On Thursday, 7 December 2017 at 09:47:31 UTC, Jacob Carlborg wrote:
> On 2017-12-06 20:05, mrphobby wrote:
> There are two kinds of language constructs that uses the "import" keyword. One is the "Import Declaration" [1] which is the most common one and is used to import other symbols. The other language construct is the "Import Expression" [2], which is used to read a file at compile time and put its content into a string literal in your source code.
>
> Anything specified to the "stringImportPaths" build setting will be sent to the compiler with the -J flag.
>
> [1] https://dlang.org/spec/module.html#ImportDeclaration
> [2] https://dlang.org/spec/expression.html#import_expressions

Ok thanks! I couldn't find that in the docs so kudos for pointing me to it.

I still think using the word "import" is confusing. Would rather have it called "load" or something. But at least I understand it now :)
December 08
On 12/08/2017 12:10 PM, mrphobby wrote:

> I still think using the word "import" is confusing. Would rather have it
> called "load" or something. But at least I understand it now :)

We don't want any more keywords. :) (D's keywords are context-independent.)

An unfortunate example was the "body", which is on its way out of being a keyword. Instead, the "do" keyword is overloaded to take "body"s responsibility:

int foo(int i)
in {
    assert(i < 100);
} out(result) {
    assert(result > i);
} do {
    auto body = i + 7;    // YAY! :)
    return body;
}

void main() {
    foo(42);
}

Of course the same can be said about any other keyword but life is not fair. :)

Ali