digitalmars.D.learn - Make function alias
- Andrey (6/13) Aug 20 2018 Hello,
- Andrey (2/9) Aug 20 2018
- vit (8/17) Aug 20 2018 static void log(bool newline = true)(string text)
- Paul Backus (12/25) Aug 20 2018 Since newline is a compile-time parameter, you can use `static
- ag0aep6g (31/46) Aug 20 2018 `writeln` is a template, so you can't do `&writeln`. You'd have to
- Andrey (2/3) Aug 20 2018 Thanks everybody for your answers.
- Basile B. (16/29) Aug 20 2018 If it's about having a concise syntax then you can do:
Hello, I want to make an alias to function "std.stdio.writeln" and "std.stdio.write" and use it like:static void log(bool newline = true)(string text) { alias print(T...) = newline ? &writeln : &write; _file.print(); text.print(); }Unfortunately, it doesn't work... Also tried with "enum print ..." but also no success. How to do it correctly?
Aug 20 2018
On Monday, 20 August 2018 at 13:14:14 UTC, Andrey wrote: Mistake... this is:static void log(bool newline = true)(string text) { alias print(T...) = newline ? &writeln : &write; _file.print(text); text.print(); }
Aug 20 2018
On Monday, 20 August 2018 at 13:22:02 UTC, Andrey wrote:On Monday, 20 August 2018 at 13:14:14 UTC, Andrey wrote: Mistake... this is:static void log(bool newline = true)(string text) { static if(newline)alias print = writeln; else alias print = write; _file.print(text); text.print(); }static void log(bool newline = true)(string text) { alias print(T...) = newline ? &writeln : &write; _file.print(text); text.print(); }
Aug 20 2018
On Monday, 20 August 2018 at 13:14:14 UTC, Andrey wrote:Hello, I want to make an alias to function "std.stdio.writeln" and "std.stdio.write" and use it like:Since newline is a compile-time parameter, you can use `static if`: static if (newline) { alias print = writeln; } else { alias print = write; } Note that since this alias is local to the function, you cannot use it with UFCS, so you will have to write `print(text)` instead of `text.print()`. Full example: https://run.dlang.io/is/SrBJdkstatic void log(bool newline = true)(string text) { alias print(T...) = newline ? &writeln : &write; _file.print(); text.print(); }Unfortunately, it doesn't work... Also tried with "enum print ..." but also no success. How to do it correctly?
Aug 20 2018
On 08/20/2018 03:14 PM, Andrey wrote:Hello, I want to make an alias to function "std.stdio.writeln" and "std.stdio.write" and use it like:`writeln` is a template, so you can't do `&writeln`. You'd have to instantiate the template before you can get the function pointer: `&writeln!T`. Even then you can't make an alias of that. `&writeln!T` is a function pointer, which is a value. But aliases work on types and symbols, not values. If you manage to obtain aliases, you won't be able to use the ternary operator on them. Being an expression, `foo ? bar : baz` works on values. You can't use it with function aliases. You have to commit to either function aliases or function pointers (values). With aliases (no address-of operator, no ternary operator, `print` is not a template): ---- void log(bool newline = true)(string text) { static if (newline) alias print = writeln; else alias print = writeln; print(text); /* Can't use UFCS with a local `print`, so `text.print()` doesn't work. */ } ---- With function pointers (have to instantiate `writeln`, `write`, and `print): ---- void log(bool newline = true)(string text) { enum print(T ...) = newline ? &writeln!T : &write!T; print!string(text); /* No IFTI, because `print` isn't a function template. */ } ----static void log(bool newline = true)(string text) { alias print(T...) = newline ? &writeln : &write; _file.print(); text.print(); }Unfortunately, it doesn't work... Also tried with "enum print ..." but also no success. How to do it correctly?
Aug 20 2018
On Monday, 20 August 2018 at 13:35:07 UTC, ag0aep6g wrote:On 08/20/2018 03:14 PM, Andrey wrote:Thanks everybody for your answers.
Aug 20 2018
On Monday, 20 August 2018 at 13:14:14 UTC, Andrey wrote:Hello, I want to make an alias to function "std.stdio.writeln" and "std.stdio.write" and use it like:If it's about having a concise syntax then you can do: --- import std.stdio; void main() { log!false("meep ! "); log!true("meep meep !"); } static void log(bool newline = true)(string text) { alias print = (a) => newline ? writeln(a) : write(a); print(text); } --- although this is like static if {} () else {}static void log(bool newline = true)(string text) { alias print(T...) = newline ? &writeln : &write; _file.print(); text.print(); }Unfortunately, it doesn't work... Also tried with "enum print ..." but also no success. How to do it correctly?
Aug 20 2018