Skip to content

Instantly share code, notes, and snippets.

@michaeloyer
Last active November 21, 2020 01:40
Show Gist options
  • Save michaeloyer/9f87316eecafe5884b3702009e405753 to your computer and use it in GitHub Desktop.
Save michaeloyer/9f87316eecafe5884b3702009e405753 to your computer and use it in GitHub Desktop.
Hello World Program using Result/Pattern Matching in Typescript, F# and C#
interface IResult { }
class Accepted : IResult
{
public string text { get; set; }
}
class Rejected : IResult { }
string GetResultText(IResult result)
{
return result switch
{
Accepted r => r.text,
_ => "Reject"
};
}
void PrintResult(IResult result) =>
System.Console.WriteLine($"Hello, {GetResultText(result)}!");
PrintResult(new Accepted {text = "World"});
PrintResult(new Rejected());
type Result =
| Accepted of {| text: string |}
| Rejected
let GetResultText = function
| Accepted result -> result.text
| Rejected -> "Reject"
let PrintResult = GetResultText >> printfn "Hello, %s!"
Accepted {| text = "World" |} |> PrintResult
Rejected |> PrintResult
type Result = Accepted | Rejected
interface Accepted {
status: "Accepted"
text: string
}
interface Rejected {
status: "Rejected"
}
function GetResultText(result: Result) : string {
switch (result.status) {
case "Accepted": return result.text
case "Rejected": return "Reject"
}
}
function PrintResult(result: Result) {
console.log('Hello, ' + GetResultText(result) + '!')
}
PrintResult({ status: "Accepted", text: "World" })
PrintResult({ status: "Rejected" })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment