FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Java Array --> D Array

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.     Forum Index -> DWT
View previous topic :: View next topic  
Author Message
brad
Site Admin


Joined: 22 Feb 2004
Posts: 490
Location: Atlanta, GA USA

PostPosted: Sat Jul 03, 2004 1:12 am    Post subject: Java Array --> D Array Reply with quote

(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
View user's profile Send private message
Blandger



Joined: 21 May 2004
Posts: 50
Location: Ukraine, Kharkov

PostPosted: Sat Jul 03, 2004 4:10 am    Post subject: Re: Java Array --> D Array Reply with quote

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
View user's profile Send private message
kris



Joined: 27 Mar 2004
Posts: 1494
Location: South Pacific

PostPosted: Sat Jul 03, 2004 12:22 pm    Post subject: Re: Java Array --> D Array Reply with quote

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
View user's profile Send private message
brad
Site Admin


Joined: 22 Feb 2004
Posts: 490
Location: Atlanta, GA USA

PostPosted: Sat Jul 03, 2004 12:46 pm    Post subject: Reply with quote

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
View user's profile Send private message
kris



Joined: 27 Mar 2004
Posts: 1494
Location: South Pacific

PostPosted: Sat Jul 03, 2004 12:49 pm    Post subject: Reply with quote

Oops; too slow (per usual) ...
Back to top
View user's profile Send private message
andy



Joined: 15 Mar 2004
Posts: 71

PostPosted: Sun Jul 25, 2004 11:11 am    Post subject: Reply with quote

I'm a bit too late, but you can also do
Code:
return (&this)[0 .. 1].dup;

for ultimate terseness. Twisted Evil
_________________
"Complacency is a far more dangerous attitude than outrage." - Naomi Littlebear
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.     Forum Index -> DWT All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group