using System;
public interface IAnimal
{
void Poke();
}
public class Animals
{
private struct Cow : IAnimal
{
public void Poke() { Console.WriteLine("Mooo..."); }
}
private struct Dog : IAnimal
{
public void Poke() { Console.WriteLine("Bark! Bark!"); }
}
public interface IVisitor
{
void Visit<T>(T animal)
where T : IAnimal;
}
public static void VisitAll<T>(T visitor)
where T : IVisitor
{
visitor.Visit(default(Cow));
visitor.Visit(default(Dog));
}
}
public class Sample
{
private struct PokeVisitor : Animals.IVisitor
{
public void Visit<T>(T animal)
where T : IAnimal
{
animal.Poke();
}
}
public static void Main()
{
Animals.VisitAll(default(PokeVisitor));
}
}
Unfortunately Mono complains on generic visitor interface. Will try with Microsoft C# compiler tomorrow. In both cases there is something wrong with Mono implementation of naked type constraints.
public interface IAnimal
{
void Poke();
}
public class Animals
{
private struct Cow : IAnimal
{
public void Poke() { Console.WriteLine("Mooo..."); }
}
private struct Dog : IAnimal
{
public void Poke() { Console.WriteLine("Bark! Bark!"); }
}
public interface IVisitor
{
void Visit<T>(T animal)
where T : IAnimal;
}
public static void VisitAll<T>(T visitor)
where T : IVisitor
{
visitor.Visit(default(Cow));
visitor.Visit(default(Dog));
}
}
public class Sample
{
private struct PokeVisitor : Animals.IVisitor
{
public void Visit<T>(T animal)
where T : IAnimal
{
animal.Poke();
}
}
public static void Main()
{
Animals.VisitAll(default(PokeVisitor));
}
}
public interface IPolymorphicFunc<TProjection>
{
void Invoke<T>(T x) where T : TProjection;
}
public interface IAnimal
{
void Poke();
}
public struct PokeVisitor : IPolymorphicFunc<IAnimal>
{
public void Invoke<T>(T animal) where T : IAnimal
// produces: error CS0425: The constraints for type parameter `T' of method `PokeVisitor.Invoke<T>(T)' must match the constraints for type parameter `T' of interface method `IPolymorphicFunc<IAnimal>.Invoke<T>(T)'. Consider using an explicit interface implementation instead
{
animal.Poke();
}
void IPolymorphicFunc<IAnimal>.Invoke<T>(T animal)
// produces: error CS1061: Type `T' does not contain a definition for `Poke' and no extension method `Poke' of type `T' could be found (are you missing a using directive or an assembly reference?)
{
animal.Poke();
}
void IPolymorphicFunc<IAnimal>.Invoke<T>(T animal) where T : IAnimal
// produces: error CS0460: `PokeVisitor.IPolymorphicFunc<IAnimal>.Invoke<T>(T)': Cannot specify constraints for overrides and explicit interface implementation methods
{
animal.Poke();
}
}
{
void Invoke<T>(T x) where T : TProjection;
}
public interface IAnimal
{
void Poke();
}
public struct PokeVisitor : IPolymorphicFunc<IAnimal>
{
public void Invoke<T>(T animal) where T : IAnimal
// produces: error CS0425: The constraints for type parameter `T' of method `PokeVisitor.Invoke<T>(T)' must match the constraints for type parameter `T' of interface method `IPolymorphicFunc<IAnimal>.Invoke<T>(T)'. Consider using an explicit interface implementation instead
{
animal.Poke();
}
void IPolymorphicFunc<IAnimal>.Invoke<T>(T animal)
// produces: error CS1061: Type `T' does not contain a definition for `Poke' and no extension method `Poke' of type `T' could be found (are you missing a using directive or an assembly reference?)
{
animal.Poke();
}
void IPolymorphicFunc<IAnimal>.Invoke<T>(T animal) where T : IAnimal
// produces: error CS0460: `PokeVisitor.IPolymorphicFunc<IAnimal>.Invoke<T>(T)': Cannot specify constraints for overrides and explicit interface implementation methods
{
animal.Poke();
}
}
No comments:
Post a Comment