www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Java Array --> D Array

reply Brad Anderson <brad dsource.dot.org> writes:
I'm not terribly knowledgeable about Java arrays and all the permuatations of 
assignment and such.  I don't know what to make of the following:

// Java Code
public class Control : Widget , IDrawable {

   ...

   Control [] computeTabList () {
     if (isTabGroup ()) {
       if (getVisible () && getEnabled ()) {
         return new Control [] [this];         // not sure what this is.
       }
     }
     return new Control [0];
   }

   ...

}


The compiler complains about implicitly converting from Control to int, and I 
get that the array index should be integers, but what is Java doing with 'this' 
in the array index?

My first thought was that this is actually an assignment of the new Control[] 
that is done inside of [ ] in Java.  So I tried to use D's { }, but alas, it's 
not a static array.

Any assistance on how to turn this into D code?

BA
Jul 03 2004
parent reply Sam McCall <tunah.d tunah.net> writes:
Brad Anderson wrote:
         return new Control [] [this];         // not sure what this is.

Me neither, it's not valid java. You probably want return new Control[] {this}, which creates a new Control array (dynamically) initialised to [this]. Sam
Jul 03 2004
parent reply Brad Anderson <brad dsource.dot.org> writes:
Sam McCall wrote:

 Brad Anderson wrote:
 
         return new Control [] [this];         // not sure what this is.

Me neither, it's not valid java.

But it is in the SWT code for 3.0 M6. I can't imagine the Eclipse people would put something out that javac barfs on. I'll look around in the final SWT code for 3.0.
 You probably want return new Control[] {this}, which creates a new 
 Control array (dynamically) initialised to [this].

As for your suggestion, I had already tried that, but DMD said: found '{' when expecting ';' following 'return statement' I settled on this: Control[] ret; ret[0] = this; return ret; which works, but only if I understand their true intent. Thanks for the response, though BA
Jul 03 2004
next sibling parent reply Brad Anderson <brad dsource.dot.org> writes:
         return new Control [] [this];         // not sure what this is.

Me neither, it's not valid java.

But it is in the SWT code for 3.0 M6. I can't imagine the Eclipse people would put something out that javac barfs on. I'll look around in the final SWT code for 3.0.

Whoops. It was {this} in Java (curly braces). In any case, that doesn't work in D. Sorry for the confusion. Still wouldn't mind hearing if my work-around is on target. Thx
Jul 03 2004
parent Sam McCall <tunah.d tunah.net> writes:
Brad Anderson wrote:

         return new Control [] [this];         // not sure what this is.

Me neither, it's not valid java.

But it is in the SWT code for 3.0 M6. I can't imagine the Eclipse people would put something out that javac barfs on. I'll look around in the final SWT code for 3.0.

Whoops. It was {this} in Java (curly braces). In any case, that doesn't work in D. Sorry for the confusion.

Sam
Jul 03 2004
prev sibling parent reply "Walter" <newshound digitalmars.com> writes:
"Brad Anderson" <brad dsource.dot.org> wrote in message
news:cc5opo$2cdr$1 digitaldaemon.com...
 I settled on this:

    Control[] ret;
    ret[0] = this;
    return ret;

Try: Control[] ret = new Control[1]; ret[0] = this; return ret;
Jul 03 2004
parent Andy Friesen <andy ikagames.com> writes:
Walter wrote:
 "Brad Anderson" <brad dsource.dot.org> wrote in message
 news:cc5opo$2cdr$1 digitaldaemon.com...
 
I settled on this:

   Control[] ret;
   ret[0] = this;
   return ret;

Try: Control[] ret = new Control[1]; ret[0] = this; return ret;

Satanic alternative: return (&this)[0 .. 1]; -- andy
Jul 03 2004