Last active
May 24, 2019 16:03
-
-
Save baronfel/d9dab3f909914144c2578a73236e74dc to your computer and use it in GitHub Desktop.
sharplab example fo F# DUs in C#
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 PhoneOS = Android | Apple | |
type ThingIOwn = | |
| Car of make: string * model: string * year: string | |
| Phone of OS: PhoneOS * model: string | |
let thing = Phone(Android, "Pixel 2XL") |
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; | |
using System.Collections; | |
using System.Diagnostics; | |
using System.Reflection; | |
using System.Runtime.CompilerServices; | |
using System.Runtime.InteropServices; | |
[Serializable] | |
[StructLayout(LayoutKind.Auto, CharSet = CharSet.Auto)] | |
public sealed class PhoneOS : IEquatable<PhoneOS>, IStructuralEquatable, IComparable<PhoneOS>, IComparable, IStructuralComparable | |
{ | |
public static class Tags | |
{ | |
public const int Android = 0; | |
public const int Apple = 1; | |
} | |
[DebuggerBrowsable(DebuggerBrowsableState.Never)] | |
[CompilerGenerated] | |
internal readonly int _tag; | |
[DebuggerBrowsable(DebuggerBrowsableState.Never)] | |
[CompilerGenerated] | |
internal static readonly PhoneOS _unique_Android = new PhoneOS(0); | |
[DebuggerBrowsable(DebuggerBrowsableState.Never)] | |
[CompilerGenerated] | |
internal static readonly PhoneOS _unique_Apple = new PhoneOS(1); | |
[CompilerGenerated] | |
[DebuggerNonUserCode] | |
[DebuggerBrowsable(DebuggerBrowsableState.Never)] | |
public int Tag | |
{ | |
[CompilerGenerated] | |
[DebuggerNonUserCode] | |
get | |
{ | |
return _tag; | |
} | |
} | |
[CompilerGenerated] | |
[DebuggerNonUserCode] | |
[DebuggerBrowsable(DebuggerBrowsableState.Never)] | |
public static PhoneOS Android | |
{ | |
get | |
{ | |
return _unique_Android; | |
} | |
} | |
[CompilerGenerated] | |
[DebuggerNonUserCode] | |
[DebuggerBrowsable(DebuggerBrowsableState.Never)] | |
public bool IsAndroid | |
{ | |
[CompilerGenerated] | |
[DebuggerNonUserCode] | |
get | |
{ | |
return Tag == 0; | |
} | |
} | |
[CompilerGenerated] | |
[DebuggerNonUserCode] | |
[DebuggerBrowsable(DebuggerBrowsableState.Never)] | |
public static PhoneOS Apple | |
{ | |
get | |
{ | |
return _unique_Apple; | |
} | |
} | |
[CompilerGenerated] | |
[DebuggerNonUserCode] | |
[DebuggerBrowsable(DebuggerBrowsableState.Never)] | |
public bool IsApple | |
{ | |
[CompilerGenerated] | |
[DebuggerNonUserCode] | |
get | |
{ | |
return Tag == 1; | |
} | |
} | |
[CompilerGenerated] | |
[DebuggerNonUserCode] | |
internal PhoneOS(int _tag) | |
{ | |
this._tag = _tag; | |
} | |
[CompilerGenerated] | |
public int CompareTo(PhoneOS obj) | |
{ | |
if (this != null) | |
{ | |
if (obj != null) | |
{ | |
int tag = _tag; | |
int tag2 = obj._tag; | |
if (tag == tag2) | |
{ | |
return 0; | |
} | |
return tag - tag2; | |
} | |
return 1; | |
} | |
if (obj != null) | |
{ | |
return -1; | |
} | |
return 0; | |
} | |
[CompilerGenerated] | |
public int CompareTo(object obj) | |
{ | |
return CompareTo((PhoneOS)obj); | |
} | |
[CompilerGenerated] | |
public int CompareTo(object obj, IComparer comp) | |
{ | |
PhoneOS phoneOS = (PhoneOS)obj; | |
if (this != null) | |
{ | |
if ((PhoneOS)obj != null) | |
{ | |
int tag = _tag; | |
int tag2 = phoneOS._tag; | |
if (tag == tag2) | |
{ | |
return 0; | |
} | |
return tag - tag2; | |
} | |
return 1; | |
} | |
if ((PhoneOS)obj != null) | |
{ | |
return -1; | |
} | |
return 0; | |
} | |
[CompilerGenerated] | |
public int GetHashCode(IEqualityComparer comp) | |
{ | |
if (this != null) | |
{ | |
return _tag; | |
} | |
return 0; | |
} | |
[CompilerGenerated] | |
public bool Equals(object obj, IEqualityComparer comp) | |
{ | |
if (this != null) | |
{ | |
PhoneOS phoneOS = obj as PhoneOS; | |
if (phoneOS != null) | |
{ | |
int tag = _tag; | |
int tag2 = phoneOS._tag; | |
return tag == tag2; | |
} | |
return false; | |
} | |
return obj == null; | |
} | |
[CompilerGenerated] | |
public bool Equals(PhoneOS obj) | |
{ | |
if (this != null) | |
{ | |
if (obj != null) | |
{ | |
int tag = _tag; | |
int tag2 = obj._tag; | |
return tag == tag2; | |
} | |
return false; | |
} | |
return obj == null; | |
} | |
[CompilerGenerated] | |
public sealed override bool Equals(object obj) | |
{ | |
PhoneOS phoneOS = obj as PhoneOS; | |
if (phoneOS != null) | |
{ | |
return Equals(phoneOS); | |
} | |
return false; | |
} | |
} | |
[Serializable] | |
[StructLayout(LayoutKind.Auto, CharSet = CharSet.Auto)] | |
public abstract class ThingIOwn : IEquatable<ThingIOwn>, IStructuralEquatable, IComparable<ThingIOwn>, IComparable, IStructuralComparable | |
{ | |
public static class Tags | |
{ | |
public const int Car = 0; | |
public const int Phone = 1; | |
} | |
[Serializable] | |
public class Car : ThingIOwn | |
{ | |
[DebuggerBrowsable(DebuggerBrowsableState.Never)] | |
[CompilerGenerated] | |
internal readonly string _make; | |
[DebuggerBrowsable(DebuggerBrowsableState.Never)] | |
[CompilerGenerated] | |
internal readonly string _model; | |
[DebuggerBrowsable(DebuggerBrowsableState.Never)] | |
[CompilerGenerated] | |
internal readonly string _year; | |
[CompilerGenerated] | |
[DebuggerNonUserCode] | |
public string make | |
{ | |
[CompilerGenerated] | |
[DebuggerNonUserCode] | |
get | |
{ | |
return _make; | |
} | |
} | |
[CompilerGenerated] | |
public string model | |
{ | |
[CompilerGenerated] | |
get | |
{ | |
return _model; | |
} | |
} | |
[CompilerGenerated] | |
public string year | |
{ | |
[CompilerGenerated] | |
get | |
{ | |
return _year; | |
} | |
} | |
[CompilerGenerated] | |
internal Car(string _make, string _model, string _year) | |
{ | |
this._make = _make; | |
this._model = _model; | |
this._year = _year; | |
} | |
} | |
[Serializable] | |
public class Phone : ThingIOwn | |
{ | |
[DebuggerBrowsable(DebuggerBrowsableState.Never)] | |
[CompilerGenerated] | |
internal readonly PhoneOS oS; | |
[DebuggerBrowsable(DebuggerBrowsableState.Never)] | |
[CompilerGenerated] | |
internal readonly string _model; | |
[CompilerGenerated] | |
public PhoneOS OS | |
{ | |
[CompilerGenerated] | |
get | |
{ | |
return oS; | |
} | |
} | |
[CompilerGenerated] | |
public string model | |
{ | |
[CompilerGenerated] | |
get | |
{ | |
return _model; | |
} | |
} | |
[CompilerGenerated] | |
internal Phone(PhoneOS oS, string _model) | |
{ | |
this.oS = oS; | |
this._model = _model; | |
} | |
} | |
[CompilerGenerated] | |
[DebuggerNonUserCode] | |
public int Tag | |
{ | |
[CompilerGenerated] | |
[DebuggerNonUserCode] | |
get | |
{ | |
return (this is Phone) ? 1 : 0; | |
} | |
} | |
[CompilerGenerated] | |
[DebuggerNonUserCode] | |
public bool IsCar | |
{ | |
[CompilerGenerated] | |
[DebuggerNonUserCode] | |
get | |
{ | |
return this is Car; | |
} | |
} | |
[CompilerGenerated] | |
[DebuggerNonUserCode] | |
public bool IsPhone | |
{ | |
[CompilerGenerated] | |
[DebuggerNonUserCode] | |
get | |
{ | |
return this is Phone; | |
} | |
} | |
[CompilerGenerated] | |
[DebuggerNonUserCode] | |
internal ThingIOwn() | |
{ | |
} | |
public static ThingIOwn NewCar(string _make, string _model, string _year) | |
{ | |
return new Car(_make, _model, _year); | |
} | |
public static ThingIOwn NewPhone(PhoneOS oS, string _model) | |
{ | |
return new Phone(oS, _model); | |
} | |
[CompilerGenerated] | |
public int CompareTo(ThingIOwn obj) | |
{ | |
if (this != null) | |
{ | |
if (obj != null) | |
{ | |
int num = (this is Phone) ? 1 : 0; | |
int num2 = (obj is Phone) ? 1 : 0; | |
if (num == num2) | |
{ | |
int num3; | |
if (this is Car) | |
{ | |
Car car = (Car)this; | |
Car car2 = (Car)obj; | |
num3 = string.CompareOrdinal(car._make, car2._make); | |
if (num3 < 0) | |
{ | |
return num3; | |
} | |
if (num3 > 0) | |
{ | |
return num3; | |
} | |
int num4 = string.CompareOrdinal(car._model, car2._model); | |
if (num4 < 0) | |
{ | |
return num4; | |
} | |
if (num4 > 0) | |
{ | |
return num4; | |
} | |
return string.CompareOrdinal(car._year, car2._year); | |
} | |
Phone phone = (Phone)this; | |
Phone phone2 = (Phone)obj; | |
PhoneOS oS = phone.oS; | |
PhoneOS oS2 = phone2.oS; | |
num3 = oS.CompareTo(oS2); | |
if (num3 < 0) | |
{ | |
return num3; | |
} | |
if (num3 > 0) | |
{ | |
return num3; | |
} | |
return string.CompareOrdinal(phone._model, phone2._model); | |
} | |
return num - num2; | |
} | |
return 1; | |
} | |
if (obj != null) | |
{ | |
return -1; | |
} | |
return 0; | |
} | |
[CompilerGenerated] | |
public int CompareTo(object obj) | |
{ | |
return CompareTo((ThingIOwn)obj); | |
} | |
[CompilerGenerated] | |
public int CompareTo(object obj, IComparer comp) | |
{ | |
ThingIOwn thingIOwn = (ThingIOwn)obj; | |
if (this != null) | |
{ | |
if ((ThingIOwn)obj != null) | |
{ | |
int num = (this is Phone) ? 1 : 0; | |
ThingIOwn thingIOwn2 = thingIOwn; | |
int num2 = (thingIOwn2 is Phone) ? 1 : 0; | |
if (num == num2) | |
{ | |
int num3; | |
if (this is Car) | |
{ | |
Car car = (Car)this; | |
Car car2 = (Car)thingIOwn; | |
num3 = string.CompareOrdinal(car._make, car2._make); | |
if (num3 < 0) | |
{ | |
return num3; | |
} | |
if (num3 > 0) | |
{ | |
return num3; | |
} | |
int num4 = string.CompareOrdinal(car._model, car2._model); | |
if (num4 < 0) | |
{ | |
return num4; | |
} | |
if (num4 > 0) | |
{ | |
return num4; | |
} | |
return string.CompareOrdinal(car._year, car2._year); | |
} | |
Phone phone = (Phone)this; | |
Phone phone2 = (Phone)thingIOwn; | |
PhoneOS oS = phone.oS; | |
PhoneOS oS2 = phone2.oS; | |
num3 = oS.CompareTo(oS2, comp); | |
if (num3 < 0) | |
{ | |
return num3; | |
} | |
if (num3 > 0) | |
{ | |
return num3; | |
} | |
return string.CompareOrdinal(phone._model, phone2._model); | |
} | |
return num - num2; | |
} | |
return 1; | |
} | |
if ((ThingIOwn)obj != null) | |
{ | |
return -1; | |
} | |
return 0; | |
} | |
[CompilerGenerated] | |
public int GetHashCode(IEqualityComparer comp) | |
{ | |
if (this != null) | |
{ | |
int num = 0; | |
string year; | |
if (this is Car) | |
{ | |
Car car = (Car)this; | |
num = 0; | |
year = car._year; | |
num = -1640531527 + (((year != null) ? year.GetHashCode() : 0) + ((num << 6) + (num >> 2))); | |
year = car._model; | |
num = -1640531527 + (((year != null) ? year.GetHashCode() : 0) + ((num << 6) + (num >> 2))); | |
year = car._make; | |
return -1640531527 + (((year != null) ? year.GetHashCode() : 0) + ((num << 6) + (num >> 2))); | |
} | |
Phone phone = (Phone)this; | |
num = 1; | |
year = phone._model; | |
num = -1640531527 + (((year != null) ? year.GetHashCode() : 0) + ((num << 6) + (num >> 2))); | |
return -1640531527 + (phone.oS.GetHashCode(comp) + ((num << 6) + (num >> 2))); | |
} | |
return 0; | |
} | |
[CompilerGenerated] | |
public bool Equals(object obj, IEqualityComparer comp) | |
{ | |
if (this != null) | |
{ | |
ThingIOwn thingIOwn = obj as ThingIOwn; | |
if (thingIOwn != null) | |
{ | |
int num = (this is Phone) ? 1 : 0; | |
ThingIOwn thingIOwn2 = thingIOwn; | |
int num2 = (thingIOwn2 is Phone) ? 1 : 0; | |
if (num == num2) | |
{ | |
if (this is Car) | |
{ | |
Car car = (Car)this; | |
Car car2 = (Car)thingIOwn; | |
if (string.Equals(car._make, car2._make)) | |
{ | |
if (string.Equals(car._model, car2._model)) | |
{ | |
return string.Equals(car._year, car2._year); | |
} | |
return false; | |
} | |
return false; | |
} | |
Phone phone = (Phone)this; | |
Phone phone2 = (Phone)thingIOwn; | |
PhoneOS oS = phone.oS; | |
PhoneOS oS2 = phone2.oS; | |
if (oS.Equals(oS2, comp)) | |
{ | |
return string.Equals(phone._model, phone2._model); | |
} | |
return false; | |
} | |
return false; | |
} | |
return false; | |
} | |
return obj == null; | |
} | |
[CompilerGenerated] | |
public bool Equals(ThingIOwn obj) | |
{ | |
if (this != null) | |
{ | |
if (obj != null) | |
{ | |
int num = (this is Phone) ? 1 : 0; | |
int num2 = (obj is Phone) ? 1 : 0; | |
if (num == num2) | |
{ | |
if (this is Car) | |
{ | |
Car car = (Car)this; | |
Car car2 = (Car)obj; | |
if (string.Equals(car._make, car2._make)) | |
{ | |
if (string.Equals(car._model, car2._model)) | |
{ | |
return string.Equals(car._year, car2._year); | |
} | |
return false; | |
} | |
return false; | |
} | |
Phone phone = (Phone)this; | |
Phone phone2 = (Phone)obj; | |
if (phone.oS.Equals(phone2.oS)) | |
{ | |
return string.Equals(phone._model, phone2._model); | |
} | |
return false; | |
} | |
return false; | |
} | |
return false; | |
} | |
return obj == null; | |
} | |
[CompilerGenerated] | |
public sealed override bool Equals(object obj) | |
{ | |
ThingIOwn thingIOwn = obj as ThingIOwn; | |
if (thingIOwn != null) | |
{ | |
return Equals(thingIOwn); | |
} | |
return false; | |
} | |
} | |
static class Startup { | |
public static void Main() { | |
var thing = ThingIOwn.NewPhone(PhoneOS.Android, "Pixel 2L"); | |
// can use with switch statements: | |
switch (thing) { | |
case ThingIOwn.Car car1: | |
Console.WriteLine($"I have a {car1.year} {car1.make} {car1.model}"); | |
break; | |
case ThingIOwn.Phone phone: | |
Console.WriteLine($"I have a {phone.model} running {phone.OS}"); | |
break; | |
} | |
// or use with type-based matching | |
if (thing is ThingIOwn.Car car) { | |
Console.WriteLine($"I have a {car.year} {car.make} {car.model}"); | |
} else if (thing is ThingIOwn.Phone phone) { | |
Console.WriteLine($"I have a {phone.model} running {phone.OS}"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment