digitalmars.D - Why this doesn't compile?
- Uranuz (16/16) Oct 11 2014 When I want to pass generic String by const reference I get an
- Alexandre L. (3/21) Oct 11 2014 Try doSmth!String("bla");
- Adam D. Ruppe (10/14) Oct 11 2014 Strings are passed by reference automatically, so you wouldn't
- Shammah Chancellor (6/23) Oct 11 2014 That's not entirely true. They're passed by value unless something
- ketmar via Digitalmars-d (3/4) Oct 11 2014 yes. this is a bug in your code. and it's not alone.
- market (4/8) Oct 11 2014 You said you leave. Why dont you just go? Don't be a liar. Bye
- Shammah Chancellor (4/12) Oct 11 2014 I've withheld from responding to you for a good long while. But, what
- ketmar via Digitalmars-d (10/13) Oct 12 2014 On Sat, 11 Oct 2014 23:38:37 -0400
- market (3/24) Oct 12 2014 Shammah ignore this douche hes our resident troll.
When I want to pass generic String by const reference I get an error. As far as I understand const should accept both mutable and immutable data. And there I want to pass it by reference. Is it possible? Or is there any reason why it is not? Is it a bug? void doSmth(String)(ref const(String) str) {} void main() { doSmth("Foo"); } Compilation output: /d198/f399.d(5): Error: template f399.doSmth does not match any function template declaration. Candidates are: /d198/f399.d(1): f399.doSmth(String)(ref const(String) str) /d198/f399.d(5): Error: template f399.doSmth(String)(ref const(String) str) cannot deduce template function from argument types !()(string)
Oct 11 2014
On Saturday, 11 October 2014 at 18:01:41 UTC, Uranuz wrote:When I want to pass generic String by const reference I get an error. As far as I understand const should accept both mutable and immutable data. And there I want to pass it by reference. Is it possible? Or is there any reason why it is not? Is it a bug? void doSmth(String)(ref const(String) str) {} void main() { doSmth("Foo"); } Compilation output: /d198/f399.d(5): Error: template f399.doSmth does not match any function template declaration. Candidates are: /d198/f399.d(1): f399.doSmth(String)(ref const(String) str) /d198/f399.d(5): Error: template f399.doSmth(String)(ref const(String) str) cannot deduce template function from argument types !()(string)Try doSmth!String("bla"); Also, the type is string, not String ?
Oct 11 2014
On Saturday, 11 October 2014 at 18:01:41 UTC, Uranuz wrote:When I want to pass generic String by const reference I get an error.Strings are passed by reference automatically, so you wouldn't want to double reference them anyway.As far as I understand const should accept both mutable and immutable data. And there I want to pass it by reference.Yes, but since "foo" isn't an lvalue (variable), you can't pass it by ref in D, even if it is const. This differs from C++, but I don't recall the reason, I think there's just too many weird edge cases that D wanted to avoid. But in the case of strings, arrays, classes, pointers, and user defined structs that wrap these, you don't need to pass it as ref at all, just use plain const.
Oct 11 2014
On 2014-10-11 18:10:22 +0000, Adam D. Ruppe said:On Saturday, 11 October 2014 at 18:01:41 UTC, Uranuz wrote:That's not entirely true. They're passed by value unless something changed. By value in that the struct representation is passed. That is to say, the length and a pointer.When I want to pass generic String by const reference I get an error.Strings are passed by reference automatically, so you wouldn't want to double reference them anyway.That's because string literals are stored in the data segment and are immutable rvalues. You can't have a ref to an rvalue.As far as I understand const should accept both mutable and immutable data. And there I want to pass it by reference.Yes, but since "foo" isn't an lvalue (variable), you can't pass it by ref in D, even if it is const. This differs from C++, but I don't recall the reason, I think there's just too many weird edge cases that D wanted to avoid. But in the case of strings, arrays, classes, pointers, and user defined structs that wrap these, you don't need to pass it as ref at all, just use plain const.
Oct 11 2014
On Sat, 11 Oct 2014 18:01:39 +0000 Uranuz via Digitalmars-d <digitalmars-d puremagic.com> wrote:Is it a bug?yes. this is a bug in your code. and it's not alone.
Oct 11 2014
yaOn Saturday, 11 October 2014 at 18:11:07 UTC, ketmar via Digitalmars-d wrote:On Sat, 11 Oct 2014 18:01:39 +0000 Uranuz via Digitalmars-d <digitalmars-d puremagic.com> wrote:You said you leave. Why dont you just go? Don't be a liar. Bye ketmar!Is it a bug?yes. this is a bug in your code. and it's not alone.
Oct 11 2014
On 2014-10-11 18:10:52 +0000, ketmar via Digitalmars-d said:On Sat, 11 Oct 2014 18:01:39 +0000 Uranuz via Digitalmars-d <digitalmars-d puremagic.com> wrote:I've withheld from responding to you for a good long while. But, what is your deal Ketmar? You're constantly trolling on this newsgroup. It's not appreciated.Is it a bug?yes. this is a bug in your code. and it's not alone. <image>
Oct 11 2014
On Sat, 11 Oct 2014 23:38:37 -0400 Shammah Chancellor via Digitalmars-d <digitalmars-d puremagic.com> wrote:I've withheld from responding to you for a good long while. But, what is your deal Ketmar? You're constantly trolling on this newsgroup. It's not appreciated.you are wrong. i have strong reasons to write my posts in my way. but you aren't interested in that reasons, you are interested in flamewar. there was choice: either ask me for explanation (which is a right thing when you don't understand something) or call me "troll" (in a form of a fake question). you choose second. what answer do you expect now? i don't want to speak with person who uses presumtion of guilt, and i don't want to explain anything to such person. good day to you.
Oct 12 2014
On Sunday, 12 October 2014 at 08:59:16 UTC, ketmar via Digitalmars-d wrote:On Sat, 11 Oct 2014 23:38:37 -0400 Shammah Chancellor via Digitalmars-d <digitalmars-d puremagic.com> wrote:Shammah ignore this douche hes our resident troll.I've withheld from responding to you for a good long while. But, what is your deal Ketmar? You're constantly trolling on this newsgroup. It's not appreciated.you are wrong. i have strong reasons to write my posts in my way. but you aren't interested in that reasons, you are interested in flamewar. there was choice: either ask me for explanation (which is a right thing when you don't understand something) or call me "troll" (in a form of a fake question). you choose second. what answer do you expect now? i don't want to speak with person who uses presumtion of guilt, and i don't want to explain anything to such person. good day to you.
Oct 12 2014