Thread overview | ||||||
---|---|---|---|---|---|---|
|
April 07, 2010 String literal arguments | ||||
---|---|---|---|---|
| ||||
Hello. I'm trying to learn more of D templates, but I'm stuck with an issue I cannot resolve. Well, actually, I don't know if this is even allowed, and that's why I'm posting here. Suppose I have a function declared like this: --- import std.traits; void foo(T...)(T args) if( isSomeString!(T[0]) ) { /// Some random code. } --- and then I call it: --- auto first = 1; auto second = 2; foo( "Hello World", first, second ); --- You can notice that the first argument is a string literal. What I want to know is: If a function argument is declared as a string literal, it can be accessed at compile time? And if the answer is yes, how can I do it?. Currently, if within the function body I attempt this: --- // Silly example. Actually I have something a little more complex. static if( args[0].length > 0 ) { // ... } --- The compiler complains that the above code cannot be evaluated at C.T. Even if the argument is a string literal. The only way that I can make this work is if I rewrite the function to: --- void foo(string str, T...)(T args) if( isSomeString!(T[0]) ) { // This works well static if( str.length > 0 ) { // ... } } --- But then I would have to write the function calls like this: --- foo!("Hello World")(first, second); --- And honestly, I like more the former way. Not a big deal if you look objectively, but well, I cannot resist to bikeshed myself. I hope to make some sense. Cheers! |
April 07, 2010 Re: String literal arguments | ||||
---|---|---|---|---|
| ||||
Posted in reply to Yao G. | Yao G. <nospamyaoltzin@gmail.com> wrote: > Hello. Greetings. > foo( "Hello World", first, second ); > --- > > You can notice that the first argument is a string literal. What I want to know is: If a function argument is declared as a string literal, it can be accessed at compile time? And if the answer is yes, how can I do it?. Afraid not. What you posted later is the only way to do this. This has been discussed several times in the past, and some suggestions have popped up, including static function parameters: void foo( static string s ) { // s is a compile time constant } This has however not been implemented. -- Simen |
April 07, 2010 Re: String literal arguments | ||||
---|---|---|---|---|
| ||||
Posted in reply to Yao G. | You have to take a look at what the compiler does normally. It doesn't do magic. Generally a function is something that takes a run-time value with a simple protocol. So when an argument is inside a function, it's a variable, even if the function was called with a constant. Walter actually tried to implement what you ask for, the static arguments for functions (or better the enum arguments, in my opinion), but he has given up because it's not easy to implement. So currently you have to write: foo!("Hello World")(first, second); Or: foo!"Hello World"(first, second); This tells the compiler to partially compile foo according to the first argument as a complile time constant, and according to two the run time variables. Bye, bearophile |
April 07, 2010 Re: String literal arguments | ||||
---|---|---|---|---|
| ||||
Posted in reply to Simen kjaeraas | On Wed, 07 Apr 2010 03:05:34 -0400, Simen kjaeraas <simen.kjaras@gmail.com> wrote:
> Yao G. <nospamyaoltzin@gmail.com> wrote:
>
>> Hello.
>
> Greetings.
>
>> foo( "Hello World", first, second );
>> ---
>>
>> You can notice that the first argument is a string literal. What I want to know is: If a function argument is declared as a string literal, it can be accessed at compile time? And if the answer is yes, how can I do it?.
>
> Afraid not. What you posted later is the only way to do this. This has
> been discussed several times in the past, and some suggestions have
> popped up, including static function parameters:
>
> void foo( static string s ) {
> // s is a compile time constant
> }
>
> This has however not been implemented.
>
bearophile and Simen:
Thanks for your answers. Yes, I thought that this was not going to be possible. But again, is not a big deal for me. I'll continue to use the string-as-template version. I just wanted to explore the possibility, because I wanted this function to be consistent with my other code.
Cheers!
|
Copyright © 1999-2021 by the D Language Foundation