www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Why this doesn't compile?

reply "Uranuz" <neuranuz gmail.com> writes:
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
next sibling parent "Alexandre L." <alex.cs00 mail.com> writes:
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
prev sibling next sibling parent reply "Adam D. Ruppe" <destructionator gmail.com> writes:
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
parent Shammah Chancellor <email domain.com> writes:
On 2014-10-11 18:10:22 +0000, Adam D. Ruppe said:

 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.
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.
 
 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.
That's because string literals are stored in the data segment and are immutable rvalues. You can't have a ref to an rvalue.
Oct 11 2014
prev sibling parent reply ketmar via Digitalmars-d <digitalmars-d puremagic.com> writes:
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
next sibling parent "market" <dont spam.me> writes:
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:

 Is it a bug?
yes. this is a bug in your code. and it's not alone.
You said you leave. Why dont you just go? Don't be a liar. Bye ketmar!
Oct 11 2014
prev sibling parent reply Shammah Chancellor <email domain.com> writes:
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:
 
 Is it a bug?
yes. this is a bug in your code. and it's not alone. <image>
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.
Oct 11 2014
parent reply ketmar via Digitalmars-d <digitalmars-d puremagic.com> writes:
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
parent "market" <dont spam.me> writes:
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:

 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.
Shammah ignore this douche hes our resident troll.
Oct 12 2014