Skip to content

Instantly share code, notes, and snippets.

@amatiasq
Last active November 25, 2022 12:33
Embed
What would you like to do?
using System;
// Interface segregation
interface ISimulationReporter<TResult>
{
event Action<TResult> OnComplete;
}
interface ISimulationRunner<TInput>
{
void Run(TInput values);
}
interface ISimulation<TInput, TResult>
: ISimulationRunner<TInput>, ISimulationReporter<TResult> {}
interface IConfigurableSimulation<TInput, TResult, TBehaviour>
: ISimulation<TInput, TResult>
{
TBehaviour Behaviour { get; set; }
}
interface ISimulationBehaviour<TSim, TInput>
where TSim : ISimulationRunner<TInput>
{
TInput GetInput(TSim simulation);
}
// Implementation
class MySimulation : IConfigurableSimulation<bool, int, MyBehaviour>
{
public event Action<int> OnComplete;
public void Run(bool values) {}
public MyBehaviour Behaviour { get; set; }
}
class MyBehaviour : ISimulationBehaviour<MySimulation, bool>
{
public bool GetInput(MySimulation simulation) => true;
}
// Testing
public class Testing
{
public static void Main() {
MyBehaviour a = new MyBehaviour();
ISimulationBehaviour<ISimulation<bool, int>, bool> b;
// Cannot convert source type 'MyBehaviour' to target type
// 'ISimulationBehaviour<ISimulation<bool,int>,bool>'
//
// Why?
// MyBehaviour implements ISimulationBehaviour<MySimulation, bool>
// MySimulation implements IConfigurableSimulation<bool, int, MyBehaviour>
// which itself extends ISimulation<TInput, TResult>
b = a;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment