Make function alias

Basile B. b2.temp at gmx.com
Mon Aug 20 16:38:21 UTC 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:
>
>>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 {}


More information about the Digitalmars-d-learn mailing list