Thread overview
Type to Type[]
Jan 21, 2005
Kris
Jan 21, 2005
BreMac
Jan 21, 2005
Kris
Jan 21, 2005
BreMac
Jan 22, 2005
Vathix
Jan 21, 2005
BreMac
Jan 21, 2005
Chris Sauls
January 21, 2005
Is there any shortcut way to create an array and insert a single element (one line only)?

Instead of this:
    Object object;
    Object[] array;
    array ~= object;

I wanna write something like this:
    Object object;
    Object[] array = cast(Object[]) object;

Thanks
-- 
Miguel Ferreira Simoes


January 21, 2005
In article <csrm93$ofa$1@digitaldaemon.com>, Miguel Ferreira Simões says...
>
>Is there any shortcut way to create an array and insert a single element (one line only)?
>
>Instead of this:
>    Object object;
>    Object[] array;
>    array ~= object;
>
>I wanna write something like this:
>    Object object;
>    Object[] array = cast(Object[]) object;
>
>Thanks
>-- 
>Miguel Ferreira Simoes
>
>

You can do something like this:

# array = (&object)[0..1];


January 21, 2005
In article <csrm93$ofa$1@digitaldaemon.com>, Miguel Ferreira Simões says...
>
>Is there any shortcut way to create an array and insert a single element (one line only)?
>
>Instead of this:
>    Object object;
>    Object[] array;
>    array ~= object;
>
>I wanna write something like this:
>    Object object;
>    Object[] array = cast(Object[]) object;
>
>Thanks
>-- 
>Miguel Ferreira Simoes
>
>

I may be mistaken, but there appears to be no way to do this. :\ I've tried, and
pointer manipulations of any kind seem to be forbidden by the language.
(Ie. trying to set *(array[].ptr) = object causes runtime errors.) If this was
allowed though... I can think of several ways to do it.


January 21, 2005
In article <csrndk$q21$1@digitaldaemon.com>, Kris says...
>
>In article <csrm93$ofa$1@digitaldaemon.com>, Miguel Ferreira Simões says...
>>
>>Is there any shortcut way to create an array and insert a single element (one line only)?
>>
>>Instead of this:
>>    Object object;
>>    Object[] array;
>>    array ~= object;
>>
>>I wanna write something like this:
>>    Object object;
>>    Object[] array = cast(Object[]) object;
>>
>>Thanks
>>-- 
>>Miguel Ferreira Simoes
>>
>>
>
>You can do something like this:
>
># array = (&object)[0..1];
>
>
That causes an Access Violation :\


January 21, 2005
In article <csrnrt$qkl$1@digitaldaemon.com>, BreMac says...
>
>># array = (&object)[0..1];
>>
>>
>That causes an Access Violation :\

It certainly will if 'object' is null at the time :-)


January 21, 2005
In article <csrp44$s3q$1@digitaldaemon.com>, Kris says...
>
>In article <csrnrt$qkl$1@digitaldaemon.com>, BreMac says...
>>
>>># array = (&object)[0..1];
>>>
>>>
>>That causes an Access Violation :\
>
>It certainly will if 'object' is null at the time :-)
>
>
Well, it is initialized... Must be just my compiler (dmd .110).


January 21, 2005
Thanks!


January 21, 2005
In article <csrm93$ofa$1@digitaldaemon.com>, Miguel Ferreira Simões says...
>
>Is there any shortcut way to create an array and insert a single element (one line only)?
>
>Instead of this:
>    Object object;
>    Object[] array;
>    array ~= object;
>
>I wanna write something like this:
>    Object object;
>    Object[] array = cast(Object[]) object;
>
>Thanks
>-- 
>Miguel Ferreira Simoes
>
>

The simplest solution would be array literals, which Walter has stated will come into D in the future -- /hopefully/ before the 1.0 as I consider them an (nearly anyhow) essential tool.  Once we have them, you would do something like this:

#
#  Object object = new Object;
#  Object[] array = [object];
#

-- Chris Sauls


January 22, 2005
On Fri, 21 Jan 2005 20:36:21 +0000 (UTC), Kris <Kris_member@pathlink.com> wrote:

> In article <csrnrt$qkl$1@digitaldaemon.com>, BreMac says...
>>
>>> # array = (&object)[0..1];
>>>
>>>
>> That causes an Access Violation :\
>
> It certainly will if 'object' is null at the time :-)
>
>

It can be null. The issue is if object goes out of scope when still using array. To be safe, use array = (&object)[0..1].dup;