www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Make function alias

reply Andrey <saasecondbox yandex.ru> writes:
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
next sibling parent reply Andrey <saasecondbox yandex.ru> writes:
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
parent vit <vit vit.vit> writes:
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)
{
    alias print(T...) = newline ? &writeln : &write;
 
    _file.print(text);
    text.print();
}
static void log(bool newline = true)(string text) { static if(newline)alias print = writeln; else alias print = write; _file.print(text); text.print(); }
Aug 20 2018
prev sibling next sibling parent Paul Backus <snarwin gmail.com> writes:
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:

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?
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/SrBJdk
Aug 20 2018
prev sibling next sibling parent reply ag0aep6g <anonymous example.com> writes:
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:
 
 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?
`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. */ } ----
Aug 20 2018
parent Andrey <saasecondbox yandex.ru> writes:
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
prev sibling parent Basile B. <b2.temp gmx.com> writes:
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:

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?
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 {}
Aug 20 2018