December 11, 2011 Re: Fixing const arrays | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jonathan M Davis | On 12/11/11 1:30 AM, Jonathan M Davis wrote:
> On Sunday, December 11, 2011 01:16:28 Andrei Alexandrescu wrote:
>> To truly confer user-defined types the same capability, we should define
>> opPassByValue() which is implicitly invoked whenever an object is passed
>> by value into a function. By default that is a do-nothing operator; for
>> arrays it would do the cast thing (or, equivalently, invoke "[]" on the
>> array), and people could define it to do whatever. We could do all that.
>> The question is, is the added complexity justified?
>
> I think that it's completely justified. We need a way to define tail-constness
> for ranges. Given const's transitiveness, it's very easy to end up in a
> situation where you have a const range, and having a means to get a tail-const
> version of that range would be very valuable. I don't know if opPassByValue is
> the best solution, but if not, we at least need a similar one.
I'm not sure. How many times have you been in a place in life where you had a const range on your hands, that's not an array? I haven't.
Andrei
|
December 11, 2011 Re: Fixing const arrays | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrei Alexandrescu | On 12/11/2011 12:05 AM, Andrei Alexandrescu wrote:
> I'm not sure. How many times have you been in a place in life where you had a const range on your hands, that's not an array? I haven't.
>
> Andrei
Hmmm.. isn't that precisely what happens when you define a 'const' method for your (custom) range type? 'this' now becomes a const range.
|
December 11, 2011 Re: Fixing const arrays | ||||
---|---|---|---|---|
| ||||
Posted in reply to Mehrdad | On 12/11/11 2:08 AM, Mehrdad wrote:
> On 12/11/2011 12:05 AM, Andrei Alexandrescu wrote:
>> I'm not sure. How many times have you been in a place in life where
>> you had a const range on your hands, that's not an array? I haven't.
>>
>> Andrei
>
> Hmmm.. isn't that precisely what happens when you define a 'const'
> method for your (custom) range type? 'this' now becomes a const range.
What do you do inside that method? It's reasonable to not expect to change it.
Andrei
|
December 11, 2011 Re: Fixing const arrays | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrei Alexandrescu | On 12/11/2011 12:14 AM, Andrei Alexandrescu wrote:
> What do you do inside that method? It's reasonable to not expect to change it.
>
> Andrei
Not sure right now, but something along the lines of
auto copy = this;
foreach (v; copy)
....;
sounds like code I've definitely seen before.
|
December 11, 2011 Re: Fixing const arrays | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrei Alexandrescu | Andrei Alexandrescu:
> I'm not sure. How many times have you been in a place in life where you had a const range on your hands, that's not an array? I haven't.
How many times I have used reduce or map on constant arrays in D2? Only few times, using a cast(). Now I am using them.
Bye,
bearophile
|
December 11, 2011 Re: Fixing const arrays | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | On 12/11/11 2:48 AM, bearophile wrote: > Andrei Alexandrescu: > >> I'm not sure. How many times have you been in a place in life >> where you had a const range on your hands, that's not an array? I >> haven't. > > How many times I have used reduce or map on constant arrays in D2? > Only few times, using a cast(). Now I am using them. > > Bye, bearophile Emphasis added: >> I'm not sure. How many times have you been in a place in life where >> you had a const range on your hands, _that's_not_an_array_? I >> haven't. Andrei |
December 11, 2011 Re: Fixing const arrays | ||||
---|---|---|---|---|
| ||||
Posted in reply to Mehrdad | On 12/11/11 2:36 AM, Mehrdad wrote:
> On 12/11/2011 12:14 AM, Andrei Alexandrescu wrote:
>> What do you do inside that method? It's reasonable to not expect to
>> change it.
>>
>> Andrei
>
> Not sure right now, but something along the lines of
>
> auto copy = this;
> foreach (v; copy)
> ....;
>
> sounds like code I've definitely seen before.
I think you should write:
auto copy = this.save;
Andrei
|
December 11, 2011 Re: Fixing const arrays | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrei Alexandrescu | On 12/11/2011 12:57 AM, Andrei Alexandrescu wrote:
> I think you should write:
>
> auto copy = this.save;
>
>
> Andrei
Ah good point, I forgot about that. Idk then.
(You forgot the parentheses though. :P)
|
December 11, 2011 Re: Fixing const arrays | ||||
---|---|---|---|---|
| ||||
Posted in reply to Mehrdad | On 12/11/2011 1:04 AM, Mehrdad wrote: > On 12/11/2011 12:57 AM, Andrei Alexandrescu wrote: >> I think you should write: >> >> auto copy = this.save; >> >> >> Andrei > > Ah good point, I forgot about that. Idk then. > (You forgot the parentheses though. :P) > Try the new beta http://ftp.digitalmars.com/dmd2beta.zip |
December 11, 2011 Re: Fixing const arrays | ||||
---|---|---|---|---|
| ||||
Posted in reply to Mehrdad | On 12/11/11 10:04 AM, Mehrdad wrote:
> On 12/11/2011 12:57 AM, Andrei Alexandrescu wrote:
>> I think you should write:
>>
>> auto copy = this.save;
>
> Ah good point, I forgot about that. Idk then.
> (You forgot the parentheses though. :P)
No, it's correct like that, save() is a @property.
Even though I recently updated Phobos to compile with -property enabled, I agree with Andrei here that in many cases there will be no perfect consensus whether a given member should be a @property or not, thus potentially adding an extra source of complexity, or rather annoyance.
David
|
Copyright © 1999-2021 by the D Language Foundation