digitalmars.D - readonly storage class
- Benjamin Thaut (20/20) Apr 08 2012 While typing D code I usually come across the problem that neither const...
- Timon Gehr (3/20) Apr 08 2012 I don't get the problem. Can you demonstrate the issue with an example?
- Jonathan M Davis (14/34) Apr 08 2012 I would point out that casting const to mutable and then altering the va...
- Benjamin Thaut (7/41) Apr 08 2012 I'll come up with a example.
- Jonathan M Davis (3/6) Apr 08 2012 It _is_ implictly convertible to const.
- Simen Kjaeraas (3/56) Apr 08 2012 It is.
- Benjamin Thaut (6/63) Apr 08 2012 Thanks, then this is a misunderstanding on my side, and this topic is
- Timon Gehr (2/12) Apr 08 2012 That works.
- H. S. Teoh (12/14) Apr 08 2012 [...]
- Piotr Szturmaj (9/17) Apr 08 2012 Const is really a "readonly" view of data. I think that immutable should...
- Piotr Szturmaj (2/3) Apr 08 2012 ^ doesn't
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
Apr 08 2012
On 04/08/2012 11:16 AM, Benjamin Thaut wrote: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 mutableIt cannot.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.I don't get the problem. Can you demonstrate the issue with an example?
Apr 08 2012
On Sunday, April 08, 2012 11:16:40 Benjamin Thaut wrote: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.I would point out that casting const to mutable and then altering the variable is subverting the type system. The compiler does not support casting away either const or immutable to alter _anything_. So, as far as the type system is concerned, if you want a function that takes both const and immutable, it should take const. Now, you _can_ cast away const and alter a variable if you're careful, but you're subverting the type system when you do so and throwing away any guarantees that the compiler gives you. It's far from safe. Given that casting away const on a variable and then mutating is subverting the type system and thate therefore the compiler is free to assume that you will never do it, I don't see what your idea of readonly would buy us. It's the same as const. - Jonathan M Davis
Apr 08 2012
Am 08.04.2012 11:28, schrieb Jonathan M Davis:On Sunday, April 08, 2012 11:16:40 Benjamin Thaut wrote:I'll come up with a example. But if what you say is true, why is immutable not implicitly convertible to const? -- Kind Regards Benjamin ThautWhile 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.I would point out that casting const to mutable and then altering the variable is subverting the type system. The compiler does not support casting away either const or immutable to alter _anything_. So, as far as the type system is concerned, if you want a function that takes both const and immutable, it should take const. Now, you _can_ cast away const and alter a variable if you're careful, but you're subverting the type system when you do so and throwing away any guarantees that the compiler gives you. It's far from safe. Given that casting away const on a variable and then mutating is subverting the type system and thate therefore the compiler is free to assume that you will never do it, I don't see what your idea of readonly would buy us. It's the same as const. - Jonathan M Davis
Apr 08 2012
On Sunday, April 08, 2012 11:39:03 Benjamin Thaut wrote:I'll come up with a example. But if what you say is true, why is immutable not implicitly convertible to const?It _is_ implictly convertible to const. - Jonathan M Davis
Apr 08 2012
On Sun, 08 Apr 2012 11:39:03 +0200, Benjamin Thaut <code benjamin-thaut.de> wrote:Am 08.04.2012 11:28, schrieb Jonathan M Davis:It is.On Sunday, April 08, 2012 11:16:40 Benjamin Thaut wrote:I'll come up with a example. But if what you say is true, why is immutable not implicitly convertible to const?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.I would point out that casting const to mutable and then altering the variable is subverting the type system. The compiler does not support casting away either const or immutable to alter _anything_. So, as far as the type system is concerned, if you want a function that takes both const and immutable, it should take const. Now, you _can_ cast away const and alter a variable if you're careful, but you're subverting the type system when you do so and throwing away any guarantees that the compiler gives you. It's far from safe. Given that casting away const on a variable and then mutating is subverting the type system and thate therefore the compiler is free to assume that you will never do it, I don't see what your idea of readonly would buy us. It's the same as const. - Jonathan M Davis
Apr 08 2012
Am 08.04.2012 11:49, schrieb Simen Kjaeraas:On Sun, 08 Apr 2012 11:39:03 +0200, Benjamin Thaut <code benjamin-thaut.de> wrote:Thanks, then this is a misunderstanding on my side, and this topic is irrelevant. But what about calling const methods on immutable objects? -- Kind Regards Benjamin ThautAm 08.04.2012 11:28, schrieb Jonathan M Davis:It is.On Sunday, April 08, 2012 11:16:40 Benjamin Thaut wrote:I'll come up with a example. But if what you say is true, why is immutable not implicitly convertible to const?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.I would point out that casting const to mutable and then altering the variable is subverting the type system. The compiler does not support casting away either const or immutable to alter _anything_. So, as far as the type system is concerned, if you want a function that takes both const and immutable, it should take const. Now, you _can_ cast away const and alter a variable if you're careful, but you're subverting the type system when you do so and throwing away any guarantees that the compiler gives you. It's far from safe. Given that casting away const on a variable and then mutating is subverting the type system and thate therefore the compiler is free to assume that you will never do it, I don't see what your idea of readonly would buy us. It's the same as const. - Jonathan M Davis
Apr 08 2012
On 04/08/2012 06:09 PM, Benjamin Thaut wrote:Am 08.04.2012 11:49, schrieb Simen Kjaeraas:That works.On Sun, 08 Apr 2012 11:39:03 +0200, Benjamin Thaut <code benjamin-thaut.de> wrote:Thanks, then this is a misunderstanding on my side, and this topic is irrelevant. But what about calling const methods on immutable objects?I'll come up with a example. But if what you say is true, why is immutable not implicitly convertible to const?It is.
Apr 08 2012
On Sun, Apr 08, 2012 at 06:09:48PM +0200, Benjamin Thaut wrote: [...]Thanks, then this is a misunderstanding on my side, and this topic is irrelevant. But what about calling const methods on immutable objects?[...] Basically, the way const/immutable works is: const / \ unqualified immutable Both unqualified and immutable implicitly convert to const, but not the other way around (except in certain special cases, like copying ints). T -- People walk. Computers run.
Apr 08 2012
Benjamin Thaut wrote: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".Const is really a "readonly" view of data. I think that immutable should be named const, and const should be named readonly, so they won't cause confusion. If you need a function that don't change data just mark parameters as const. All mutable, const and immutable types are implicitly convertible to const.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.Yes, this is how const (as "readonly") works. If you think about readonly, use const. The only drawback are the names.
Apr 08 2012
Piotr Szturmaj wrote:If you need a function that don't change data just mark parameters as^ doesn't
Apr 08 2012