equivariant functions
Andrei Alexandrescu
SeeWebsiteForEmail at erdani.org
Sun Oct 12 14:45:03 PDT 2008
Denis Koroskin wrote:
> On Mon, 13 Oct 2008 01:27:42 +0400, Denis Koroskin <2korden at gmail.com>
> wrote:
>
>> On Mon, 13 Oct 2008 01:17:36 +0400, Andrei Alexandrescu
>> <SeeWebsiteForEmail at erdani.org> wrote:
>>
>>> Denis Koroskin wrote:
>>>> May I suggest one of my personal preference? Here it is:
>>>> sameconst(char)* strstr(sameconst(char)* str1, const(char)* str2)
>>>> {
>>>> return str2; // compile-time error. Can't cast const(char)* to
>>>> sameconst(char)* implicitly
>>>> }
>>>
>>> I prefer my solution because it's more general and addresses more
>>> issues than passing the qualifier out.
>>>
>>> Andrei
>>
>> Yes, my solution doesn't address clone() method return type, but I
>> think they aren't related.
>>
>> Besides, your solution can't implement the following example, so mine
>> is more flexible: :)
>>
>> sameconst(char)[] findPatternInS1OrS2(sameconst(char)[] s1,
>> sameconst(char)[] s2, string needle);
>
>
> And also this:
>
> class Storage
> {
> sameconst(Foo) foo() sameconst(this) // const, invariant or none
> {
> return _foo;
> }
>
> private Foo _foo;
> }
>
> Your version?
class Storage
{
typeof(this._foo) foo() const { return _foo; }
}
Please note that your version is technically identical to Walter's
suggestion:
T foo(return const T value);
meaning that whatever qualifier value had will be returned back. Your
solution is better visually at the obvious cost of adding a keyword, and
let me warn again that we can't afford to add one keyword for each
problem we solve. I believe it is more fertile to align compiler's view
of the code with programmer expectations.
Andrei
More information about the Digitalmars-d
mailing list