﻿<?xml version="1.0" encoding="utf-8"?><rss version="2.0"><channel><title>Ayende @ Rahien</title><link>http://ayende.com</link><description>Ayende @ Rahien</description><copyright>Copyright (C) Ayende Rahien  2004 - 2021 (c) 2026</copyright><ttl>60</ttl><item><title>Germ&amp;#225;n Schuager commented on Emulating Java Enums</title><description>How would you map a class with a property of such "enum" type in NHibernate?
</description><link>http://ayende.com/3617/emulating-java-enums#comment14</link><guid>http://ayende.com/3617/emulating-java-enums#comment14</guid><pubDate>Tue, 07 Oct 2008 15:19:18 GMT</pubDate></item><item><title>Ayende Rahien commented on Emulating Java Enums</title><description>This code is for the .NET 2.0, no Func
&lt;t&gt;</description><link>http://ayende.com/3617/emulating-java-enums#comment13</link><guid>http://ayende.com/3617/emulating-java-enums#comment13</guid><pubDate>Tue, 30 Sep 2008 14:36:57 GMT</pubDate></item><item><title>Fred Hirschfeld commented on Emulating Java Enums</title><description>Bruce,
  
  
The extension version looks very nice and you could improve it slightly by adding a custom attribute to the code that would decorate the enum member:
  
  
public enum Planet
  
{
  
    [PlanetMeasurements(3.303e+23, 2.4397e6)]
  
    Mercury,
  
  
    [PlanetMeasurements(4.869e+24, 6.0518e6)]
  
    Venus
  
}
  
  
Then in the extension methods you would simply access these attributes and use their values thus eliminating the need for the switch statements.
</description><link>http://ayende.com/3617/emulating-java-enums#comment12</link><guid>http://ayende.com/3617/emulating-java-enums#comment12</guid><pubDate>Tue, 30 Sep 2008 14:26:51 GMT</pubDate></item><item><title>Mike Brown commented on Emulating Java Enums</title><description>Just realized that my code got mangled by html
  
  
private readonly Func&lt;WorkType,bool&gt; isRightLayer;
</description><link>http://ayende.com/3617/emulating-java-enums#comment11</link><guid>http://ayende.com/3617/emulating-java-enums#comment11</guid><pubDate>Tue, 30 Sep 2008 13:35:43 GMT</pubDate></item><item><title>Mike Brown commented on Emulating Java Enums</title><description>Quick question. What are your thoughts on using the Func
&lt;t derivatives for delegate declaration?
  
  
So instead of:
  
public delegate bool IsRightLayerDelegate(WorkType type);
  
private readonly IsRightLayerDelegate isRightLayer;
  
  
You just have
  
private Func
&lt;worktype,bool&gt;
 isRightLayer;
