www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - HOW? To Object variable assignment.

reply "Blandger" <zeroman aport.ru> writes:
Porting java code I incidentally stuck with such an easy and stupid problem.

Java uses Object variable as 'universal type' for storing 'everything
(although it's not a typesafe).
How legally to assign variable value of some type to the Object variable in
D?

Something like on java:
-------------
 Object [] table = new Object[n];
 String key = "some key";
 table [0] = key; // legal code
...
and vice versa
------------

But for D I stuck:
...
   Object[] obj = new Object[10];
   wchar[] str = "fff";
   obj[0] = cast(Object)str; // error: cannot cast from wchar[] to Object

What's I make wrong?
Jul 13 2004
parent reply "Ben Hinkle" <bhinkle mathworks.com> writes:
D strings don't subclass Object. What else is being stored in "table"? Is it
storing key-value pairs? If so I'd consider using an associative array
indexed by wchar[] and declared as Object[wchar[]].

"Blandger" <zeroman aport.ru> wrote in message
news:cd1ahk$170b$1 digitaldaemon.com...
 Porting java code I incidentally stuck with such an easy and stupid
problem.
 Java uses Object variable as 'universal type' for storing 'everything
 (although it's not a typesafe).
 How legally to assign variable value of some type to the Object variable
in
 D?

 Something like on java:
 -------------
  Object [] table = new Object[n];
  String key = "some key";
  table [0] = key; // legal code
 ...
 and vice versa
 ------------

 But for D I stuck:
 ...
    Object[] obj = new Object[10];
    wchar[] str = "fff";
    obj[0] = cast(Object)str; // error: cannot cast from wchar[] to Object

 What's I make wrong?
Jul 13 2004
parent "Blandger" <zeroman aport.ru> writes:
"Ben Hinkle" <bhinkle mathworks.com> wrote in message
news:cd1bk3$18ra$1 digitaldaemon.com...
 D strings don't subclass Object. What else is being stored in "table"? Is
it
 storing key-value pairs?
It's a DWT code. Something like storing and 'playing' with string key-value but I need time to clarify it. I hoped to run this place fast but now see it will take a time.
 If so I'd consider using an associative array
 indexed by wchar[] and declared as Object[wchar[]].
Thanks for all responds. I'll try some way.
Jul 13 2004