Skip to content

Instantly share code, notes, and snippets.

@johnazariah
Created May 15, 2016 04:12
Show Gist options
  • Save johnazariah/f6f4761f4e542ff8f584560c50e96b31 to your computer and use it in GitHub Desktop.
Save johnazariah/f6f4761f4e542ff8f584560c50e96b31 to your computer and use it in GitHub Desktop.
F# vs C#
using System;
namespace FSM.BankAccount
{
public abstract class BankAccountState
{
public static readonly BankAccountState ZeroBalanceState = new ChoiceTypes.ZeroBalanceState();
public static readonly BankAccountState ActiveState = new ChoiceTypes.ActiveState();
public static readonly BankAccountState OverdrawnState = new ChoiceTypes.OverdrawnState();
public static readonly BankAccountState ClosedState = new ChoiceTypes.ClosedState();
public abstract T Match<T>(
Func<T> zeroBalanceStateFunc,
Func<T> activeStateFunc,
Func<T> overdrawnStateFunc,
Func<T> closedStateFunc);
private static class ChoiceTypes
{
// ReSharper disable MemberHidesStaticFromOuterClass
public class ZeroBalanceState : BankAccountState
{
public override T Match<T>(
Func<T> zeroBalanceStateFunc,
Func<T> activeStateFunc,
Func<T> overdrawnStateFunc,
Func<T> closedStateFunc)
{
return zeroBalanceStateFunc();
}
}
public class ActiveState : BankAccountState
{
public override T Match<T>(
Func<T> zeroBalanceStateFunc,
Func<T> activeStateFunc,
Func<T> overdrawnStateFunc,
Func<T> closedStateFunc)
{
return activeStateFunc();
}
}
public class OverdrawnState : BankAccountState
{
public override T Match<T>(
Func<T> zeroBalanceStateFunc,
Func<T> activeStateFunc,
Func<T> overdrawnStateFunc,
Func<T> closedStateFunc)
{
return overdrawnStateFunc();
}
}
public class ClosedState : BankAccountState
{
public override T Match<T>(
Func<T> zeroBalanceStateFunc,
Func<T> activeStateFunc,
Func<T> overdrawnStateFunc,
Func<T> closedStateFunc)
{
return closedStateFunc();
}
}
// ReSharper restore MemberHidesStaticFromOuterClass
}
}
}
type BankAccountState =
| ZeroBalanceState
| ActiveState
| OverdrawnState
| ClosedState
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment