www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Type to Type[]

reply "Miguel Ferreira Simões" <Kobold netcabo.pt> writes:
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 
Jan 21 2005
next sibling parent reply Kris <Kris_member pathlink.com> writes:
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];
Jan 21 2005
next sibling parent reply BreMac <BreMac_member pathlink.com> writes:
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 :\
Jan 21 2005
parent reply Kris <Kris_member pathlink.com> writes:
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 :-)
Jan 21 2005
next sibling parent BreMac <BreMac_member pathlink.com> writes:
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).
Jan 21 2005
prev sibling parent Vathix <vathix dprogramming.com> writes:
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;
Jan 21 2005
prev sibling parent "Miguel Ferreira Simões" <Kobold netcabo.pt> writes:
Thanks! 
Jan 21 2005
prev sibling next sibling parent BreMac <BreMac_member pathlink.com> writes:
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.
Jan 21 2005
prev sibling parent Chris Sauls <Chris_member pathlink.com> writes:
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
Jan 21 2005