digitalmars.D.learn - Playing arround with mixin - alias?
- Martin Tschierschke (11/11) Apr 21 2017 Is it possible to define an alias for something like
- ketmar (4/12) Apr 21 2017 nope. the best you can get is `mixin use_local_function;`.
- biozic (3/22) Apr 21 2017 Would there be an advantage compared to a simple `import
- ketmar (2/23) Apr 21 2017 i doubt so.
- Martin Tschierschke (31/58) Apr 21 2017 So my "solution" on how to get a short cut for using:
- Martin Tschierschke (11/13) Apr 21 2017 I thought way to complicated:
- ketmar (76/79) Apr 21 2017 ..
- Jonathan M Davis via Digitalmars-d-learn (13/24) Apr 21 2017 You can alias symbols. You can't alias statements, and mixins are always
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.
Apr 21 2017
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.
Apr 21 2017
On Friday, 21 April 2017 at 09:42:33 UTC, ketmar wrote:Martin Tschierschke wrote:Would there be an advantage compared to a simple `import local_function_file;`?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;`.
Apr 21 2017
biozic wrote:On Friday, 21 April 2017 at 09:42:33 UTC, ketmar wrote:i doubt so.Martin Tschierschke wrote:Would there be an advantage compared to a simple `import local_function_file;`?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;`.
Apr 21 2017
On Friday, 21 April 2017 at 10:31:46 UTC, ketmar wrote:biozic wrote: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)On Friday, 21 April 2017 at 09:42:33 UTC, ketmar wrote:i doubt so.Martin Tschierschke wrote:Would there be an advantage compared to a simple `import local_function_file;`?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;`.
Apr 21 2017
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: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!biozic wrote:
Apr 21 2017
Martin Tschierschke wrote:.. 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); }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"));
Apr 21 2017
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
Apr 21 2017