digitalmars.D.learn - string literals
- Jack Applegame (15/15) May 31 2013 What's the reason that the string literal is a dynamic array, not
- bearophile (10/12) May 31 2013 Originally it was a fixed sized array. But in most cases you want
- Jonathan M Davis (12/28) May 31 2013 Would you really want to end up with a copy of a string literal every ti...
- Jack Applegame (6/12) May 31 2013 If to pass reference to static array as function argument, pragma
- Kenji Hara (14/30) May 31 2013 With 2.063, this code works.
What's the reason that the string literal is a dynamic array, not a static? So sometimes it is not possible to get string length compile time: void foo(T: E[N], E, size_t N)(auto ref T data) { pragma(msg, "static"); pragma(msg, data.length); } void foo(T: E[], E)(auto ref T data) { pragma(msg, "dynamic"); pragma(msg, data.length); // Error: variable data // cannot be read at compile time } ... foo("test");
May 31 2013
Jack Applegame:What's the reason that the string literal is a dynamic array, not a static?Originally it was a fixed sized array. But in most cases you want a dynamic array. Rust language forces you to specify where to allocate the string literal with a symbol before the string, as ~"hello". In D they have chosen a simpler solution, defaulting to dynamic. This enhancement is meant to lessen the problem a little: http://d.puremagic.com/issues/show_bug.cgi?id=481 Bye, bearophile
May 31 2013
On Friday, May 31, 2013 16:20:44 Jack Applegame wrote:What's the reason that the string literal is a dynamic array, not a static?Would you really want to end up with a copy of a string literal every time you used it? The fact that they're immutable and can be passed around without ever being copied is a definite efficiency boost for handling string literals (and a lot of string handling involves string literals). Making them static arrays wouldn't buy us anything and would cost us a lot.So sometimes it is not possible to get string length compile time: void foo(T: E[N], E, size_t N)(auto ref T data) { pragma(msg, "static"); pragma(msg, data.length); } void foo(T: E[], E)(auto ref T data) { pragma(msg, "dynamic"); pragma(msg, data.length); // Error: variable data // cannot be read at compile time } ... foo("test");You can't get the length there because data is not known at compile time. The variable must be known at compile time for it to work with pragma. The fact that it's a string is irrelevant, and making it a static array woludn't help any. If data were a template argument, it would work, but it's a funciton argument, so it won't. - Jonathan M Davis
May 31 2013
On Friday, 31 May 2013 at 15:35:51 UTC, Jonathan M Davis wrote:The fact that it's a string is irrelevant, and making it a static array woludn't help any. If data were a template argument, it would work, but it's a funciton argument, so it won't.If to pass reference to static array as function argument, pragma will work, But you are right. I have to find another way. Perhaps passing string as template alias.
May 31 2013
On Friday, 31 May 2013 at 14:20:45 UTC, Jack Applegame wrote:What's the reason that the string literal is a dynamic array, not a static? So sometimes it is not possible to get string length compile time: void foo(T: E[N], E, size_t N)(auto ref T data) { pragma(msg, "static"); pragma(msg, data.length); } void foo(T: E[], E)(auto ref T data) { pragma(msg, "dynamic"); pragma(msg, data.length); // Error: variable data // cannot be read at compile time } ... foo("test");With 2.063, this code works. void foo(E, size_t N)(ref E[N] data) { pragma(msg, "static, E = ", E, ", N = ", N); } void foo(E)(E[] data) { pragma(msg, "dynamic, E = ", E); } void main() { foo("test"); // static auto s = "test"; foo(s); // dynamic } Kenji Hara
May 31 2013