const or immutable?
Ali Çehreli
acehreli at yahoo.com
Wed Sep 22 20:26:18 UTC 2021
On 9/22/21 1:17 PM, Adam D Ruppe wrote:
> On Wednesday, 22 September 2021 at 20:06:59 UTC, Ali Çehreli wrote:
>> tl;dr Do you use 'const' or 'immutable' by-default for parameters and
>> for local data?
>
> well i often ignore it but `const` (or often better yet, `inout` if
> there's any relation to a member of yours or if you return it)
'inout' has been useful in cases like you mention. For example, a
slice's elements should reflect the mutability of 'this'. I am mostly
interested in revising a beginner-oriented chapter, where 'inout'
doesn't exist yet.
> is better
> on function arguments, and `immutable` is better on internal members.
>
> The exception is when you are storing something from someone else, where
> immutable params might be more important.
I noticed that as well: For example, if a file name constructor
parameter is string, I don't need to copy the file name to a member.
(This actually appears later in the book where I realize constructor
arguments might better be 'immutable'.)
> But if you are looking and not
> touching, const or `in` is good.
'in' has been another question: It appears a lot in my book but I almost
never use it myself. Perhaps that's the answer? I would like that.
However, the proposed change of meaning of 'in' is also worrying but I
think it will strenghthen the case for it's use I guess.
>> Aside: If 'const' is welcoming, why do we type 'string' for string
>> parameters when we don't actually *require* immutable:
>
> Unfortunate naming here... the convenient name encourages its use but it
> is indeed often suboptimal.
I agree. However, ironically and worryingly :), it just works most of
the time.
Ali
More information about the Digitalmars-d
mailing list