digitalmars.D.learn - d equivilent of java's public static class fields
- %u (78/78) Aug 08 2010 I'm porting some code from Java to D and am getting stuck on what Java c...
- Simen kjaeraas (27/35) Aug 08 2010 [snip]
- bearophile (4/15) Aug 08 2010 'Correctness' there begs to be a typedef :-]
- Simen kjaeraas (4/17) Aug 08 2010 Don't I know it. But typedefs are supposed to go away.
- Lutger (2/19) Aug 08 2010 I think it wants to go boolean.
- Jacob Carlborg (4/37) Aug 09 2010 --
- Simen kjaeraas (5/6) Aug 09 2010 Uhm, since I made it up? Sorry about that, I believe I
- Jacob Carlborg (5/83) Aug 09 2010 I don't know how you are trying to use your class but I don't see any
I'm porting some code from Java to D and am getting stuck on what Java calls static class fields. I can't find out how to write the functional (or best practices) equivalent in D. (There are other syntax errors in D code, I'm fixing them one by one) an Enum, but .... haven't) public class Answer { /* I've tried various combinations of syntax here */ static int CORRECT = 0; public const int WRONG = 1; private int _answerCode; private string _answerText; /* */ public this(int answerCode, string answerText) { _answerCode = answerCode; _answerText = answerText; } /* */ public int getAnswerCode() { return _answerCode; } /* */ public string getAnswerText() { return _answerText; } } public class Answer { public static final int CORRECT = 0; public static final int WRONG = 1; private int _answerCode; private String _answerText; /* */ public Answer(int answerCode, String answerText) { _answerCode = answerCode; _answerText = answerText; } /* */ public int getAnswerCode() { return _answerCode; } /* */ public String getAnswerText() { return _answerText; } } int asd = Answer.CORRECT; <-- compile error here try { answer = problem.getAnswer(reply); // grade answer if (answer.getAnswerCode() == Answer.CORRECT) <-- compile error here inOut.displayAnswer("Correct"); else inOut.displayAnswer("Nope, " ~ answer.getAnswerText()); } catch (Exception iae) { inOut.displayAnswer(iae.getMessage()); repeatQuestion = true; // show same question again }
Aug 08 2010
%u <throwaway limewall.org> wrote:I'm porting some code from Java to D and am getting stuck on what Java calls static class fields. I can't find out how to write the functional (or best practices) equivalent in D.[snip]static int CORRECT = 0;[snip]int asd = Answer.CORRECT; <-- compile error here[snip]if (answer.getAnswerCode() == Answer.CORRECT) <-- compile error hereThe default access level in D is private, so CORRECT will not be available outside the module in which Answer is defined. As you mention, you should probably use an enum for wrong/correct. However, due to lookup rules, an embedded enum would require users to specify Answer.Correctness.CORRECT/WRONG, so separate enums for each might be a better idea. The most common way to expose the stored values in D code would probably be as properties: class Answer { alias int Correctness; enum Correctness CORRECT = 0; enum Correctness WRONG = 1; Correctness _answerCode; string _answerText; property Correctness answerCode( ) const { return _answerCode; } property string answerText( ) const { return _answerText; } } -- Simen
Aug 08 2010
Simen kjaeraas:class Answer { alias int Correctness; enum Correctness CORRECT = 0; enum Correctness WRONG = 1; Correctness _answerCode; string _answerText; property Correctness answerCode( ) const { return _answerCode; }'Correctness' there begs to be a typedef :-] Bye, bearophile
Aug 08 2010
bearophile <bearophileHUGS lycos.com> wrote:Simen kjaeraas:Don't I know it. But typedefs are supposed to go away. -- Simenclass Answer { alias int Correctness; enum Correctness CORRECT = 0; enum Correctness WRONG = 1; Correctness _answerCode; string _answerText; property Correctness answerCode( ) const { return _answerCode; }'Correctness' there begs to be a typedef :-]
Aug 08 2010
bearophile wrote:Simen kjaeraas:I think it wants to go boolean.class Answer { alias int Correctness; enum Correctness CORRECT = 0; enum Correctness WRONG = 1; Correctness _answerCode; string _answerText; property Correctness answerCode( ) const { return _answerCode; }'Correctness' there begs to be a typedef :-] Bye, bearophile
Aug 08 2010
On 2010-08-08 21:28, Simen kjaeraas wrote:%u <throwaway limewall.org> wrote:Since when is the default access level in D private?I'm porting some code from Java to D and am getting stuck on what Java calls static class fields. I can't find out how to write the functional (or best practices) equivalent in D.[snip]static int CORRECT = 0;[snip]int asd = Answer.CORRECT; <-- compile error here[snip]if (answer.getAnswerCode() == Answer.CORRECT) <-- compile error hereThe default access level in D is private, so CORRECT will not be available outside the module in which Answer is defined.As you mention, you should probably use an enum for wrong/correct. However, due to lookup rules, an embedded enum would require users to specify Answer.Correctness.CORRECT/WRONG, so separate enums for each might be a better idea. The most common way to expose the stored values in D code would probably be as properties: class Answer { alias int Correctness; enum Correctness CORRECT = 0; enum Correctness WRONG = 1; Correctness _answerCode; string _answerText; property Correctness answerCode( ) const { return _answerCode; } property string answerText( ) const { return _answerText; } }-- /Jacob Carlborg
Aug 09 2010
Jacob Carlborg <doob me.com> wrote:Since when is the default access level in D private?Uhm, since I made it up? Sorry about that, I believe I mixed it up with C++. -- Simen
Aug 09 2010
On 2010-08-08 21:12, %u wrote:I'm porting some code from Java to D and am getting stuck on what Java calls static class fields. I can't find out how to write the functional (or best practices) equivalent in D. (There are other syntax errors in D code, I'm fixing them one by one) an Enum, but .... haven't) public class Answer { /* I've tried various combinations of syntax here */ static int CORRECT = 0; public const int WRONG = 1; private int _answerCode; private string _answerText; /* */ public this(int answerCode, string answerText) { _answerCode = answerCode; _answerText = answerText; } /* */ public int getAnswerCode() { return _answerCode; } /* */ public string getAnswerText() { return _answerText; } } public class Answer { public static final int CORRECT = 0; public static final int WRONG = 1; private int _answerCode; private String _answerText; /* */ public Answer(int answerCode, String answerText) { _answerCode = answerCode; _answerText = answerText; } /* */ public int getAnswerCode() { return _answerCode; } /* */ public String getAnswerText() { return _answerText; } } int asd = Answer.CORRECT;<-- compile error here try { answer = problem.getAnswer(reply); // grade answer if (answer.getAnswerCode() == Answer.CORRECT)<-- compile error here inOut.displayAnswer("Correct"); else inOut.displayAnswer("Nope, " ~ answer.getAnswerText()); } catch (Exception iae) { inOut.displayAnswer(iae.getMessage()); repeatQuestion = true; // show same question again }I don't know how you are trying to use your class but I don't see any errors in your code. -- /Jacob Carlborg
Aug 09 2010