Re: Make function alias

2018-08-20 Thread Basile B. via Digitalmars-d-learn

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 ?  : 

   _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 {}


Re: Make function alias

2018-08-20 Thread Andrey via Digitalmars-d-learn

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.


Re: Make function alias

2018-08-20 Thread ag0aep6g via Digitalmars-d-learn

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 ?  : 

   _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 ``. You'd have to 
instantiate the template before you can get the function pointer: 
`!T`.


Even then you can't make an alias of that. `!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 ? !T : !T;

print!string(text); /* No IFTI, because `print` isn't a function 
template. */

}



Re: Make function alias

2018-08-20 Thread Paul Backus via Digitalmars-d-learn

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 ?  : 

   _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


Re: Make function alias

2018-08-20 Thread vit via Digitalmars-d-learn

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 ?  : 

   _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();
}



Re: Make function alias

2018-08-20 Thread Andrey via Digitalmars-d-learn

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 ?  : 

   _file.print(text);
   text.print();
}