Discussion:
Why this doesn't compile?
Uranuz via Digitalmars-d
2014-10-11 18:01:39 UTC
Permalink
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)
ketmar via Digitalmars-d
2014-10-11 18:10:52 UTC
Permalink
On Sat, 11 Oct 2014 18:01:39 +0000
Post by Uranuz via Digitalmars-d
Is it a bug?
yes. this is a bug in your code. and it's not alone.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 181 bytes
Desc: not available
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20141011/a88f8e7b/attachment.sig>
Alexandre L. via Digitalmars-d
2014-10-11 18:10:14 UTC
Permalink
Post by Uranuz via Digitalmars-d
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");
}
/d198/f399.d(5): Error: template f399.doSmth does not match any
/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 ?
Adam D. Ruppe via Digitalmars-d
2014-10-11 18:10:22 UTC
Permalink
Post by Uranuz via Digitalmars-d
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.
Post by Uranuz via Digitalmars-d
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.
Shammah Chancellor via Digitalmars-d
2014-10-12 03:41:11 UTC
Permalink
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.
market via Digitalmars-d
2014-10-11 19:54:09 UTC
Permalink
yaOn Saturday, 11 October 2014 at 18:11:07 UTC, ketmar via
Post by ketmar via Digitalmars-d
On Sat, 11 Oct 2014 18:01:39 +0000
Post by Uranuz via Digitalmars-d
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!
Shammah Chancellor via Digitalmars-d
2014-10-12 03:38:37 UTC
Permalink
Post by ketmar via Digitalmars-d
On Sat, 11 Oct 2014 18:01:39 +0000
Post by Uranuz via Digitalmars-d
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.
ketmar via Digitalmars-d
2014-10-12 08:58:35 UTC
Permalink
On Sat, 11 Oct 2014 23:38:37 -0400
Shammah Chancellor via Digitalmars-d <digitalmars-d at puremagic.com>
Post by Shammah Chancellor via Digitalmars-d
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.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 181 bytes
Desc: not available
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20141012/d7aa8453/attachment.sig>
market via Digitalmars-d
2014-10-12 15:58:01 UTC
Permalink
On Sunday, 12 October 2014 at 08:59:16 UTC, ketmar via
Post by ketmar via Digitalmars-d
On Sat, 11 Oct 2014 23:38:37 -0400
Shammah Chancellor via Digitalmars-d
<digitalmars-d at puremagic.com>
Post by Shammah Chancellor via Digitalmars-d
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.

Shammah Chancellor via Digitalmars-d
2014-10-12 03:38:37 UTC
Permalink
Post by ketmar via Digitalmars-d
On Sat, 11 Oct 2014 18:01:39 +0000
Post by Uranuz via Digitalmars-d
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.
Continue reading on narkive:
Loading...