Thread overview | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
April 21, 2017 Playing arround with mixin - alias? | ||||
---|---|---|---|---|
| ||||
Is it possible to define an alias for something like mixin(import("local_function_file.d")); to write only use_local_function; which will be translated to: mixin(import("local_function_file.d")); (this additionally needs the file local_function_file.d in source/ + -J./source as parameter for dmd aka. dflags "-J./source" in dub.sdl) Regards mt. |
April 21, 2017 Re: Playing arround with mixin - alias? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Martin Tschierschke | Martin Tschierschke wrote:
> Is it possible to define an alias for something like
>
> mixin(import("local_function_file.d"));
>
> to write only
>
> use_local_function;
>
> which will be translated to: mixin(import("local_function_file.d"));
>
> (this additionally needs the file local_function_file.d in source/ +
> -J./source as parameter for dmd aka. dflags "-J./source" in dub.sdl)
>
> Regards mt.
nope. the best you can get is `mixin use_local_function;`.
i'm assuming that you want your imported function behave like normally declared function here.
|
April 21, 2017 Re: Playing arround with mixin - alias? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Martin Tschierschke | On Friday, April 21, 2017 09:31:52 Martin Tschierschke via Digitalmars-d- learn wrote:
> Is it possible to define an alias for something like
>
> mixin(import("local_function_file.d"));
>
> to write only
>
> use_local_function;
>
> which will be translated to:
> mixin(import("local_function_file.d"));
>
> (this additionally needs the file local_function_file.d in
> source/ +
> -J./source as parameter for dmd aka. dflags "-J./source" in
> dub.sdl)
>
> Regards mt.
You can alias symbols. You can't alias statements, and mixins are always explicit. Now, you could have a function that returned what you wanted to mix in so that you just did something like
mixin(use_local_function());
and have use_local_function return whatever code it is you want to mix in, but you can't just do something like
use_local_function;
and have it work. That would be more like a macro system, and D doesn't have that. We have string mixins and template mixins, but those are both explicitly mixed in.
- Jonathan M Davis
|
April 21, 2017 Re: Playing arround with mixin - alias? | ||||
---|---|---|---|---|
| ||||
Posted in reply to ketmar | On Friday, 21 April 2017 at 09:42:33 UTC, ketmar wrote:
> Martin Tschierschke wrote:
>
>> Is it possible to define an alias for something like
>>
>> mixin(import("local_function_file.d"));
>>
>> to write only
>>
>> use_local_function;
>>
>> which will be translated to: mixin(import("local_function_file.d"));
>>
>> (this additionally needs the file local_function_file.d in source/ +
>> -J./source as parameter for dmd aka. dflags "-J./source" in dub.sdl)
>>
>> Regards mt.
>
> nope. the best you can get is `mixin use_local_function;`.
Would there be an advantage compared to a simple `import local_function_file;`?
|
April 21, 2017 Re: Playing arround with mixin - alias? | ||||
---|---|---|---|---|
| ||||
Posted in reply to biozic | biozic wrote:
> On Friday, 21 April 2017 at 09:42:33 UTC, ketmar wrote:
>> Martin Tschierschke wrote:
>>
>>> Is it possible to define an alias for something like
>>>
>>> mixin(import("local_function_file.d"));
>>>
>>> to write only
>>>
>>> use_local_function;
>>>
>>> which will be translated to: mixin(import("local_function_file.d"));
>>>
>>> (this additionally needs the file local_function_file.d in source/ +
>>> -J./source as parameter for dmd aka. dflags "-J./source" in dub.sdl)
>>>
>>> Regards mt.
>>
>> nope. the best you can get is `mixin use_local_function;`.
>
> Would there be an advantage compared to a simple `import local_function_file;`?
i doubt so.
|
April 21, 2017 Re: Playing arround with mixin - alias? | ||||
---|---|---|---|---|
| ||||
Posted in reply to ketmar | On Friday, 21 April 2017 at 10:31:46 UTC, ketmar wrote: > biozic wrote: > >> On Friday, 21 April 2017 at 09:42:33 UTC, ketmar wrote: >>> Martin Tschierschke wrote: >>> >>>> Is it possible to define an alias for something like >>>> >>>> mixin(import("local_function_file.d")); >>>> >>>> to write only >>>> >>>> use_local_function; >>>> >>>> which will be translated to: mixin(import("local_function_file.d")); >>>> >>>> (this additionally needs the file local_function_file.d in source/ + >>>> -J./source as parameter for dmd aka. dflags "-J./source" in dub.sdl) >>>> >>>> Regards mt. >>> >>> nope. the best you can get is `mixin use_local_function;`. >> >> Would there be an advantage compared to a simple `import local_function_file;`? > > i doubt so. So my "solution" on how to get a short cut for using: writeln(mixin(interp!"${name} you are app. ${age*365} days old")); // first place in source/exho.d the following code: auto mixinter(string x)(){return mixin(interp!x);} auto exho(string x)(){return mixin("writeln("~interp!x~")");} later compile this with -J./source source/app.d: import scriptlike; auto insert(string name)(){return import(name);} alias insert!"exho.d" use_exho; void main() { auto name = userInput!string("Please enter your name"); auto age = userInput!int("And your age"); //Now you can write in every scope you need it: mixin(use_exho); exho!"${name} you are app. ${age*365} days old"; // Which looks little nicer than: // writeln(mixin(interp!"${name} you are app. ${age*365} days old")); // or if you need the result in a var: mixin(use_exho); auto var = mixinter!"${name} you are app. ${age*365} days old"; writeln(var); } you may even name the alias just exho and write: alias insert!"exho.d" exho; mixin(exho); exho!"${name} you are app. ${age*365} days old"; (exho was derived from shell echo with mi_x_in) |
April 21, 2017 Re: Playing arround with mixin - alias? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Martin Tschierschke | On Friday, 21 April 2017 at 11:58:13 UTC, Martin Tschierschke wrote: > On Friday, 21 April 2017 at 10:31:46 UTC, ketmar wrote: >> biozic wrote: I thought way to complicated: Just define the string at top: enum exho="auto mixinter(string x)(){return mixin(interp!x);} auto exho(string x)(){return mixin(\"writeln(\"~interp!x~\")\");}"; Then use: mixin(exho); exho!"${name} you are app. ${age*365} days old"; In your scope! |
April 21, 2017 Re: Playing arround with mixin - alias? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Martin Tschierschke | Martin Tschierschke wrote: >> i doubt so. > So my "solution" on how to get a short cut for using: > > writeln(mixin(interp!"${name} you are app. ${age*365} days old")); .. NEVER. EVER. USE. THE. FOLOWING. IN. YOUR. CODE. NEVER!!! import std.stdio; mixin template usesexpand() { import std.format : format; string sexpand1(string s) () { char[] res; res.length = s.length+1024; auto rused = res.length; auto rpos = rused*0; // i really HAET `size_t`! void put(bool escape=true) (const(char)[] s...) { foreach (char ch; s) { static if (escape) { if (ch == '"' || ch == '\\') { if (rpos == rused) { rused += 1024; res.length = rused; } res[rpos++] = '\\'; } } if (rpos == rused) { rused += 1024; res.length = rused; } res[rpos++] = ch; } } char[] fmt; fmt.length = 256; auto fused = fmt.length; auto fpos = fused*0; // i really HAET `size_t`! void putfmt (const(char)[] s...) { foreach (char ch; s) { if (fpos == fused) { fused += 1024; fmt.length = fused; } fmt[fpos++] = ch; } } putfmt(`format("`); auto pos = rpos, epos = rpos; pos = 0; while (pos < s.length) { epos = pos; while (epos < s.length && s[epos] != '$') ++epos; putfmt("%s"); put!false(`, "`); if (epos+1 >= s.length || s[epos+1] != '{') { put(s[pos..epos+1]); put!false(`"`); pos = epos+1; continue; } put(s[pos..epos]); put!false(`"`); pos = epos+2; while (epos < s.length && s[epos] != '}') ++epos; if (epos > pos) { putfmt("%s"); put(`, `); put(s[pos..epos]); } pos = epos+1; } put(`)`); putfmt(`"`); return cast(string)fmt[0..fpos]~cast(string)res[0..rpos]; // it is safe to cast here } enum sexpandstr(string s) = sexpand1!s; enum sexpand(string s) = mixin(sexpand1!s); void echo(string s) () { writeln(mixin(sexpand1!s)); } } mixin usesexpand; immutable n = 40; enum nstr = sexpand!"n is ${n+2}"; void main () { mixin usesexpand; int a = 42/2; echo!"a is ${a*2}"; //writeln(mixin(sexpandstr!"a is ${a*2}")); writeln(nstr); } |
Copyright © 1999-2021 by the D Language Foundation