digitalmars.D.learn - Converting Java code to D
- Mike James (16/16) Apr 20 2015 Here is a fragment of Java code from an SWT program...
- John Colvin (18/34) Apr 20 2015 I'm not too hot at Java, but I'm pretty sure this gives you what
- bearophile (5/19) Apr 20 2015 The constructor doesn't look very useful.
- John Colvin (5/26) Apr 20 2015 True, the constructor doesn't really add anything here.
- Mike James (5/35) Apr 20 2015 Maybe they extended enum to get over the lack of structs.
- Russel Winder via Digitalmars-d-learn (17/24) Apr 20 2015 It's "Type Safe Enum" from Java and C++:
- Steven Schveighoffer (13/27) Apr 20 2015 enum LineStyle : string {
- Jacob Carlborg (6/17) Apr 20 2015 This is probably the best translation, depending on if the Java API
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
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
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
On Monday, 20 April 2015 at 17:24:30 UTC, bearophile wrote:John Colvin: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.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
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: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=-John Colvin: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.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
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
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
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