Thread overview
Converting Java code to D
Apr 20, 2015
Mike James
Apr 20, 2015
John Colvin
Apr 20, 2015
bearophile
Apr 20, 2015
John Colvin
Apr 20, 2015
Mike James
Apr 20, 2015
Russel Winder
Apr 21, 2015
Jacob Carlborg
April 20, 2015
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=-
April 20, 2015
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;
    }
}
April 20, 2015
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
April 20, 2015
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.
April 20, 2015
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=-
April 20, 2015
On Mon, 2015-04-20 at 17:28 +0000, John Colvin via Digitalmars-d-learn wrote:
> […]
> 
> 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.

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=1
http://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Type_Safe_Enum

-- 
Russel. ============================================================================= Dr Russel Winder      t: +44 20 7585 2200   voip: sip:russel.winder@ekiga.net 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




April 20, 2015
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
April 21, 2015
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