Skip to content

Instantly share code, notes, and snippets.

@vhenzl
Last active December 17, 2015 11:08
Show Gist options
  • Save vhenzl/5599505 to your computer and use it in GitHub Desktop.
Save vhenzl/5599505 to your computer and use it in GitHub Desktop.
using System;
class Program
{
static void Main()
{
var result1 = Authenticate("me", "password");
PrintAuthResult(result1);
var result2 = Authenticate("ja", "heslo");
PrintAuthResult(result2);
}
static AuthenticationResult Authenticate(string login, string password)
{
if (password == "heslo")
return new Authenticated(new User(login));
else
return new NotAuthenticated();
}
static void PrintAuthResult(AuthenticationResult result)
{
result.Match(
some: user => Console.WriteLine("yes: {0}", user.Login),
none: () => Console.WriteLine("no")
);
}
}
public class User
{
public User(string login)
{
Login = login;
}
public string Login { get; private set; }
}
public interface AuthenticationResult { }
public class NotAuthenticated : AuthenticationResult { }
public class Authenticated : AuthenticationResult
{
public Authenticated(User user)
{
User = user;
}
public User User { get; private set; }
}
public static class AuthenticationResultExtension
{
public static void Match(this AuthenticationResult result, Action<User> some, Action none)
{
var authenticated = result as Authenticated;
if (authenticated == null)
none();
else
some(authenticated.User);
}
}
type User(login: string) = class
member x.Login = login
end
type AuthenticationResult =
| NotAuthenticated
| Authenticated of User
let Authenticate (login: string) (password: string) =
if password = "heslo" then Authenticated(new User(login))
else NotAuthenticated
let PrintAuthResult (result: AuthenticationResult) =
match result with
| AuthenticationResult.Authenticated(result) -> printfn "yes: %s" result.Login
| AuthenticationResult.NotAuthenticated -> printfn "no"
let main() =
let result1 = Authenticate "me" "password"
PrintAuthResult result1
let result2 = Authenticate "ja" "heslo"
PrintAuthResult result2
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment