www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Template function alias that leaves out the last type parameter

reply torhu <torhu yahoo.com> writes:
How would I achieve something like this, do I have to turn the 
aliases into functions?

```d
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;
```
Sep 27 2022
next sibling parent torhu <torhu yahoo.com> writes:
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?

 ```d
 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: ```d 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); } ```
Sep 27 2022
prev sibling parent reply Adam D Ruppe <destructionator gmail.com> writes:
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"); } ---
Sep 27 2022
parent reply torhu <torhu yahoo.com> writes:
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!
Sep 27 2022
parent reply rassoc <rassoc posteo.de> writes:
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?
Sep 28 2022
parent reply torhu <torhu yahoo.com> writes:
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); } } ```
Sep 28 2022
parent rassoc <rassoc posteo.de> writes:
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, %)"); } ```
Sep 28 2022