www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - d equivilent of java's public static class fields

reply %u <throwaway limewall.org> writes:
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
next sibling parent reply "Simen kjaeraas" <simen.kjaras gmail.com> writes:
%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
Aug 08 2010
next sibling parent reply bearophile <bearophileHUGS lycos.com> writes:
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
next sibling parent "Simen kjaeraas" <simen.kjaras gmail.com> writes:
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
Aug 08 2010
prev sibling parent Lutger <lutger.blijdestijn gmail.com> writes:
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.
Aug 08 2010
prev sibling parent reply Jacob Carlborg <doob me.com> writes:
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
Aug 09 2010
parent "Simen kjaeraas" <simen.kjaras gmail.com> writes:
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
prev sibling parent Jacob Carlborg <doob me.com> writes:
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