Thread overview
Template function alias that leaves out the last type parameter
Sep 27, 2022
torhu
Sep 27, 2022
torhu
Sep 27, 2022
Adam D Ruppe
Sep 28, 2022
torhu
Sep 28, 2022
rassoc
Sep 28, 2022
torhu
Sep 28, 2022
rassoc
September 27, 2022

How would I achieve something like this, do I have to turn the aliases into functions?

void _messageBox(string title, int style, T...)(T args)
{
    string s = format(args);
    /* etc... */
}

alias _messageBox!(APPNAME, SWT.ICON_INFORMATION) info;
alias _messageBox!("Warning", SWT.ICON_WARNING) warning;
alias _messageBox!("Error", SWT.ICON_ERROR) error;
September 27, 2022

On Tuesday, 27 September 2022 at 22:39:52 UTC, torhu wrote:

>

How would I achieve something like this, do I have to turn the aliases into functions?

void _messageBox(string title, int style, T...)(T args)
{
    string s = format(args);
    /* etc... */
}

alias _messageBox!(APPNAME, SWT.ICON_INFORMATION) info;
alias _messageBox!("Warning", SWT.ICON_WARNING) warning;
alias _messageBox!("Error", SWT.ICON_ERROR) error;

I should mention that I am really looking for a Phobos 2 way of achieving what I currently do, which is this:

void _messageBox(string title, int style)(...)
{
	char[] msg;
	void f(dchar c) { encode(msg, c); }
	doFormat(&f, _arguments, _argptr);
	messageBox(cast(string)msg, title, style);
}
September 27, 2022

On Tuesday, 27 September 2022 at 22:39:52 UTC, torhu wrote:

>

How would I achieve something like this, do I have to turn the aliases into functions?

You can write it out long-form with two steps like this:


template _messageBox(string title, int style)
{
void _messageBox(T...)(T args)
{
import std.format;
string s = format("%s", args);
/* etc... */
}
}

alias _messageBox!("lol", 4) info;
alias _messageBox!("Warning", 4) warning;
alias _messageBox!("Error", 4) error;

void main() {
info(4, "ok");
}

September 28, 2022

On Tuesday, 27 September 2022 at 23:18:06 UTC, Adam D Ruppe wrote:

>

On Tuesday, 27 September 2022 at 22:39:52 UTC, torhu wrote:

>

How would I achieve something like this, do I have to turn the aliases into functions?

You can write it out long-form with two steps like this:

Thank you, works like a charm!

September 28, 2022
On 9/28/22 02:04, torhu via Digitalmars-d-learn wrote:
> Thank you, works like a charm!

It does? How are you formatting `info("foo", 'a', 42)` inside the template if I may ask?
September 28, 2022

On Wednesday, 28 September 2022 at 12:43:52 UTC, rassoc wrote:

>

On 9/28/22 02:04, torhu via Digitalmars-d-learn wrote:

>

Thank you, works like a charm!

It does? How are you formatting info("foo", 'a', 42) inside the template if I may ask?

It works like writefln, so that example would not compile. I forgot to make the format string explicit, I probably should have done that.

private template _messageBox(string title, int style)
{
	void _messageBox(T...)(T args)
	{
		messageBox(format(args), title, style);
	}
}
September 28, 2022
On 9/28/22 18:47, torhu via Digitalmars-d-learn wrote:
> It works like writefln, so that example would not compile. I forgot to make the format string explicit, I probably should have done that.
> 
> ```
> private template _messageBox(string title, int style)
> {
>      void _messageBox(T...)(T args)
>      {
>          messageBox(format(args), title, style);
>      }
> }
> ```

If you want to pass a message, why bother with varargs? Wouldn't it be simpler to just accept a formatted string?

```d
import std;

void messageBox(string title, int style)(string msg) {
    guiMessageBox(msg, title, style);
}

alias info = messageBox!("Info", 4);
alias warn = messageBox!("Warning", 4);
alias err  = messageBox!("Error", 4);

void main() {
    info("ok ok");
    warn(5.iota.format!"%(%s, %)");
}
```