Thread overview
Bug? tupleof and const string = ""
Feb 13, 2014
Andre
Feb 13, 2014
Tobias Pankrath
Feb 13, 2014
Andre
Feb 13, 2014
bearophile
Feb 13, 2014
John Colvin
Feb 13, 2014
Dicebot
Feb 13, 2014
monarch_dodra
Feb 13, 2014
Andre
February 13, 2014
Hi,

could you have a look whether this is a bug with tupleof?
In case you have a const string with a default value,
the attribute is not in the tupleof list.

struct A {
	const string a = "abc";
	string d;
}

void main(){
	assert(A().tupleof.length == 2); // fails -> length = 1
}

Kind regards
André
February 13, 2014
On Thursday, 13 February 2014 at 16:37:20 UTC, Andre wrote:
> Hi,
>
> could you have a look whether this is a bug with tupleof?
> In case you have a const string with a default value,
> the attribute is not in the tupleof list.
>
> struct A {
> 	const string a = "abc";
> 	string d;
> }
>
> void main(){
> 	assert(A().tupleof.length == 2); // fails -> length = 1
> }
>
> Kind regards
> André

I'm not sure. May guess is the compiler figures that you'll never be able to change a and pulls it out of the struct. What I don't know is, if it should do this without using immutable or enum.

Besides: I don't think that const elements make any sense. What do you want to do?
February 13, 2014
Am 13.02.2014 17:42, schrieb Tobias Pankrath:
> On Thursday, 13 February 2014 at 16:37:20 UTC, Andre wrote:
>> Hi,
>>
>> could you have a look whether this is a bug with tupleof?
>> In case you have a const string with a default value,
>> the attribute is not in the tupleof list.
>>
>> struct A {
>>     const string a = "abc";
>>     string d;
>> }
>>
>> void main(){
>>     assert(A().tupleof.length == 2); // fails -> length = 1
>> }
>>
>> Kind regards
>> André
>
> I'm not sure. May guess is the compiler figures that you'll never be
> able to change a and pulls it out of the struct. What I don't know is,
> if it should do this without using immutable or enum.
>
> Besides: I don't think that const elements make any sense. What do you
> want to do?

It is strange, const string attributes without a value are working fine.

The use case is for communication with a database procedure. The database procedure want the request in JSON format. I created the
structure similiar to the JSON request and serialize it to JSON.
Some of the values should be constant others should be variable.

Kind regards
André


February 13, 2014
Andre:

> could you have a look whether this is a bug with tupleof?
> In case you have a const string with a default value,
> the attribute is not in the tupleof list.
>
> struct A {
> 	const string a = "abc";
> 	string d;
> }
>
> void main(){
> 	assert(A().tupleof.length == 2); // fails -> length = 1
> }

Tupleeof is working correctly here. Currently const/immutable fields don't become struct instance fields. After a transition phase this will be changed, and in some months you will see two fields there. DMD 2.065 has a warning on this.

Bye,
bearophile
February 13, 2014
On Thursday, 13 February 2014 at 16:52:58 UTC, bearophile wrote:
> Andre:
>
>> could you have a look whether this is a bug with tupleof?
>> In case you have a const string with a default value,
>> the attribute is not in the tupleof list.
>>
>> struct A {
>> 	const string a = "abc";
>> 	string d;
>> }
>>
>> void main(){
>> 	assert(A().tupleof.length == 2); // fails -> length = 1
>> }
>
> Tupleeof is working correctly here. Currently const/immutable fields don't become struct instance fields. After a transition phase this will be changed, and in some months you will see two fields there. DMD 2.065 has a warning on this.
>
> Bye,
> bearophile

Don't you mean const/immutable fields with initialisers don't become instance fields? Or is this actually as strange as it sounds?
February 13, 2014
Am 13.02.2014 17:52, schrieb bearophile:
> Andre:
>
>> could you have a look whether this is a bug with tupleof?
>> In case you have a const string with a default value,
>> the attribute is not in the tupleof list.
>>
>> struct A {
>>     const string a = "abc";
>>     string d;
>> }
>>
>> void main(){
>>     assert(A().tupleof.length == 2); // fails -> length = 1
>> }
>
> Tupleeof is working correctly here. Currently const/immutable fields
> don't become struct instance fields. After a transition phase this will
> be changed, and in some months you will see two fields there. DMD 2.065
> has a warning on this.
>
> Bye,
> bearophile


Thanks for the info. Time to update to 2.065:)

Kind regards
André
February 13, 2014
On Thursday, 13 February 2014 at 16:57:19 UTC, John Colvin wrote:
> Don't you mean const/immutable fields with initialisers don't become instance fields? Or is this actually as strange as it sounds?

Yes, it is legacy behavior (== bug) that persisted since D1 days and was addressed only recently. Until this deprecation/transition process will end this:

struct X
{
    const string value = "literal"'
}

is effectively same as this:

struct X
{
    enum value = "literal"'
}
February 13, 2014
On Thursday, 13 February 2014 at 17:12:28 UTC, Dicebot wrote:
> On Thursday, 13 February 2014 at 16:57:19 UTC, John Colvin wrote:
>> Don't you mean const/immutable fields with initialisers don't become instance fields? Or is this actually as strange as it sounds?
>
> Yes, it is legacy behavior (== bug) that persisted since D1 days and was addressed only recently. Until this deprecation/transition process will end this:
>
> struct X
> {
>     const string value = "literal";
> }
>
> is effectively same as this:
>
> struct X
> {
>     enum value = "literal";
> }

To be anal, it's technically the same as:

struct X
{
     static const string value = "literal";
}

The immutable/const member will be made a static member, which
you can deference, or pass by reference.