Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Request: Expression bodied member syntax for interface implemented by field #18876

Closed
mattbenic opened this issue Apr 21, 2017 · 1 comment

Comments

@mattbenic
Copy link

mattbenic commented Apr 21, 2017

Expression-bodied members were a great addition to C#6.

Expanding from that feature and syntax, being able to simplify the common pattern of a class implementing an interface by containing an instance of another class that implements that field would be very helpful. Similarly to with automatic properties, at compile time expand this to common properties and methods directly calling the corresponding properties and methods on the internal field.

Example:

public interface IAnimationPlayer
{
	bool IsPlayingAnimation { get; }

	void PlayAnimation(AnimationData animationData);
}

public class AnimationPlayer :
	IAnimationPlayer
{
	public bool IsPlayingAnimation { get; private set; }

	public void PlayAnimation(AnimationData animationData)
	{
		// Do work to play animation...

		IsPlayingAnimation = true;
	}
}

public class Actor :
	IAnimationPlayer,
	IOtherActorInterface
{
	// IAnimationPlayer interface implementation is 
	// delegated to private field
	public IAnimationPlayer => animationPlayer;

	public Actor(IAnimationPlayer animationPlayer)
	{
		this.animationPlayer = animationPlayer;
	}

	// Implement additional functionality...

	private readonly IAnimationPlayer animationPlayer;
}

With Actor compiling to:

public class Actor :
	IAnimationPlayer,
	IOtherActorInterface
{
	public bool IsPlayingAnimation 
	{
		get
		{
			return animationPlayer.IsPlayingAnimation;
		}
	}

	public void PlayAnimation(AnimationData animationData)
	{
		animationPlayer.PlayAnimation(animationData);
	}

	public Actor(IAnimationPlayer animationPlayer)
	{
		this.animationPlayer = animationPlayer;
	}

	// Implement additional functionality...

	private readonly IAnimationPlayer animationPlayer;
}
@gafter gafter closed this as completed Apr 21, 2017
@gafter
Copy link
Member

gafter commented Apr 21, 2017

Issue moved to dotnet/csharplang #477 via ZenHub

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants