Last active
December 17, 2015 11:08
-
-
Save vhenzl/5599505 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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