Re: Associative Array of Const Objects?

2015-03-29 Thread ketmar via Digitalmars-d-learn
On Sun, 29 Mar 2015 20:29:49 +, bitwise wrote: The verbosity and blatant disregard for DRY makes me CRY. See what I did there.. ;) you can always `alias` it to something funny or obscene. signature.asc Description: PGP signature

Re: Associative Array of Const Objects?

2015-03-29 Thread bitwise via Digitalmars-d-learn
I'm a little confused at this point why this doesn't work either: const(Test) test = new Test(); // fine test = new Test(); // error In C++, There is a clear distinction: const Test *test1 = nullptr; // const before type test1 = new Test(); // fine Test

Re: Associative Array of Const Objects?

2015-03-29 Thread bitwise via Digitalmars-d-learn
perhaps something like Rebindable could be used. Looking at Rebindable now, there is a useful example. There should probably be a mention of this on the const/immutable docs. For people coming from C++, this will not be obvious. auto a = Rebindable!(const Widget)(new Widget); a.y();

Re: Associative Array of Const Objects?

2015-03-29 Thread bearophile via Digitalmars-d-learn
bitwise: I'm a little confused at this point why this doesn't work either: const and immutable are rather different between C++ and D, I suggest you to take a look at the documentation: http://dlang.org/const-faq.html Bye, bearophile

Re: Associative Array of Const Objects?

2015-03-29 Thread bitwise via Digitalmars-d-learn
On Sunday, 29 March 2015 at 19:04:30 UTC, anonymous wrote: Notice how you have that '*' there that allows you to distinguish the data from the reference. You can have a mutable pointer to const data in D, too: struct Test {} const(Test)* test1 = null; test1 = new Test; /* fine */

Re: Associative Array of Const Objects?

2015-03-29 Thread bitwise via Digitalmars-d-learn
Although, I suppose this is still a step up from C# which has not const at all =O

Re: Associative Array of Const Objects?

2015-03-29 Thread anonymous via Digitalmars-d-learn
On Sunday, 29 March 2015 at 19:13:32 UTC, bitwise wrote: Interesting, but I still don't understand why D doesn't have something like this: const Test test;// or const(Test) test; test = new Test() // fine, underlaying data is const, the reference is not Test const test = new Test();

Re: Associative Array of Const Objects?

2015-03-29 Thread bitwise via Digitalmars-d-learn
1) Such placement based syntax is foreign to D. I would have to agree that this is a strange way to do things in any language. The great int* a vs int *a debate... 2) It would be special syntax just for class types. IMO, it would be worth it 3) It's not how C++ rolls. `const Test test;`

Re: Associative Array of Const Objects?

2015-03-29 Thread anonymous via Digitalmars-d-learn
On Sunday, 29 March 2015 at 18:43:32 UTC, bitwise wrote: I'm a little confused at this point why this doesn't work either: const(Test) test = new Test(); // fine test = new Test(); // error In C++, There is a clear distinction: const Test *test1 = nullptr; // const

Re: Associative Array of Const Objects?

2015-03-29 Thread anonymous via Digitalmars-d-learn
On Sunday, 29 March 2015 at 20:29:50 UTC, bitwise wrote: 3) It's not how C++ rolls. `const Test test;` and `Test const test;` are equivalent in C++. You need that '*' in C++, too, to make a distinction between reference and data. I'm a little confused. I was comparing a C++ pointer-to-class

Re: Associative Array of Const Objects?

2015-03-28 Thread bearophile via Digitalmars-d-learn
of const objects? You meant to say associative array with const objects as values. I think the short answer is that you can't. This is a breaking change, I think, it broke some of my code too. But perhaps something like Rebindable could be used. Bye, bearophile

Re: Associative Array of Const Objects?

2015-03-28 Thread via Digitalmars-d-learn
I insert an item into an associative array of const objects? FWIW, it was changed in https://github.com/D-Programming-Language/dmd/pull/4148 It seems Kenji argues that the first assignment (like in your case) should be allowed, because it's supposed to be a construction rather than

Re: Associative Array of Const Objects?

2015-03-28 Thread Baz via Digitalmars-d-learn
I insert an item into an associative array of const objects? Generally speaking, you can insert an item in a constructor: --- class Test{} const (Test)[string] tests; static this() { tests[test] = new Test(); } class Bar { immutable (Test)[string] tests2

Associative Array of Const Objects?

2015-03-27 Thread bitwise via Digitalmars-d-learn
class Test{} void main() { const(Test)[string] tests; tests[test] = new Test(); } This code used to work, but after upgrading to dmd 2.067, it no longer does. --Error: cannot modify const expression tests[test] How do I insert an item into an associative array of const