D import idiom compilation time
Dgame
r.schuett.1987 at gmail.com
Sat Jan 5 22:48:30 UTC 2019
On Saturday, 5 January 2019 at 21:36:10 UTC, Neia Neutuladh wrote:
> On Sat, 05 Jan 2019 21:14:37 +0000, Dgame wrote:
>> I'm curious about that. Would that work or did I miss
>> something?
>
> In your version, I might write:
>
> from.std.stdio.thisFunctionDoesNotExist("Hallo");
>
> And the error I would get is:
>
> Error: struct FromImpl does not overload ()
True.. What about:
----
template isModuleImport(string import_)
{
enum isModuleImport = __traits(compiles, { mixin("import ",
import_, ";"); });
}
template isSymbolInModule(string module_, string symbol)
{
static if (isModuleImport!module_) {
enum import_ = module_ ~ ":" ~ symbol;
enum isSymbolInModule = __traits(compiles, {
mixin("import ", import_, ";"); });
} else {
enum isSymbolInModule = false;
}
}
template FailedSymbol(string symbol, string module_)
{
auto FailedSymbol(Args...)(auto ref Args args)
{
assert(0, "Symbol \"" ~ symbol ~ "\" not found in " ~
module_);
}
}
struct FromImpl(string module_)
{
template opDispatch(string symbol)
{
static if (isSymbolInModule!(module_, symbol)) {
mixin("import ", module_, "; alias opDispatch = ",
symbol, ";");
} else {
static if (module_.length == 0) {
enum opDispatch = FromImpl!(symbol)();
} else {
enum import_ = module_ ~ "." ~ symbol;
static if (isModuleImport!import_) {
enum opDispatch = FromImpl!(import_)();
} else {
alias opDispatch = FailedSymbol!(symbol,
module_);
}
}
}
}
}
enum from = FromImpl!null();
void main()
{
from.std.stdio.writeln("Hallo");
auto _ = from.std.datetime.stopwatch.AutoStart.yes;
from.std.stdio.thisFunctionDoesNotExist("Hallo");
from.std.stdio.thisFunctionDoesNotExist(42);
}
----
Output:
----
Hallo
core.exception.AssertError at onlineapp.d(20): Symbol
"thisFunctionDoesNotExist" not found in std.stdio
----------------
??:? _d_assert_msg [0x3c00dc54]
onlineapp.d:20 pure nothrow @nogc @safe void
onlineapp.FailedSymbol!("thisFunctionDoesNotExist",
"std.stdio").FailedSymbol!(immutable(char)[]).FailedSymbol(immutable(char)[]) [0x3c00ceac]
onlineapp.d:52 _Dmain [0x3c00c8c3]
----
More information about the Digitalmars-d
mailing list