Make a variable single-assignment?

Ary Manzana ary at esperanto.org.ar
Tue Nov 22 07:58:39 PST 2011


On 11/21/11 1:17 PM, Kapps wrote:
> For one reason, public fields that lack a set without having to create a
> backing field, followed by a bulky property. It does sound lazy, but
> when it's something you have to repeat many times, it gets annoying.
>
> On 21/11/2011 9:43 AM, Ary Manzana wrote:
>> On 11/21/11 11:04 AM, Alex Rønne Petersen wrote:
>>> Hi,
>>>
>>> Is there any way to make a variable single-assignment, regardless of its
>>> type? I.e.:
>>>
>>> void foo()
>>> {
>>> <some magical keyword?> int i = 0;
>>> i = 2; // Error: i cannot be reassigned
>>> }
>>>
>>> I realize const and immutable will do this, but they are transitive and
>>> infect the type, which I do *not* want. I simply want the variable to be
>>> single-assignment. Is it possible?
>>>
>>> - Alex
>>
>> Why do you want that?
>

But that's different from what you asked. In your example "i" is a local 
variable. You want that for public fields...

In Ruby you'd do:

attr_reader :foo

Isn't some standard mixin in D for that? (if the accesor method gets 
inline then the method call shouldn't affect the performance)


More information about the Digitalmars-d-learn mailing list