Thread overview | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
August 08, 2010 d equivilent of java's public static class fields | ||||
---|---|---|---|---|
| ||||
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) ######### Begin D code (ported from Java code below, and I probably should use 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; } } ####### End D code code ####### Begin Java code (included to show how it was done in java) 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; } } ###### End Java code ######### Begin D code snippet trying to use the Answer class 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 } ######### End D code snippet |
August 08, 2010 Re: d equivilent of java's public static class fields | ||||
---|---|---|---|---|
| ||||
Posted in reply to %u | %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 here The 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 |
August 08, 2010 Re: d equivilent of java's public static class fields | ||||
---|---|---|---|---|
| ||||
Posted in reply to Simen kjaeraas | 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
|
August 08, 2010 Re: d equivilent of java's public static class fields | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | bearophile <bearophileHUGS@lycos.com> wrote: > 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 :-] Don't I know it. But typedefs are supposed to go away. -- Simen |
August 08, 2010 Re: d equivilent of java's public static class fields | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | bearophile wrote:
> 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
I think it wants to go boolean.
|
August 09, 2010 Re: d equivilent of java's public static class fields | ||||
---|---|---|---|---|
| ||||
Posted in reply to %u | 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) > > ######### Begin D code (ported from Java code below, and I probably should use > 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; > } > } > ####### End D code code > > ####### Begin Java code (included to show how it was done in java) > 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; > } > } > ###### End Java code > > ######### Begin D code snippet trying to use the Answer class > 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 > } > ######### End D code snippet I don't know how you are trying to use your class but I don't see any errors in your code. -- /Jacob Carlborg |
August 09, 2010 Re: d equivilent of java's public static class fields | ||||
---|---|---|---|---|
| ||||
Posted in reply to Simen kjaeraas | On 2010-08-08 21:28, Simen kjaeraas wrote: > %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 here > > The default access level in D is private, so CORRECT will not be > available outside the module in which Answer is defined. Since when is the default access level in D private? > 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 |
August 09, 2010 Re: d equivilent of java's public static class fields | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jacob Carlborg | 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 |
Copyright © 1999-2021 by the D Language Foundation