www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Converting Java code to D

reply "Mike James" <foo bar.com> writes:
Here is a fragment of Java code from an SWT program...

public enum LineStyle {
     NONE("None"),
     SOLID("Solid"),
     DASH("Dash"),
     DOT("Dot"),
     DASHDOT("Dash Dot"),
     DASHDOTDOT("Dash Dot Dot");

     public final String label;

     private LineStyle(String label) {
         this.label = label;
     }
}

What would be the best ('canonical') way of translating it to D?

Regards,

-=mike=-
Apr 20 2015
next sibling parent reply "John Colvin" <john.loughran.colvin gmail.com> writes:
On Monday, 20 April 2015 at 15:28:04 UTC, Mike James wrote:
 Here is a fragment of Java code from an SWT program...

 public enum LineStyle {
     NONE("None"),
     SOLID("Solid"),
     DASH("Dash"),
     DOT("Dot"),
     DASHDOT("Dash Dot"),
     DASHDOTDOT("Dash Dot Dot");

     public final String label;

     private LineStyle(String label) {
         this.label = label;
     }
 }

 What would be the best ('canonical') way of translating it to D?

 Regards,

 -=mike=-
I'm not too hot at Java, but I'm pretty sure this gives you what you want. Perhaps "immutable" instead of "enum" would be closer to what Java does with enum members, but I don't know. struct LineStyle { enum NONE = "None"; enum SOLID = "Solid"; enum DASH = "Dash"; enum DOT = "Dot"; enum DASHDOT = "Dash Dot"; enum DASHDOTDOT = "Dash Dot Dot"; string label; private this(string label) { this.label = label; } }
Apr 20 2015
parent reply "bearophile" <bearophileHUGS lycos.com> writes:
John Colvin:

 struct LineStyle
 {
     enum NONE = "None";
     enum SOLID = "Solid";
     enum DASH = "Dash";
     enum DOT = "Dot";
     enum DASHDOT = "Dash Dot";
     enum DASHDOTDOT = "Dash Dot Dot";

     string label;

     private this(string label)
     {
         this.label = label;
     }
 }
The constructor doesn't look very useful. Perhaps a named enum is safer. Bye, bearophile
Apr 20 2015
parent reply "John Colvin" <john.loughran.colvin gmail.com> writes:
On Monday, 20 April 2015 at 17:24:30 UTC, bearophile wrote:
 John Colvin:

 struct LineStyle
 {
    enum NONE = "None";
    enum SOLID = "Solid";
    enum DASH = "Dash";
    enum DOT = "Dot";
    enum DASHDOT = "Dash Dot";
    enum DASHDOTDOT = "Dash Dot Dot";

    string label;

    private this(string label)
    {
        this.label = label;
    }
 }
The constructor doesn't look very useful. Perhaps a named enum is safer. Bye, bearophile
True, the constructor doesn't really add anything here. To be honest, the combination of enumeration and runtime variables in the Java code seems like a rubbish design, but perhaps there's a good reason for it that I'm not aware of.
Apr 20 2015
next sibling parent "Mike James" <foo bar.com> writes:
On Monday, 20 April 2015 at 17:28:27 UTC, John Colvin wrote:
 On Monday, 20 April 2015 at 17:24:30 UTC, bearophile wrote:
 John Colvin:

 struct LineStyle
 {
   enum NONE = "None";
   enum SOLID = "Solid";
   enum DASH = "Dash";
   enum DOT = "Dot";
   enum DASHDOT = "Dash Dot";
   enum DASHDOTDOT = "Dash Dot Dot";

   string label;

   private this(string label)
   {
       this.label = label;
   }
 }
The constructor doesn't look very useful. Perhaps a named enum is safer. Bye, bearophile
True, the constructor doesn't really add anything here. To be honest, the combination of enumeration and runtime variables in the Java code seems like a rubbish design, but perhaps there's a good reason for it that I'm not aware of.
Maybe they extended enum to get over the lack of structs. Looking at the spec for java enums it appears that you can return an enumeration or the associated string using the original code. Regards, -=mike=-
Apr 20 2015
prev sibling parent Russel Winder via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
On Mon, 2015-04-20 at 17:28 +0000, John Colvin via Digitalmars-d-learn wrot=
e:
 [=E2=80=A6]
=20
 True, the constructor doesn't really add anything here.
=20
 To be honest, the combination of enumeration and runtime=20
 variables in the Java code seems like a rubbish design, but=20
 perhaps there's a good reason for it that I'm not aware of.
It's "Type Safe Enum" from Java and C++: http://docs.oracle.com/javase/8/docs/technotes/guides/language/enums.ht ml http://www.javapractices.com/topic/TopicAction.do?Id=3D1 http://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Type_Safe_Enum --=20 Russel. =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D Dr Russel Winder t: +44 20 7585 2200 voip: sip:russel.winder ekiga.n= et 41 Buckmaster Road m: +44 7770 465 077 xmpp: russel winder.org.uk London SW11 1EN, UK w: www.russel.org.uk skype: russel_winder
Apr 20 2015
prev sibling parent reply Steven Schveighoffer <schveiguy yahoo.com> writes:
On 4/20/15 11:28 AM, Mike James wrote:
 Here is a fragment of Java code from an SWT program...

 public enum LineStyle {
      NONE("None"),
      SOLID("Solid"),
      DASH("Dash"),
      DOT("Dot"),
      DASHDOT("Dash Dot"),
      DASHDOTDOT("Dash Dot Dot");

      public final String label;

      private LineStyle(String label) {
          this.label = label;
      }
 }

 What would be the best ('canonical') way of translating it to D?
enum LineStyle : string { NONE = "None", SOLID = "Solid", ... // etc } Used like this: funcThatTakesString(LineStyle.NONE); LineStyle ls = LineStyle.SOLID; funcThatTakesLineStyle(ls); I'm not a Java programmer, and my time with Java was before enums. But this is how I would do it. -Steve
Apr 20 2015
parent Jacob Carlborg <doob me.com> writes:
On 2015-04-20 20:05, Steven Schveighoffer wrote:

 enum LineStyle : string {
     NONE = "None",
     SOLID = "Solid",
     ... // etc
 }

 Used like this:

 funcThatTakesString(LineStyle.NONE);

 LineStyle ls = LineStyle.SOLID;

 funcThatTakesLineStyle(ls);

 I'm not a Java programmer, and my time with Java was before enums. But
 this is how I would do it.
This is probably the best translation, depending on if the Java API needs to be retained or not. "label" is not included in this translation, assuming you can access that in Java. -- /Jacob Carlborg
Apr 20 2015