immutability - When and how should I use `const` and `immutable` in D? -


in many modern languages const correctness should used clarify interfaces , intent provide opportunities compiler optimize. in d there's cool feature of immutable data. how should const , immutable used? kinda figured, preferring const qualifiers functions arguments , immutable local variables seems way write code, want assign struct reference or pointer data member (private or not) immutable variable can't that.

struct s {     private int[] data; }  void main() {     immutable s = new s; // won't work, since members aren't immutable } 

hence, changing implementation of struct can break code, if use immutable. should prefer const local variables , use immutable when necessary? guidelines?

you extremely lucky - video dconf2013 presentation devoted topic has been published last week : http://youtu.be/mpr2usps0fe

in case, auto s = new immutable(s)(); should trick. can create data slice points in constructor. however, if slice may point any data, s can't possibly immutable, because both const , immutable transitive in d - provide strong guarantees not variable data can accessed indirectly via reference/pointer.

it covered in linked video, short summary want use immutable when intend to. in other words, when want code break if s implementation changes immutable guarantees no longer valid.


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -