View mode: basic / threaded / horizontal-split · Log in · Help
July 10, 2012
readonly?
Maybe D need's a readonly keyword.
Sometimes i have a class which can take an object from 
everywhere to store it. So it can not be const, because i didn't 
just initialized it with a ctor.
But i don't want to change the object, i only want to read or 
call const methods. What now?
I'd suggest a readonly keyword for that.

[code]
class Foo {
readonly:
    Bar _b;

public:
void SetBar(readonly Bar b) {
    _b = b;
}
}
[/code]

Or has D an alternative?
July 10, 2012
Re: readonly?
On Tue, 10 Jul 2012 21:27:54 +0200, Namespace <rswhite4@googlemail.com>  
wrote:

> Maybe D need's a readonly keyword.
> Sometimes i have a class which can take an object from everywhere to  
> store it. So it can not be const, because i didn't just initialized it  
> with a ctor.
> But i don't want to change the object, i only want to read or call const  
> methods. What now?
> I'd suggest a readonly keyword for that.
>
> [code]
> class Foo {
> readonly:
>      Bar _b;
>
> public:
> void SetBar(readonly Bar b) {
>      _b = b;
> }
> }
> [/code]
>
> Or has D an alternative?

If Bar is a class, use std.typecons.Rebindable.

Otherwise, it's hairier. What should readonly mean?

From what I see, it provides const access, and is reassignable. This is  
easily implemented in user code today:

import std.traits;

struct Readonly( T ) {
    private T payload;

    this( Unqual!T value ) {
        payload = value;
    }

    auto opAssign( const T value ) {
        payload = cast()value;
        return this;
    }

    @property
    const(T) get( ) const {
        return payload;
    }

    alias get this;
} unittest {
    struct S {
        int n;

        void foo() {}
        void bar() const {}
    }

    Readonly!S a;
    a = S( 3 );
    assert( a.n == 3 );
    assert( !__traits( compiles, a.n = 4 ) );
    assert( !__traits( compiles, a.foo() ) );
    assert( __traits( compiles, a.bar() ) );
}
July 10, 2012
Re: readonly?
On Tuesday, 10 July 2012 at 19:27:56 UTC, Namespace wrote:
> Maybe D need's a readonly keyword.
> Sometimes i have a class which can take an object from 
> everywhere to store it. So it can not be const, because i 
> didn't just initialized it with a ctor.
> But i don't want to change the object, i only want to read or 
> call const methods. What now?
> I'd suggest a readonly keyword for that.
>
> [code]
> class Foo {
> readonly:
>     Bar _b;
>
> public:
> void SetBar(readonly Bar b) {
>     _b = b;
> }
> }
> [/code]
>
> Or has D an alternative?

const(T)* ?
July 10, 2012
Re: readonly?
> const(T)* ?

Example?
July 10, 2012
Re: readonly?
On 07/10/2012 03:53 PM, Namespace wrote:
>> const(T)* ?
>
> Example?

class Bar
{}

class Foo
{
    const(Bar) * _b;

    void SetBar(const(Bar) * b) {
        _b = b;
    }
}

void main()
{
    auto b = new Bar();
    auto f = new Foo();
    f.SetBar(&b);
}

Ali
July 10, 2012
Re: readonly?
> class Bar
> {}
>
> class Foo
> {
>     const(Bar) * _b;
>
>     void SetBar(const(Bar) * b) {
>         _b = b;
>     }
> }
>
> void main()
> {
>     auto b = new Bar();
>     auto f = new Foo();
>     f.SetBar(&b);
> }
>
> Ali

Hmm... That's good. Thanks.
July 10, 2012
Re: readonly?
On 07/11/2012 12:58 AM, Ali Çehreli wrote:
> On 07/10/2012 03:53 PM, Namespace wrote:
>>> const(T)* ?
>>
>> Example?
>
> class Bar
> {}
>
> class Foo
> {
>      const(Bar) * _b;
>
>      void SetBar(const(Bar) * b) {
>          _b = b;
>      }
> }
>
> void main()
> {
>      auto b = new Bar();
>      auto f = new Foo();
>      f.SetBar(&b);
> }
>
> Ali

This escapes a stack reference.
July 11, 2012
Re: readonly?
> This escapes a stack reference.

Ins't b supposed to be allocated on the heap?
July 11, 2012
Re: readonly?
On Wednesday, July 11, 2012 08:34:28 Tobias Pankrath wrote:
> > This escapes a stack reference.
> 
> Ins't b supposed to be allocated on the heap?

The object is. The reference is not. &b is taking the address of the 
reference, not the object.

- Jonathan M Davis
July 11, 2012
Re: readonly?
On Wednesday, 11 July 2012 at 06:34:29 UTC, Tobias Pankrath wrote:
>> This escapes a stack reference.
>
> Ins't b supposed to be allocated on the heap?

The Bar instance is, but the pointer to it is not. Making _b a 
Rebindable instead of using a pointer (to what effectively is a 
pointer to the real object) should help.

David
« First   ‹ Prev
1 2 3
Top | Discussion index | About this forum | D home