Dynamic Arrays & Class Properties
Denis Koroskin
2korden at gmail.com
Thu Aug 28 15:39:13 PDT 2008
On Fri, 29 Aug 2008 01:19:34 +0400, Jarrett Billingsley
<kb3ctd2 at yahoo.com> wrote:
> "Denis Koroskin" <2korden at gmail.com> wrote in message
> news:op.ugl05wn1o7cclz at proton.creatstudio.intranet...
>> On Thu, 28 Aug 2008 22:22:44 +0400, Sergey Gromov
>> <snake.scaly at gmail.com>
>> wrote:
>>
>>> Mason Green <mason.green at gmail.com> wrote:
>>>> Hi,
>>>>
>>>> Anyone know how to expose dynamic array elements via class properties?
>>>> I would like to do something this this:
>>>>
>>>> class Foo {
>>>>
>>>> private int[] m_dummy;
>>>>
>>>> this() {
>>>> m_dummy ~= 19;
>>>> m_dummy ~= 77;
>>>> }
>>>>
>>>> int dummy( ??????? ) {
>>>> return m_dummy[x];
>>>> }
>>>> }
>>>>
>>>> void main() {
>>>> auto foo = new Foo();
>>>> Cout(foo.dummty[0]); // Print 19
>>>> }
>>>>
>>>> Any help would be much appreciated! The question marks are where I'm
>>>> stuck....
>>>
>>> class Foo {
>>> private int[] m_dummy;
>>> const int dummy() {
>>> return m_dummy;
>>> }
>>> }
>>>
>>> I think this is better than tons of templates.
>>>
>>
>> First, it should be like this:
>>
>> class Foo {
>> this()
>> {
>> m_dummy = new int[1];
>> m_dummy[0] = 19;
>> }
>>
>> const(int)[] dummy() {
>> return m_dummy;
>> }
>>
>> private int[] m_dummy;
>> }
>>
>> Second, that's D2 :)
>>
>> That's kinda strange, but unfortunately most of the discussion here is
>> about D1, not D2 :(
>> There is no const in D1:
>>
>> const int[] test = [0, 1, 2, 3];
>> test[0] = 2;
>>
>> writefln(test[0]); // prints 2
>
> On Windows. I'd bet that'd give you a segfault on Linux. Though it
> doesn't
> change the fact that it's actually incorrect and that the compiler really
> *should* give an error for it.
>
>
There is also a final in D1:
final char[] test = "hello";
test.length = 4; // Error: cannot modify final variable 'test'
test = "world"; // Error: cannot modify final variable 'test'
but it doesn't work very well, too:
test[0] = '!'; // success
and everything compiles "fine" with -v1 switch (what does it do?):
final char[] test = "hello";
test.length = 4; // no error
More information about the Digitalmars-d-learn
mailing list