View mode: basic / threaded / horizontal-split · Log in · Help
May 17, 2012
Would it be possible (and useful) to introduce declarations like `auto foo() if(isInputRange(auto));`
Is there anything preventing us from adding constraints on the 
auto function return value? I mean, such language extension seems 
to be quite useful.

For example, it would be no longer necessary to provide method 
bodies for functions with auto return values.

In many cases this would eliminate the need for introducing an 
interface.

interface returnsDuckTyped
{
    auto foo() if(isInputRange(auto));
}
May 17, 2012
Re: Would it be possible (and useful) to introduce declarations like `auto foo() if(isInputRange(auto));`
On Thursday, 17 May 2012 at 11:49:18 UTC, Roman D. Boiko wrote:
> Is there anything preventing us from adding constraints on the 
> auto function return value? I mean, such language extension 
> seems to be quite useful.
>
> For example, it would be no longer necessary to provide method 
> bodies for functions with auto return values.
>
> In many cases this would eliminate the need for introducing an 
> interface.

interface returnsDuckTyped
{
    auto foo() if(isInputRange!auto);
}

(fixed typo)
May 17, 2012
Re: Would it be possible (and useful) to introduce declarations like `auto foo() if(isInputRange(auto));
`
To: "digitalmars.D" <digitalmars-d@puremagic.com>
User-Agent: KMail/4.8.2 (Linux/3.3.1-1-ARCH; KDE/4.8.2; x86_64; ; )
X-Authenticated: #68274723
X-Flags: 0001
X-KMail-CryptoMessageFormat: 15
X-KMail-EncryptActionEnabled: false
X-KMail-Fcc: 15
X-KMail-SignatureActionEnabled: false
X-KMail-Transport: 1406625660
X-Mailer: GMX.com Web Mailer
x-registered: 0
X-GMX-UID: pi3HbzE+3zOlOEKpenAhypZ+IGRvb0CJ

On Thursday, May 17, 2012 13:49:16 Roman D. Boiko wrote:
> Is there anything preventing us from adding constraints on the
> auto function return value? I mean, such language extension seems
> to be quite useful.
> 
> For example, it would be no longer necessary to provide method
> bodies for functions with auto return values.
> 
> In many cases this would eliminate the need for introducing an
> interface.
> 
> interface returnsDuckTyped
> {
> auto foo() if(isInputRange(auto));
> }

It would still be necessary, because the compiler needs to know what the 
actual return type is. Knowing that the type implements popFront, front, and 
empty isn't enough. It needs to know the actual, physical layout of the type 
to generate the proper code. And when dealing with an interface, the return 
type must be covariant, and unless the types are both classes and one is 
derived from the other (directly or indirectly), they won't be covariant even 
if they have all of the same functions.

- Jonathan M Davis
May 17, 2012
Re: Would it be possible (and useful) to introduce declarations like `auto foo() if(isInputRange(auto));
On Thursday, 17 May 2012 at 21:09:10 UTC, Jonathan M Davis wrote:
> It would still be necessary, because the compiler needs to know 
> what the
> actual return type is. Knowing that the type implements 
> popFront, front, and
> empty isn't enough. It needs to know the actual, physical 
> layout of the type
> to generate the proper code. And when dealing with an 
> interface, the return
> type must be covariant, and unless the types are both classes 
> and one is
> derived from the other (directly or indirectly), they won't be 
> covariant even
> if they have all of the same functions.
>
> - Jonathan M Davis
I felt there is some fundamental problem, otherwise it would have 
been implemented already. But couldn't find any myself. Thanks!
May 18, 2012
Re: Would it be possible (and useful) to introduce declarations like `auto foo() if(isInputRange(auto)); `
On Thursday, May 17, 2012 13:49:16 Roman D. Boiko wrote:
> Is there anything preventing us from adding constraints on the
> auto function return value? I mean, such language extension seems
> to be quite useful.
> 
> For example, it would be no longer necessary to provide method
> bodies for functions with auto return values.
> 
> In many cases this would eliminate the need for introducing an
> interface.
> 
> interface returnsDuckTyped
> {
>      auto foo() if(isInputRange(auto));
> }

It would still be necessary, because the compiler needs to know what the 
actual return type is. Knowing that the type implements popFront, front, and 
empty isn't enough. It needs to know the actual, physical layout of the type 
to generate the proper code. And when dealing with an interface, the return 
type must be covariant, and unless the types are both classes and one is 
derived from the other (directly or indirectly), they won't be covariant even 
if they have all of the same functions.

- Jonathan M Davis
Top | Discussion index | About this forum | D home