Skip to content

gfl94/Goose

Repository files navigation

Goose

An unconstrained duck typing library.

class Duck
{
    public void Quack() { }
}

interface IDuck
{
    void Quack();
}

var duck = new Duck();
var duckTyped = duck.As<IDuck>();
duckTyped.Quack();      //calls Duck.Quack()

Goose Typing

More than duck typing, the interface can have more methods than the class.

class Duck
{
    public void Quack() { }
}

interface INamedDuck
{
    string Name { get; }
    void Quack();
}

var duck = new Duck();
var duckTyped = duck.As<INamedDuck>();
duckTyped.Quack();          //calls Duck.Quack()
var name = duckTyped.Name;  //throws NotImplementedException

Goose Typing Methods

More than simple goose typing, you can make classes and interfaces compatible in method calls.

class Food
{
    public int Calories { get; set; }
}

interface IFood
{
    int Calories { get; }
}

class Person
{
    public void Eat(Food food) { }
}

interface IPerson
{
    void Eat(IFood food);
    void Walk();
}

var food = new Food();
var person = new Person();
var ifood = food.As<IFood>();
var iperson = person.As<IPerson>(GooseTypePair.Create<Food, IFood>());
iperson.Eat(ifood);

About

An unconstrained duck typing library

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages