While typing D code I usually come across the problem that neither const nor immutable describe the usage pattern of the memory I'm currently working on 100%. Sometimes I have immutable data that has been shared among threads that I want to pass to a function. Then I have some const data that I want to pass to the same function. Currently you don't have any other choice but to write that function two times. But the function itself does not need the "extended" properties of const or immutable:

const: can be casted back to mutable
immutable: can be implicitly shared among threads

The only thing the function cares about is, that it will not change the data passed to it. It would be kind of nice to have a thrid storage class "readonly". It can not be casted back to mutable and it can not be implicitly shared among threads, but both const and immutable implicitly convert to readonly, because both of these storage classes lose one of their properties during conversion. That way you only have to write the function once and can pass both const and immutable data to it.

Just an idea, comments and critics welcome.

--
Kind Regards
Benjamin Thaut

Reply via email to