Jump to page: 1 2
Thread overview
Make a variable single-assignment?
Nov 21, 2011
Trass3r
Nov 21, 2011
Ary Manzana
Nov 21, 2011
Kapps
Nov 22, 2011
Ary Manzana
Nov 21, 2011
Timon Gehr
Nov 21, 2011
deadalnix
Nov 21, 2011
Robert Clipsham
Nov 21, 2011
Jesse Phillips
Nov 21, 2011
Andrej Mitrovic
Nov 30, 2011
Stewart Gordon
Nov 30, 2011
Timon Gehr
November 21, 2011
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
November 21, 2011
Don't think so.
You could also wrap it in a struct with disabled opAssign, but this would also change the type.
November 21, 2011
On 21-11-2011 15:48, Trass3r wrote:
> Don't think so.
> You could also wrap it in a struct with disabled opAssign, but this
> would also change the type.

Perhaps allowing 'final' on fields and locals would be a nice way to gain this effect...

- Alex
November 21, 2011
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?
November 21, 2011
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?

November 21, 2011
On 11/21/2011 03:04 PM, 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

How should that be possible without infecting the type?

void main(){
    <magical keyword> int i = 0;
    auto a = &i;
    *a = 2; // oops...
}
November 21, 2011
Le 21/11/2011 15:04, Alex Rønne Petersen a écrit :
> 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

You can create a struct Final you could use as Final!(type) variable;

Overloading opAssign should do whatever you need.

I don't think adding to the core language what could ba achived with a nice abstraction should be done.
November 21, 2011
On 21-11-2011 17:17, 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?
>

Exactly. In general, it would be useful for guaranteeing that you don't make an accidental assignment to a local or field. Just because you don't want it reassignable doesn't mean you don't want the *contents* reassignable, hence why transitive immutable is not acceptable.

- Alex
November 21, 2011
On 21/11/2011 14:04, 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

In D1 you could use final, in D2 your choices are either const, immutable, or as others have suggested, some sort of a wrapper. You could also use enum if you only want to work with primitive types and the value can be calculated at compile time.

-- 
Robert
http://octarineparrot.com/
November 21, 2011
What you are describing is Head Const, and is not available.

http://www.d-programming-language.org/const-faq.html#head-const

It will not be added as it doesn't provide any guarantees about the code that is useful to the compiler. It can't be added to the existing system without complicating the type system even more, which outweighs the benefits.

Tail Const, a more useful direction has been shafted for much the same reason. There is however a pull request to add support into the compiler.
« First   ‹ Prev
1 2