Skip to content

Instantly share code, notes, and snippets.

@colinbull
Created July 22, 2014 12:52
Show Gist options
  • Save colinbull/4488811d3130645fa28d to your computer and use it in GitHub Desktop.
Save colinbull/4488811d3130645fa28d to your computer and use it in GitHub Desktop.
Comparing C#, C# + StyleCop and F#
////F#
type AddressContext =
| CustomerAddressContext of customer:Customer
| SiteAddressContext of site:Site
| ContactAddressContext of contact:Contact
with
member x.Id =
match x with
| CustomerAddressContext _ -> 1
| SiteAddressContext _ -> 2
| ContactAddressContext _ -> 3
////Vanilla C#
public abstract class AddressContext
{
public abstract int Id { get; }
public class CustomerAddressContext : AddressContext
{
public CustomerAddressContext(Customer customer)
{
this.Customer = customer;
}
public Customer Customer { get; private set; }
public override int Id { get { return 1; } }
}
public class SiteAddressContext : AddressContext
{
public SiteAddressContext(Site site)
{
this.Site = site;
}
public override int Id { get { return 2; } }
public Site Site { get; private set; }
}
public class ContactAddressContext : AddressContext
{
public override int Id { get { return 3; } }
}
public T Match<T>(
Func<CustomerAddressContext, T> cust,
Func<SiteAddressContext, T> site,
Func<ContactAddressContext, T> contact)
{
var custContext = this as CustomerAddressContext;
if (custContext != null)
{
return cust(custContext);
}
var siteContext = this as SiteAddressContext;
if (siteContext != null)
{
return site(siteContext);
}
var contactContext = this as ContactAddressContext;
if (contactContext != null)
{
return contact(contactContext);
}
throw new IncompleteMatchException(this.GetType());
}
}
////Stylecop C#
/// <summary>
/// The address context.
/// </summary>
public abstract class AddressContext
{
/// <summary>
/// Gets the id.
/// </summary>
/// <value>
/// The id value.
/// </value>
public abstract int Id { get; set; }
/// <summary>
/// The customer address context.
/// </summary>
public class CustomerAddressContext : AddressContext
{
/// <summary>
/// Initializes a new instance of the <see cref="AddressContext.CustomerAddressContext"/> class.
/// </summary>
/// <param name="customer">
/// The customer.
/// </param>
public CustomerAddressContext(Customer customer)
{
this.Customer = customer;
}
/// <summary>
/// Gets the customer.
/// </summary>
/// <value>
/// The customer.
/// </value>
public Customer Customer
{
get; private set;
}
/// <summary>
/// Gets the id.
/// </summary>
/// <value>
/// The id value.
/// </value>
public override int Id
{
get
{
return 1;
}
}
}
/// <summary>
/// The site address context.
/// </summary>
public class SiteAddressContext : AddressContext
{
/// <summary>
/// Initializes a new instance of the <see cref="AddressContext.SiteAddressContext"/> class.
/// </summary>
/// <param name="site">
/// The site instance.
/// </param>
public SiteAddressContext(Site site)
{
this.Site = site;
}
/// <summary>
/// Gets the id.
/// </summary>
/// <value>
/// The id value.
/// </value>
public override int Id
{
get
{
return 2;
}
}
/// <summary>
/// Gets or sets the site.
/// </summary>
/// <value>
/// The site instance.
/// </value>
public Site Site { get; private set; }
}
/// <summary>
/// The contact address context.
/// </summary>
public class ContactAddressContext : AddressContext
{
/// <summary>
/// Gets the id.
/// </summary>
/// <value>
/// The id value.
/// </value>
public override int Id
{
get
{
return 3;
}
}
}
/// <summary>
/// The match function to allow pattern matching.
/// </summary>
/// <param name="cust">
/// The cust function to execute on customer context.
/// </param>
/// <param name="site">
/// The site function to excute on site context.
/// </param>
/// <param name="contact">
/// The contact function to execute on contact context.
/// </param>
/// <typeparam name="T">
/// The return type
/// </typeparam>
/// <returns>
/// The result of the ran functions.
/// </returns>
public T Match<T>(
Func<CustomerAddressContext, T> cust,
Func<SiteAddressContext, T> site,
Func<ContactAddressContext, T> contact)
{
var custContext = this as CustomerAddressContext;
if (custContext != null)
{
return cust(custContext);
}
var siteContext = this as SiteAddressContext;
if (siteContext != null)
{
return site(siteContext);
}
var contactContext = this as ContactAddressContext;
if (contactContext != null)
{
return contact(contactContext);
}
throw new IncompleteMatchException(this.GetType());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment