Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re^2: const correctness

by fergal (Chaplain)
on May 14, 2008 at 13:56 UTC ( [id://686515]=note: print w/replies, xml ) Need Help??


in reply to Re: const correctness
in thread const correctness

Perl has Readonly and constant which provide the same functionality.

They're not the same thing. const is not about constructing immutable values it's about being able to declare that "in the following code, the contents of this variable should not be modified, if you see me trying to modify it, throw a compile-time error".

It's also about making a promise in your API which the caller knows will be kept because it has been checked at compile-time. Of course you can force the compiler to let you break the promise using a cast but it can't happen as an unanticipated side-effect or typo.

Replies are listed 'Best First'.
Re^3: const correctness
by BrowserUk (Patriarch) on May 14, 2008 at 14:47 UTC

    In compiled languages, its also about giving the compiler clues about optimisations it can safely perform, especially in multi-threaded environments. A const value can be safely shared without locking, or moved into a register and not re-read for every reference. The current big problem is the lack of transitivity of const.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://686515]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (6)
As of 2024-04-19 11:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found