I suggest you ...

Support "this" as a return type to make building fluid interfaces easier

Right now, having fluid interface methods on a base class requires using the "curiously recurring template pattern" (CRTP) and even then it only works properly for one level of derived classes. For example:

public abstract class AwesomeListBase
{
public AwesomeListBase WithNotifications() { .... }
}

public AwesomeList<T> : AwesomeListBase
{
public AwesomeList<T> WithDefaultItems(IEnumerable<T> items) { ... }
}

This is far less then ideal because now we can't do this:

var list = new AwesomeList<int>()
.WithNotifications()
.WithDefaultItems(new[] { 1, 2, 3 }); // ERROR: no method WithDefaultItems on AwesomeListBase

The CRTP solution "fixes" this problem for one level above the base class, at the expense of some mind numbing and rather smelly code:

public abstract class AwesomeListBase<TDerived> where TDerived : AwesomeListBase<TDerived>
{
public TDerived WithNotifications() { .... }
}

public AwesomeList<T> : AwesomeListBase<AwesomeList<T>>
{
public AwesomeList<T> WithDefaultItems(IEnumerable<T> items) { ... }
}

This pattern gets even more complicated and mind numbing when you want a non-generic base class so you can do things like Collection<AwesomeList>, and classes that derive from AwesomeList<T> will face the same problem as the first examples above.

Wouldn't it be much nicer to be able to do this:

public abstract class AwesomeListBase
{
public this WithNotifications() { .... }
}

public AwesomeList<T> : AwesomeListBase
{
public this WithDefaultItems(IEnumerable<T> items) { ... }
}

And have it work such that any subclasses will automatically get a method with the same signature and return type of the subclass itself? As a bonus, the compiler can automatically insert "return this" at the end of the method for you so that you can't do anything silly.

3 votes
Vote
Sign in
(thinking…)
Password icon
Signed in as (Sign out)
You have left! (?) (thinking…)
Mike Marynowski shared this idea  ·   ·  Flag idea as inappropriate…  ·  Admin →

0 comments

Sign in
(thinking…)
Password icon
Signed in as (Sign out)
Submitting...

Feedback and Knowledge Base