Jump to page: 1 2
Thread overview
T... args!
Dec 08, 2021
Salih Dincer
Dec 08, 2021
Adam Ruppe
Dec 09, 2021
Salih Dincer
Apr 29, 2022
Salih Dincer
Apr 29, 2022
Tejas
Apr 29, 2022
Dennis
Apr 29, 2022
Salih Dincer
Apr 30, 2022
Tejas
Apr 30, 2022
Salih Dincer
December 08, 2021

Hi all,

Is this not a contradiction? : and 3.1415 aren't string:

void foo(string...)(string args) {
  foreach(ref str; args) {
    str.writeln('\t', typeof(str).stringof);
  }
}

foo("Pi", "number", ':', 3.1415); /*
Pi		string
number	string
:		char
3,1415	double
//*/
December 08, 2021

On Wednesday, 8 December 2021 at 23:43:48 UTC, Salih Dincer wrote:

>

Is this not a contradiction? : and 3.1415 aren't string:

void foo(string...)(string args) {

string... there is a user-defined identifier representing a mix of types.

string isn't special, yo can declare your own variables with that name. And that's what you did here.

I think you meant to say

void foo(string[] args...) {}

which is avariadtic number of only strings

December 09, 2021

On Wednesday, 8 December 2021 at 23:47:07 UTC, Adam Ruppe wrote:

>

On Wednesday, 8 December 2021 at 23:43:48 UTC, Salih Dincer wrote:

I think you meant to say

void foo(string[] args...) {}

Not exactly...

alias str = immutable(char)[];

void foo(str...)(str args) {
  foreach(ref a; args) {
    a.writeln('\t', typeof(a).stringof);
  }
  str s; // "Amazing! ---v";
  s.writeln(": ", typeof(s).stringof);
}
December 09, 2021

On Thursday, 9 December 2021 at 00:36:29 UTC, Salih Dincer wrote:

>

On Wednesday, 8 December 2021 at 23:47:07 UTC, Adam Ruppe wrote:

>

On Wednesday, 8 December 2021 at 23:43:48 UTC, Salih Dincer wrote:

I think you meant to say

void foo(string[] args...) {}

Not exactly...

alias str = immutable(char)[];

void foo(str...)(str args) {
  foreach(ref a; args) {
    a.writeln('\t', typeof(a).stringof);
  }
  str s; // "Amazing! ---v";
  s.writeln(": ", typeof(s).stringof);
}

Unlike value template parameters (which constitute of existing type + identifier), all other template parameter forms introduce a brand new identifier in the template scope that is completely unrelated to whatever other types you may have outside in your program (including the ones implicitly imported from object.d like string).

The str... in your foo function introduces a template sequence parameter which shadows the str alias you have above. The str s; line declares a variable of the str type sequence, so it's essentially a tuple (*). See:

See:

import std.stdio : writeln;

alias str = immutable(char)[];

void foo(str...)(str args) {
  foreach(ref a; args) {
    a.writeln('\t', typeof(a).stringof);
  }
  str s; // "Amazing! ---v";
  s.writeln(": ", typeof(s).stringof);
}

void main()
{
    foo(1, true, 3.5);
}
1	int
true	bool
3.5	double
0falsenan: (int, bool, double)

(*) Technically, you can attempt to explicitly instantiate foo with non type template arguments, but it will fail to compile, since:

  • The args function parameter demands str to be a type (or type sequence)
  • The s function local variable demands str to be a type (or type sequence)

If you can either remove args and s or filter the sequence to keep only the types:

import std.meta : Filter;
enum bool isType(alias x) = is(x);
alias TypesOnly(args...) = Filter!(isType, args);

void foo(str...)(TypesOnly!str args)
{
    static foreach(s; str)
        pragma (msg, s);
}

void main()
{
    static immutable int a = 42;
    foo!(int, double, string)(3, 4.5, "asd");
    pragma (msg, `----`);
    foo!(a, "asd", bool, foo, int[])(true, []);
}
int
double
string
----
42
asd
bool
foo(str...)(TypesOnly!str args)
int[]
December 09, 2021

On 12/8/21 7:36 PM, Salih Dincer wrote:

>

On Wednesday, 8 December 2021 at 23:47:07 UTC, Adam Ruppe wrote:

>

On Wednesday, 8 December 2021 at 23:43:48 UTC, Salih Dincer wrote:

I think you meant to say

void foo(string[] args...) {}

Not exactly...

alias str = immutable(char)[];

void foo(str...)(str args) {
   foreach(ref a; args) {
     a.writeln('\t', typeof(a).stringof);
   }
   str s; // "Amazing! ---v";
   s.writeln(": ", typeof(s).stringof);
}

So what people are saying here (but not directly) is that str inside your template is a different symbol than str outside the template.

This code is exactly the same as the code above, it might help understand what is happening:

alias foobar = immutable(char)[]; // this doesn't play into the template at all

void foo(str...)(str args) {
  foreach(ref a; args) {
    a.writeln('\t', typeof(a).stringof);
  }
  str s; // "Amazing! ---v";
  s.writeln(": ", typeof(s).stringof);
}

Just like the two i below refer to different things:

int i;
void foo(int i) {
   writeln(i); // this is the function parameter i, not the module-level i
}

You may want to post what you want to achieve with your code, instead of examples of what you tried, and it may allow us to make things clearer. You are likely using the wrong construct to achieve your goals.

-Steve

April 29, 2022

On Thursday, 9 December 2021 at 14:34:58 UTC, Steven Schveighoffer wrote:

>

You may want to post what you want to achieve with your code, instead of examples of what you tried, and it may allow us to make things clearer. You are likely using the wrong construct to achieve your goals.

-Steve

Hi, sorry to bring up an old topic again, but I'm not a zombie. It's true I'm a dinosaur. 😀

What I want to do is: What is supported in many programming languages ​​but not in modern D programming language: String Interpolation

I believe this will be done using the template. What is this D template called as the special syntax called?

SDB@79

April 29, 2022

On 4/28/22 10:48 PM, Salih Dincer wrote:

>

On Thursday, 9 December 2021 at 14:34:58 UTC, Steven Schveighoffer wrote:

>

You may want to post what you want to achieve with your code, instead of examples of what you tried, and it may allow us to make things clearer. You are likely using the wrong construct to achieve your goals.

Hi, sorry to bring up an old topic again, but I'm not a zombie. It's true I'm a dinosaur. 😀

What I want to do is: What is supported in many programming languages ​​ but not in modern D programming language: String Interpolation

I believe this will be done using the template. What is this D template called as the special syntax called?

There is no string interpolation in D. You can use a function such as std.conv.text to produce a string given interleaving strings and items. Or you can use std.format.format to make it happen.

It looks like you want to take any number of parameters as a template parameter comprised of a tuple of strings? That isn't supported. What you instead created was a tuple with a name that shadows the outer name. Note that string is NOT a keyword in D, it's just an alias (one that the compiler recognizes specially). So it's possible to redefine string as a symbol to mean something else.

-Steve

April 29, 2022

On Friday, 29 April 2022 at 12:57:15 UTC, Steven Schveighoffer wrote:

>

On 4/28/22 10:48 PM, Salih Dincer wrote:

>

[...]

There is no string interpolation in D. You can use a function such as std.conv.text to produce a string given interleaving strings and items. Or you can use std.format.format to make it happen.

It looks like you want to take any number of parameters as a template parameter comprised of a tuple of strings? That isn't supported. What you instead created was a tuple with a name that shadows the outer name. Note that string is NOT a keyword in D, it's just an alias (one that the compiler recognizes specially). So it's possible to redefine string as a symbol to mean something else.

-Steve

It's not a keyword yet it's recognised specially by the compiler... What?
I'm understanding the concept over here, but why didn't we make string a keyword anyways since the compiler recognises it specially?

April 29, 2022

On Friday, 29 April 2022 at 15:13:08 UTC, Tejas wrote:

>

It's not a keyword yet it's recognised specially by the compiler... What?

It's not really recognized by the compiler, there's a little bit of magic to print string in outputted D code (e.g. error messages) instead of immutable(char)[], but that's it.

April 29, 2022

On Friday, 29 April 2022 at 12:57:15 UTC, Steven Schveighoffer wrote:

>

There is no string interpolation in D. You can use a function such as std.conv.text to produce a string given interleaving strings and items. Or you can use std.format.format to make it happen.

I see, think it can be done with mixin:

template prn(alias args)
{
  string prn()
  {
    string result = "write(";
    foreach(s; args.split("|"))
    {
      result ~= format("%s,", s);
    }
    return result ~ ");";
  }
}

void main()
{
  int data = 456;
  char enter = '\n';

  mixin(prn!q{
    enter| 123 | " str " |  data |

    enter|    __TIME__   | enter }
  );

  mixin(
    prn!q{"This value of " |41| " is prime."}
  );
}

If there was some convenience on the compiler side, we could integrate it into D.

SDB@79

« First   ‹ Prev
1 2