View previous topic :: View next topic |
Author |
Message |
brad Site Admin
Joined: 22 Feb 2004 Posts: 490 Location: Atlanta, GA USA
|
Posted: Sat Jul 03, 2004 1:12 am Post subject: Java Array --> D Array |
|
|
(duplicate post in NG)
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:
Code: |
// Java Code (sort of)
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 _________________ I really like the vest! |
|
Back to top |
|
|
Blandger
Joined: 21 May 2004 Posts: 50 Location: Ukraine, Kharkov
|
Posted: Sat Jul 03, 2004 4:10 am Post subject: Re: Java Array --> D Array |
|
|
brad wrote: | Code: |
// Java Code (sort of)
public class Control : Widget , IDrawable {
...
if (getVisible () && getEnabled ()) {
return new Control [] [this]; // not sure what this is.
}
...
}
|
|
I've changed it yesterday as:
Code: |
..
Control[1] control;
control[] = this;
return control;
...
|
Hope it's right and think it can be rewritten another way. _________________ Regards, Yuriy |
|
Back to top |
|
|
kris
Joined: 27 Mar 2004 Posts: 1494 Location: South Pacific
|
Posted: Sat Jul 03, 2004 12:22 pm Post subject: Re: Java Array --> D Array |
|
|
brad wrote: | (duplicate post in NG)
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:
Code: |
// Java Code (sort of)
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 |
The function returns a heap alllocated Control[] with either 1 or zero entries. In the first case, the entry contains a reference to the enclosing class (this), whereas in the second case there's nothing present at all (an empty array).
The funky looking array assignment is just that. I don't know of an equivalent shortcut in D, since D thinks you're trying to allocate a multidimentional array. You can always unwind the assignment like so:
Code: | {
Control[] c = new Control[1];
c[0] = this;
return c;
} |
or like so
Code: | {
Control[1] c = this;
return c.dup;
}
|
which is effectively what Blanger was suggesting, but with a heap allocation instead of what I suspect is the return of a stack assignment.
- Kris |
|
Back to top |
|
|
brad Site Admin
Joined: 22 Feb 2004 Posts: 490 Location: Atlanta, GA USA
|
Posted: Sat Jul 03, 2004 12:46 pm Post subject: |
|
|
Kris,
After a go-around in the NG, and help from Walter, I arrived at your first suggestion.
Code: |
{
Control[] ret = new Control[1];
ret[0] = this;
return ret;
}
|
Thanks, all ! _________________ I really like the vest! |
|
Back to top |
|
|
kris
Joined: 27 Mar 2004 Posts: 1494 Location: South Pacific
|
Posted: Sat Jul 03, 2004 12:49 pm Post subject: |
|
|
Oops; too slow (per usual) ... |
|
Back to top |
|
|
andy
Joined: 15 Mar 2004 Posts: 71
|
Posted: Sun Jul 25, 2004 11:11 am Post subject: |
|
|
I'm a bit too late, but you can also do
Code: | return (&this)[0 .. 1].dup; |
for ultimate terseness. _________________ "Complacency is a far more dangerous attitude than outrage." - Naomi Littlebear |
|
Back to top |
|
|
|