&gt;</description><link>http://ayende.com/3617/emulating-java-enums#comment10</link><guid>http://ayende.com/3617/emulating-java-enums#comment10</guid><pubDate>Tue, 30 Sep 2008 11:11:42 GMT</pubDate></item><item><title>Andrey Shchekin commented on Emulating Java Enums</title><description>Good choice. Enums are antipattern for most situations anyway, since they lead to having unextensible switch-by-enum instead of using polymorphism.
</description><link>http://ayende.com/3617/emulating-java-enums#comment9</link><guid>http://ayende.com/3617/emulating-java-enums#comment9</guid><pubDate>Tue, 30 Sep 2008 08:25:24 GMT</pubDate></item><item><title>Bruce Boughton commented on Emulating Java Enums</title><description>I'm a big fan of Java enums and I had a look at emulating their functionality a while ago in both C# 2.0 and C# 3.5:
  
  
[siliconbea.ch/.../emulating-java-5-enums-in-dotnet](http://siliconbea.ch/technology/2008-06-08/emulating-java-5-enums-in-dotnet/)  
  
My C# 2.0 solution uses an inner enum and a class that wraps it (with common functionality in a base class).
  
  
[siliconbea.ch/.../emulating-java-5-enums-in-dot...](http://siliconbea.ch/technology/2008-06-08/emulating-java-5-enums-in-dotnet-part-2/)  
  
My C# 3.5 solution uses extension methods.
</description><link>http://ayende.com/3617/emulating-java-enums#comment8</link><guid>http://ayende.com/3617/emulating-java-enums#comment8</guid><pubDate>Tue, 30 Sep 2008 06:37:40 GMT</pubDate></item><item><title>Justin Rudd commented on Emulating Java Enums</title><description>The C# enum isn't quite a replacement for the Java one.  
  
  
As it is, it can't be serialized.  Adding serializable is just one part.  You'd also have to implement whatever the equivalent of readObject and writeObject are to ensure the same instance is returned no matter what.  
  
  
It can't be used in a switch statement.
  
  
You could use the same delegate trick to add methods to get polymorphism like you can with Java enums (
[http://langrsoft.com/articles/enum2.shtml](http://langrsoft.com/articles/enum2.shtml)), but that breaks down pretty quick if you get beyond a few methods.
</description><link>http://ayende.com/3617/emulating-java-enums#comment7</link><guid>http://ayende.com/3617/emulating-java-enums#comment7</guid><pubDate>Tue, 30 Sep 2008 05:10:33 GMT</pubDate></item><item><title>Matthew Podwysocki commented on Emulating Java Enums</title><description>This is why I like F# for this purpose using discriminated unions.  Such code as this works perfectly for modeling what was above:
  
  
#light
  
  
type WorkType =
  
  | Collection
  
  | NonCollection
  
  
type Layer =
  
  | First
  
  | Second
  
with member x.IsRightLayer(workType) =
  
      match (x, workType) with
  
      | l, w when l = First &amp;&amp; w &lt;&gt; Collection -&gt; true
  
      | l, w -&gt; l = Second &amp;&amp; w = Collection
  
  
Then I can use it such as this:
  
let result = Layer.Second.IsRightLayer(WorkType.Collection)
  
  
Matt
</description><link>http://ayende.com/3617/emulating-java-enums#comment6</link><guid>http://ayende.com/3617/emulating-java-enums#comment6</guid><pubDate>Tue, 30 Sep 2008 03:03:08 GMT</pubDate></item><item><title>David commented on Emulating Java Enums</title><description>Oops, shoulda refreshed before posting :-\
</description><link>http://ayende.com/3617/emulating-java-enums#comment5</link><guid>http://ayende.com/3617/emulating-java-enums#comment5</guid><pubDate>Tue, 30 Sep 2008 02:04:56 GMT</pubDate></item><item><title>David commented on Emulating Java Enums</title><description>How about using extension methods?
  
  
public static class LayerExtensions
  
{
  
  public static bool IsRightLayer(this Layer layer, WorkType workType)
  
  {
  
    if (layer == Layer.FIRST &amp;&amp; workType != WorkType.COLLECTION) return true;
  
    return layer == Layer.SECOND &amp;&amp; workType == WorkType.COLLECTION;
  
  }
  
 }
</description><link>http://ayende.com/3617/emulating-java-enums#comment4</link><guid>http://ayende.com/3617/emulating-java-enums#comment4</guid><pubDate>Tue, 30 Sep 2008 02:04:17 GMT</pubDate></item><item><title>Hendry Luk commented on Emulating Java Enums</title><description>Having said that, there's nothing stopping us from implementing it the same way as java counterpart:
  
private static class Layer
  
{
  
     public static readonly Layer FIRST = new Layer();
  
     public static readonly Layer SECOND = new Layer();
  
  
    public boolean IsRightLayer(WorkType type) {
  
		if (this == FIRST &amp;&amp; type != WorkType.COLLECTION) return true;
  
		return this == SECOND &amp;&amp; type == WorkType.COLLECTION;
  
   }
  
}
</description><link>http://ayende.com/3617/emulating-java-enums#comment3</link><guid>http://ayende.com/3617/emulating-java-enums#comment3</guid><pubDate>Tue, 30 Sep 2008 00:30:02 GMT</pubDate></item><item><title>Ayende Rahien commented on Emulating Java Enums</title><description>Java enums also have state, which extension methods do not have.
</description><link>http://ayende.com/3617/emulating-java-enums#comment2</link><guid>http://ayende.com/3617/emulating-java-enums#comment2</guid><pubDate>Mon, 29 Sep 2008 23:15:08 GMT</pubDate></item><item><title>Niki commented on Emulating Java Enums</title><description>Why don't you just declare an extension method on you enum?
  
  
private static enum Layer { FIRST, SECOND }
  
  
private static class LayerExtensions {
  
   public static bool isRightLayer(this Layer layer, WorkType type) {
  
      if (layer == FIRST &amp;&amp; type != WorkType.COLLECTION) return true;
  
      return layer == SECOND &amp;&amp; type == WorkType.COLLECTION;
  
   }
  
}
  
  
I know it's not the same internally, but the syntax would be more or less the same. 
</description><link>http://ayende.com/3617/emulating-java-enums#comment1</link><guid>http://ayende.com/3617/emulating-java-enums#comment1</guid><pubDate>Mon, 29 Sep 2008 22:34:01 GMT</pubDate></item></channel></rss>