Why D const is annoying

Chris Cain clcain at uncg.edu
Tue May 1 21:18:16 PDT 2012


On Wednesday, 2 May 2012 at 04:05:11 UTC, Mehrdad wrote:
> Okay, so let's say you're right
>
>    void process1(Student s) { .. }
>    void process2(const(Student) s) { ... }
>
> and that what I  guess what I REALLY want is to say, 
> "process1() won't change the student's name, birthday, or any 
> other attribute, but process2() will".
>
> How do you propose I make that guarantee known to the 
> compiler/caller?

What about the object do you want const? How should this object 
normally change things? It looks like the way you've coded it, 
your connection will get information off of a server. Does 
const(Student) mean that it shouldn't write to the server? If so, 
use something akin to a compile-time dependency injection ... 
specify that one of those students has a read-only connection. 
Something like Student!StudentDBConnection might work, and then 
maybe Student!const(StudentDBConnection).

> Is that not the entire point of saying "const(Student)" in the 
> first place?
"const(Student)" means I won't change _anything_ about the 
object. It's const. So in a sense, you can say that you won't 
change the fields name, birthday, etc. That means you can't 
change the fields.


More information about the Digitalmars-d mailing list