D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 16395 - auto return on override
Summary: auto return on override
Status: NEW
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P4 enhancement
Assignee: No Owner
URL:
Keywords: spec
Depends on:
Blocks:
 
Reported: 2016-08-16 09:19 UTC by Lodovico Giaretta
Modified: 2024-12-13 18:49 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Lodovico Giaretta 2016-08-16 09:19:02 UTC
Currently DMD does not allow returning auto when overriding a function.

Consider this case:

===========================================
interface Foo { /* some methods */ }
interface Bar { Foo getFoo(); }

class BarImpl: Bar
{
    override auto getFoo()
    {
        class FooImpl: Foo
        {
             // implementation of Foo ...
             // ... plus range methods
        }
    }
}
===========================================

Currently I have two options:
1) remove auto and return Foo instead; this way the user cannot use the range interface of FooImpl, because it receives a Foo
2) put FooImpl outside the function and return FooImpl; this way all functionality is correct, but it's not optimal as I have to put FooImpl outside the only function that uses it (which looks weird), plus FooImpl cannot implicitly access the getFoo context.

This is not a essential enhancement, I don't know if it is feasible to implement it, but it would make the language features more orthogonal, allowing the use of overriding with return type inference and voldemort types.
Comment 1 Seb 2016-08-16 09:48:23 UTC
> Currently I have two options:

3) use structs (they are usually used for ranges anyways). With alias this and mixins you can get a pretty decent inheritance, but of course I don't know your code ;-)
Comment 2 Lodovico Giaretta 2016-08-16 10:05:50 UTC
(In reply to greenify from comment #1)
> > Currently I have two options:
> 
> 3) use structs (they are usually used for ranges anyways). With alias this
> and mixins you can get a pretty decent inheritance, but of course I don't
> know your code ;-)

True in general, but not really an option for my code (I need a certain amount of runtime polymorphism).

Regardless of my example, which I solved with option 2, the question of this issue is "is this feasible to implement? If it is, why not have it? If it's not, fair enough."
Comment 3 Seb 2016-08-16 10:15:37 UTC
> True in general, but not really an option for my code (I need a certain amount of runtime polymorphism).

I thought so :/

> Regardless of my example, which I solved with option 2, the question of this issue is "is this feasible to implement? If it is, why not have it? If it's not, fair enough."

I do agree that it's a bug / missing feature. 
If you want to get" dirty" yourself, I can highly recommend Walter's video from Dconf16
Comment 4 dlangBugzillaToGithub 2024-12-13 18:49:29 UTC
THIS ISSUE HAS BEEN MOVED TO GITHUB

https://github.com/dlang/dmd/issues/19170

DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB