| Thread overview | |||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
March 02, 2009 const?? When and why? This is ugly! | ||||
|---|---|---|---|---|
| ||||
I haven't been following D for over a year .. now I notice there's a const!!
In C++, in my experience, the only time I need const is when I pass temporary objects by reference and the compiler refuses, so I make the parameter const to make the compiler happy.
void my_func( const SomeType& obj ) { ... }
This is annoying because now const propagates like a virus! any function that I call on `obj` must also be changed to accept a `const` reference. any method on obj which doesn't change it must also be marked as `const` in order for it to be callable from inside this function.
This whole stupid process wouldn't even be needed in the first place if C++ had a garbage collector, because then I would always "new" them (as per Java, C#, and D)
SomeType *obj = new SomeType();
but because there's no garbage collector, I have to create the object not as a reference.
SomeType obj();
Again, this is all stupid C++ stuff that shouldn't even be needed in the first place.
However, with all that being said, that's just my opinion, maybe over the time some people found some actually useful uses for const, great, so they can use it if they want.
What really annoys me is the viral nature of const.
Yesterday I was reading this: http://www.digitalmars.com/d/2.0/const3.html
(btw, this page is empty: http://www.digitalmars.com/d/2.0/const.html )
and, to my surprise, I see:
char[] p = "world"; // error, cannot implicitly convert invariant
// to mutable
and all I can think of is: WHAT - THE - HELL??!!!!!!
invariant(char)[] is ugly! might as well be saying std::vector<char> (ok, not the best example of stupidity, but I hope you get my point).
(and, is invariant(char)[] the correct type? or must it be invariant(char)[5]??)
This is not the D that I loved. What's going on?
P.S. look here http://stackoverflow.com/questions/557011/d-programming-language-char-arrays/
I know you can use "auto" and all that, but still .. I don't feel good about this.
| ||||
March 02, 2009 Re: const?? When and why? This is ugly! | ||||
|---|---|---|---|---|
| ||||
Posted in reply to hasen | == Quote from hasen (hasan.aljudy@gmail.com)'s article
> I haven't been following D for over a year .. now I notice there's a const!!
> In C++, in my experience, the only time I need const is when I pass
> temporary objects by reference and the compiler refuses, so I make the
> parameter const to make the compiler happy.
> void my_func( const SomeType& obj ) { ... }
> This is annoying because now const propagates like a virus! any function
> that I call on `obj` must also be changed to accept a `const` reference.
> any method on obj which doesn't change it must also be marked as
> `const` in order for it to be callable from inside this function.
> This whole stupid process wouldn't even be needed in the first place if
> C++ had a garbage collector, because then I would always "new" them (as
> per Java, C#, and D)
> SomeType *obj = new SomeType();
> but because there's no garbage collector, I have to create the object
> not as a reference.
> SomeType obj();
> Again, this is all stupid C++ stuff that shouldn't even be needed in the
> first place.
> However, with all that being said, that's just my opinion, maybe over
> the time some people found some actually useful uses for const, great,
> so they can use it if they want.
> What really annoys me is the viral nature of const.
> Yesterday I was reading this: http://www.digitalmars.com/d/2.0/const3.html
> (btw, this page is empty: http://www.digitalmars.com/d/2.0/const.html )
> and, to my surprise, I see:
> char[] p = "world"; // error, cannot implicitly convert invariant
> // to mutable
> and all I can think of is: WHAT - THE - HELL??!!!!!!
> invariant(char)[] is ugly! might as well be saying std::vector<char>
> (ok, not the best example of stupidity, but I hope you get my point).
> (and, is invariant(char)[] the correct type? or must it be
> invariant(char)[5]??)
> This is not the D that I loved. What's going on?
> P.S. look here
> http://stackoverflow.com/questions/557011/d-programming-language-char-arrays/
> I know you can use "auto" and all that, but still .. I don't feel good
> about this.
Uhh, that's what aliases are for. The string alias is defined automatically in object, and string is an alias for immutable(char)[].
Also, modulo a few library functions that use strings/immutable(char)[]s, when they should be using const(char)[]s, one can avoid const by simply not using it if one doesn't like it or doesn't need it for the program they're writing.
| |||
March 02, 2009 Re: const?? When and why? This is ugly! | ||||
|---|---|---|---|---|
| ||||
Posted in reply to hasen | Hello hasen, > I haven't been following D for over a year .. now I notice there's a > const!! > [...] > > This is annoying because now const propagates like a virus! Yes const is viral (that can't be avoided) > This whole stupid process wouldn't even be needed in the first place > if C++ had a garbage collector, because then I would always "new" them > (as per Java, C#, and D) > > SomeType *obj = new SomeType(); > > but because there's no garbage collector, I have to create the object > not as a reference. > > SomeType obj(); > > Again, this is all stupid C++ stuff that shouldn't even be needed in > the first place. > IIRC const has exactly zero to do with new/reference/value stuff. It is totaly about knowing if somthing will get changed out from under you. This is needed to make unsable large scale libraries. And has a HUGE impact on parallel code. > However, with all that being said, that's just my opinion, maybe over > the time some people found some actually useful uses for const, great, > so they can use it if they want. > [...] > > char[] p = "world"; // error, cannot implicitly convert invariant > // to mutable > and all I can think of is: WHAT - THE - HELL??!!!!!! That's totally correct. On linux under D1.0 (that would work BTW) if you then try to alter p you get an access violation. String literals can't be modified and that what const/invariant says. | |||
March 02, 2009 Re: const?? When and why? This is ugly! | ||||
|---|---|---|---|---|
| ||||
Posted in reply to dsimcha | dsimcha wrote: > == Quote from hasen (hasan.aljudy@gmail.com)'s article >> I haven't been following D for over a year .. now I notice there's a const!! >> In C++, in my experience, the only time I need const is when I pass >> temporary objects by reference and the compiler refuses, so I make the >> parameter const to make the compiler happy. >> void my_func( const SomeType& obj ) { ... } >> This is annoying because now const propagates like a virus! any function >> that I call on `obj` must also be changed to accept a `const` reference. >> any method on obj which doesn't change it must also be marked as >> `const` in order for it to be callable from inside this function. >> This whole stupid process wouldn't even be needed in the first place if >> C++ had a garbage collector, because then I would always "new" them (as >> per Java, C#, and D) >> SomeType *obj = new SomeType(); >> but because there's no garbage collector, I have to create the object >> not as a reference. >> SomeType obj(); >> Again, this is all stupid C++ stuff that shouldn't even be needed in the >> first place. >> However, with all that being said, that's just my opinion, maybe over >> the time some people found some actually useful uses for const, great, >> so they can use it if they want. >> What really annoys me is the viral nature of const. >> Yesterday I was reading this: http://www.digitalmars.com/d/2.0/const3.html >> (btw, this page is empty: http://www.digitalmars.com/d/2.0/const.html ) >> and, to my surprise, I see: >> char[] p = "world"; // error, cannot implicitly convert invariant >> // to mutable >> and all I can think of is: WHAT - THE - HELL??!!!!!! >> invariant(char)[] is ugly! might as well be saying std::vector<char> >> (ok, not the best example of stupidity, but I hope you get my point). >> (and, is invariant(char)[] the correct type? or must it be >> invariant(char)[5]??) >> This is not the D that I loved. What's going on? >> P.S. look here >> http://stackoverflow.com/questions/557011/d-programming-language-char-arrays/ >> I know you can use "auto" and all that, but still .. I don't feel good >> about this. > > Uhh, that's what aliases are for. The string alias is defined automatically in > object, and string is an alias for immutable(char)[]. That Phobos introduces this alias is not really an argument that speaks in favor of the syntax. Also, I don't quite understand yet what constness is good for. While it seems to be a good idea, it seems to be more an annoyance in practice. It doesn't seem to pay off. What is this additional complexity for? And really, what was wrong with not having constness? All I hear is something about threading, but I didn't see anything convincing yet. No real examples, not even a full concept. Only some loose arguments. Come on, the newest compiler releases can do const, now convince me already that it's a good feature! Regarding pure functions: isn't CTFE already enough? > Also, modulo a few library functions that use strings/immutable(char)[]s, when > they should be using const(char)[]s, one can avoid const by simply not using it if > one doesn't like it or doesn't need it for the program they're writing. | |||
March 02, 2009 Re: const?? When and why? This is ugly! | ||||
|---|---|---|---|---|
| ||||
Posted in reply to grauzone | grauzone Wrote: > dsimcha wrote: > > == Quote from hasen (hasan.aljudy@gmail.com)'s article > > Uhh, that's what aliases are for. The string alias is defined automatically in > > object, and string is an alias for immutable(char)[]. > > That Phobos introduces this alias is not really an argument that speaks in favor of the syntax. > > Also, I don't quite understand yet what constness is good for. While it seems to be a good idea, it seems to be more an annoyance in practice. It doesn't seem to pay off. What is this additional complexity for? And really, what was wrong with not having constness? All I hear is something about threading, but I didn't see anything convincing yet. No real examples, not even a full concept. Only some loose arguments. Come on, the newest compiler releases can do const, now convince me already that it's a good feature! Given the tone of your messages, I assume convincing you is impossible. The biggest help I can offer is to tell you that "the D you loved" still exists as D 1.0... Walter still maintains the compiler for it and Tango, Descent, GDC, LDC, and many other things all support D1. > Regarding pure functions: isn't CTFE already enough? The big driver of D2 is provably safe multithreading without locks. Pure functions are the embodiment of that, and immutable data is required for that. Const is used to handle either mutable or immutable data. Does that help? | |||
March 02, 2009 Re: const?? When and why? This is ugly! | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Jason House | Jason House wrote: > grauzone Wrote: > >> dsimcha wrote: >>> == Quote from hasen (hasan.aljudy@gmail.com)'s article >>> Uhh, that's what aliases are for. The string alias is defined automatically in >>> object, and string is an alias for immutable(char)[]. >> That Phobos introduces this alias is not really an argument that speaks in favor of the syntax. >> >> Also, I don't quite understand yet what constness is good for. While it seems to be a good idea, it seems to be more an annoyance in practice. It doesn't seem to pay off. What is this additional complexity for? And really, what was wrong with not having constness? All I hear is something about threading, but I didn't see anything convincing yet. No real examples, not even a full concept. Only some loose arguments. Come on, the newest compiler releases can do const, now convince me already that it's a good feature! > > Given the tone of your messages, I assume convincing you is impossible. The biggest help I can offer is to tell you that "the D you loved" still exists as D 1.0... Walter still maintains the compiler for it and Tango, Descent, GDC, LDC, and many other things all support D1. Forgive me, I always get the tone wrong. As for the "D you loved", please don't assume things. I think D2.0 is a huge improvement over D1.0. But of course, there are always some issues. Nothing is perfect, after all. And actually, I'm not opposed to these new (actually old) ideas. I just want more information. And yes, I don't like the current const mechanism, because I think it introduces complexity for no gain. That's why I'd like to know more in the first place. And I think I'm not the only one who would prefer to see ast macros to be implemented rather than const. >> Regarding pure functions: isn't CTFE already enough? > > The big driver of D2 is provably safe multithreading without locks. Pure functions are the embodiment of that, and immutable data is required for that. Const is used to handle either mutable or immutable data. You probably mean that data shared between threads has to be immutable. Then all threads can access that data without synchronization. Immutability will sure be useful here: the type system guarantees, that data can never be modified. But how does this work for complex types like objects? Is there an idup for objects? Does it do a deep copy of an object graph? How is communication between threads supposed to work? Shouldn't all global variables be immutable then? Please, where can I find complete, thought-through concepts? Does this approach fit D at all? D is a C based system programming language. Does it work to demand from everyone to obey the const rules? Couldn't all this be done manually, instead of bothering the programmer with a complex type system? Sometimes, you have to leave the more spiffy features to languages like Erlang or Haskell which are suited better for this. For example, Erlang doesn't share any state by default, and in Haskell, everything is immutable. But they use very special mechanisms to make up for the problems caused by this, and that's why you can't just pick the cherries: it won't fit, it will feel unnatural, and everything will be a mess. It's a good idea to copy good features from other languages, but never forget where you come from. Regarding pureness: I don't think it has anything do to with locking. A function could just take const arguments, and everything is safe. It can't write to shared data either, because shared data is supposed to be immutable, right? In this context, it doesn't matter if the function writes to anything else, because that data will be thread local. This rather seems to have to do with optimization by caching results, and with automatic parallelization. Well then, I want to see how this can be applied in D! Again, forgive me my tone. I mean, it's not like I'm threatening anyone or that I want to start flamewars, I just want to know more about this. > Does that help? Not really. I just wasted time by writing this post, and probably the time of people who read this post. It's a shame. | |||
March 02, 2009 Re: const?? When and why? This is ugly! | ||||
|---|---|---|---|---|
| ||||
Posted in reply to hasen | On Mon, 02 Mar 2009 06:28:24 +0300, hasen <hasan.aljudy@gmail.com> wrote:
> I haven't been following D for over a year .. now I notice there's a const!!
>
> In C++, in my experience, the only time I need const is when I pass temporary objects by reference and the compiler refuses, so I make the parameter const to make the compiler happy.
>
> void my_func( const SomeType& obj ) { ... }
>
> This is annoying because now const propagates like a virus! any function that I call on `obj` must also be changed to accept a `const` reference. any method on obj which doesn't change it must also be marked as `const` in order for it to be callable from inside this function.
>
> This whole stupid process wouldn't even be needed in the first place if C++ had a garbage collector, because then I would always "new" them (as per Java, C#, and D)
>
> SomeType *obj = new SomeType();
>
> but because there's no garbage collector, I have to create the object not as a reference.
>
> SomeType obj();
>
> Again, this is all stupid C++ stuff that shouldn't even be needed in the first place.
>
> However, with all that being said, that's just my opinion, maybe over the time some people found some actually useful uses for const, great, so they can use it if they want.
>
> What really annoys me is the viral nature of const.
>
> Yesterday I was reading this: http://www.digitalmars.com/d/2.0/const3.html
>
> (btw, this page is empty: http://www.digitalmars.com/d/2.0/const.html )
>
> and, to my surprise, I see:
>
> char[] p = "world"; // error, cannot implicitly convert invariant
> // to mutable
>
>
> and all I can think of is: WHAT - THE - HELL??!!!!!!
>
> invariant(char)[] is ugly! might as well be saying std::vector<char> (ok, not the best example of stupidity, but I hope you get my point).
> (and, is invariant(char)[] the correct type? or must it be invariant(char)[5]??)
>
> This is not the D that I loved. What's going on?
>
> P.S. look here http://stackoverflow.com/questions/557011/d-programming-language-char-arrays/
>
> I know you can use "auto" and all that, but still .. I don't feel good about this.
>
I you don't understand something, it doesn't mean something is wrong with the language. Perhaps, something wrong with you.
Regarding this exaple:
char[] p = "world";
It is rejected for a reason. Modifying the "world" through p will cause a segfault anyway. So it *must* be const. In case it won't, it will cause bugs that are even worse to track:
// file 1
char[] a = "hello";
a[4] = '!';
// file 2
writefln("hello"); // prints 'hell!'
| |||
March 02, 2009 Re: const?? When and why? This is ugly! | ||||
|---|---|---|---|---|
| ||||
Posted in reply to grauzone | grauzone wrote: > You probably mean that data shared between threads has to be immutable. Then all threads can access that data without synchronization. Immutability will sure be useful here: the type system guarantees, that data can never be modified. But how does this work for complex types like objects? Is there an idup for objects? Does it do a deep copy of an object graph? How is communication between threads supposed to work? Shouldn't all global variables be immutable then? Please, where can I find complete, thought-through concepts? You cannot find them yet because the thinking-through process is still ongoing. > Does this approach fit D at all? D is a C based system programming language. Does it work to demand from everyone to obey the const rules? Couldn't all this be done manually, instead of bothering the programmer with a complex type system? C is still having trouble removing those pesky buffer overruns. Writing a large manycore application in C will be anything but casual programming. > Sometimes, you have to leave the more spiffy features to languages like Erlang or Haskell which are suited better for this. For example, Erlang doesn't share any state by default, and in Haskell, everything is immutable. But they use very special mechanisms to make up for the problems caused by this, and that's why you can't just pick the cherries: it won't fit, it will feel unnatural, and everything will be a mess. It's a good idea to copy good features from other languages, but never forget where you come from. We plan to add the necessary mechanisms. > Regarding pureness: I don't think it has anything do to with locking. A function could just take const arguments, and everything is safe. It can't write to shared data either, because shared data is supposed to be immutable, right? In this context, it doesn't matter if the function writes to anything else, because that data will be thread local. Not all shared data is immutable. Data marked as "shared" is shared. The cool thing is that D switches the default to the "right" default. The style of programming in which you create a new thread that joyously sees and can modify *all* of the memory space available to every other thread will go the way of the dinosaur. It's just not tenable anymore, and I predict today's mainstream programming languages abiding to that model are facing major troubles in the near future. Andrei | |||
March 02, 2009 Re: const?? When and why? This is ugly! | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Andrei Alexandrescu | On Mon, Mar 2, 2009 at 12:25 PM, Andrei Alexandrescu <SeeWebsiteForEmail@erdani.org> wrote: > The style of programming in which you create a new thread that joyously sees and can modify *all* of the memory space available to every other thread will go the way of the dinosaur. It's just not tenable anymore, and I predict today's mainstream programming languages abiding to that model are facing major troubles in the near future. Eheh, sounds like the dawn of protected memory 20 years ago ;) | |||
March 02, 2009 Re: const?? When and why? This is ugly! | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Jason House | Jason House wrote:
> Given the tone of your messages, I assume convincing you is impossible. The biggest help I can offer is to tell you that "the D you loved" still exists as D 1.0... Walter still maintains the compiler for it and Tango, Descent, GDC, LDC, and many other things all support D1.
>
Just a quick note, grauzone is not the OP, it's me :)
| |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply