Thread overview
Overloading methods by constness?
Aug 22, 2007
Nathan Reed
Aug 22, 2007
Paul Collier
Aug 22, 2007
Craig Black
Aug 26, 2007
Witold Baryluk
Aug 26, 2007
Nathan Reed
August 22, 2007
Hello,

In C++, you can overload methods by the constness of the method itself:

	class Foo
	{
		void bar ();		// #1
		void bar () const;	// #2
	}
	...
	Foo f;		f.bar();	// calls #1
	const Foo g;	g.bar();	// calls #2

I am wondering if there is any way to get similiar functionality in D? I have tried the following:

	class Foo
	{
		void bar () { ... }
		const void bar () { ... }
	}

but get a compiler error that the "bar"s conflict.

Why would I want to do this, you might ask?  Well, I would like to use this to write a pair of opSlice methods.  With built-in arrays, you can do this:

	char[] str = "Hello, world".dup;
	char[] substr = str[0..5];
	
	const(char)[] str2 = "Hello, world";
	const(char)[] substr2 = str2[0..5];
	char[] substr3 = str2[0..5]; 	// fails

i.e., the slice returns either a char[] or a const(char)[] depending on the constness of the array.  However, I cannot see how to make my own classes do the same thing, unless I can overload opSlice by constness.

Thanks,
Nathan Reed
August 22, 2007
Nathan Reed wrote:
> but get a compiler error that the "bar"s conflict.
Unfortunately (AFAIK) the symbol mangler doesn't take const into account yet, so it's impossible to overload like that until mangling is changed. :( I suspect Walter is hesitant to completely break library compatibility from D 1.0 to D 2.0! That's the problem with const correctness... gotta do it all the way or not at all... ;)
August 22, 2007
Perhaps this should be posted as a bug?

"Nathan Reed" <nathaniel.reed@gmail.com> wrote in message news:faga07$hj9$1@digitalmars.com...
> Hello,
>
> In C++, you can overload methods by the constness of the method itself:
>
> class Foo
> {
> void bar (); // #1
> void bar () const; // #2
> }
> ...
> Foo f; f.bar(); // calls #1
> const Foo g; g.bar(); // calls #2
>
> I am wondering if there is any way to get similiar functionality in D? I have tried the following:
>
> class Foo
> {
> void bar () { ... }
> const void bar () { ... }
> }
>
> but get a compiler error that the "bar"s conflict.
>
> Why would I want to do this, you might ask?  Well, I would like to use this to write a pair of opSlice methods.  With built-in arrays, you can do this:
>
> char[] str = "Hello, world".dup;
> char[] substr = str[0..5];
>
> const(char)[] str2 = "Hello, world";
> const(char)[] substr2 = str2[0..5];
> char[] substr3 = str2[0..5]; // fails
>
> i.e., the slice returns either a char[] or a const(char)[] depending on the constness of the array.  However, I cannot see how to make my own classes do the same thing, unless I can overload opSlice by constness.
>
> Thanks,
> Nathan Reed


August 26, 2007
Dnia Wed, 22 Aug 2007 14:20:19 -0500
"Craig Black" <cblack@ara.com> napisaƂ/a:

> Perhaps this should be posted as a bug?
> 

This isn't bug.

Look also at the http://s3.amazonaws.com/dconf2007/WalterAndrei.pdf

page 38.


regards.

-- 
Witold Baryluk, aleph0
MAIL: baryluk@smp.if.uj.edu.pl
JID: movax@jabber.autocom.pl
August 26, 2007
Witold Baryluk wrote:
> 
> This isn't bug.
> 
> Look also at the http://s3.amazonaws.com/dconf2007/WalterAndrei.pdf
> 
> page 38.

Good, I'm glad to see this problem is being addressed.  I do have a question about the 'return' storage-class, though.  Will it only work for non-member functions, or will there be a syntax for writing a member function for which 'this' has the 'return' storage-class?  Although, with the new interchangeable function call syntax, this doesn't matter that much...

Thanks,
Nathan Reed