digitalmars.D - The motivation for const (the big picture)
- guslay (12/12) Jan 05 2008 I would appreciate if Walter or maybe someone who assisted to the D conf...
- Jason House (19/28) Jan 06 2008 I don't think I match your criteria for someone you want to hear from, b...
- Bill Baxter (16/19) Jan 06 2008 I think it only helps in the sense that it makes automatic
- Dan (13/24) Jan 06 2008 [snip]
- Bill Baxter (5/29) Jan 06 2008 So you're saying the likely usage patterns will involve giving the
- Robert Fraser (2/38) Jan 07 2008 I think he's trying to say that it's possible to cast away invariant.
- Walter Bright (3/4) Jan 06 2008 This is clearly a frequently asked question, so I added it to the FAQ:
- Bill Baxter (6/13) Jan 07 2008 Some of those FAQs need updating:
- Walter Bright (2/9) Jan 07 2008 Done.
- Bill Baxter (8/18) Jan 07 2008 One more thing:
- BC (5/12) Jan 08 2008 can I ask, which compiler combinations does interfacing work with? is it...
- Frits van Bommel (7/17) Jan 10 2008 DMD on Windows is compatible with DMC.
- Lionello Lunesu (3/10) Jan 07 2008 Small thing: "of little import" should probably read "of little importan...
- Bill Baxter (3/14) Jan 07 2008 It's valid English. The meaning is the same.
- Jason House (4/20) Jan 07 2008 Not all native english speakers recognize that as valid english (take me...
- Bill Baxter (4/23) Jan 07 2008 Yeh true. Effective communication is all about considering your target
- Bruce Adams (9/33) Jan 07 2008 I'd hate to be stuck with a less rich vocabularly to suit the lowest
- Daniel Lewis (8/27) Jan 07 2008 When we program, we are making the assumption that data doesn't arbitrar...
I would appreciate if Walter or maybe someone who assisted to the D conference could enlight me. Taking a step back from the syntax/expressiveness debate, I would like to understand better where do const/invariant reside in the grand scheme of things. Is the current const/invariant the end goal, or does it lead to something else? Why is const/invariant necessary? - Is it just a checklist feature? [I think it's more than that, but C++ users are accustomed to this, it might be an handicap not to have it] - Is it mainly there because it's required to implement something else? [Say, immutable string] - Or is it there because it is useful just by itself? In that case, why? What's the *primary* intent? - To protect programmers from making mistakes? - For documentation? - As a "contract" in functions interface? - To enable certain optimizations? And mainly, how does it stand with respect to pure function and functional programming? Is it required from that or is it more of a complementary feature? Lots of questions....
Jan 05 2008
guslay wrote:I would appreciate if Walter or maybe someone who assisted to the D conference could enlight me. Taking a step back from the syntax/expressiveness debate, I would like to understand better where do const/invariant reside in the grand scheme of things. Is the current const/invariant the end goal, or does it lead to something else? Why is const/invariant necessary?I don't think I match your criteria for someone you want to hear from, but I'll try anyway ;) I view const as part of an API specification. Essentially a contract promising to not modify the data that is passed into a function (or held by a class, etc...). Conceptually, this is useful (like in, out, ref parameters in functions) because it helps understand the intended functionality and gives a mechanism for the compiler to catch accidental violations of the intent. Unfortunately, const (AKA readonly) does not do anything for the compiler besides giving it extra work to check code. There's no guarantee that data won't be changed when calling functions or through actions of another thread. Because of this, the compiler can not optimize code under the assumption that the data never changes. As I understand it, this is the purpose of invariant. I've also heard (but never seen justification/explanation) that invariant helps with functional programming. I share a lot of your questions, and I hope someone who knows more than me can jump in and explain this stuff better.
Jan 06 2008
Jason House wrote:I've also heard (but never seen justification/explanation) that invariant helps with functional programming.I think it only helps in the sense that it makes automatic parallelization of functional-style programs possible. If I foreach over some data structure and all the inputs are invariant, then the compiler is free to do that in any order or in parallel because it knows that executing a function on one element cannot possibly change the value of another element by any means. With a C++ readonly style const you only know that you're not changing it, but there are a dozen ways that it could be changed out from under you in sneaky and unexpected, but perfectly legal, ways. That said, I'm not sure what that's going to look like in D. It seems like making use of invariant for functional programming in D will require a totally different programming style where you make everything invariant all the time. It doesn't seem like you'll be able to just "toss in" a little FP to an existing project without a lot of redesign. --bb
Jan 06 2008
Bill Baxter Wrote:Jason House wrote:[snip] Invariant allows for alot of assumptions to be made about something; but it's because we assume merrily that it's okay for the program to crash if something changes out from under us even if the program is paused. So we can: - fearlessly run parallel functions over the same data - cache something and expect it to be correct indefinitely - trust our libraries, and 3rd parties not to mangle our stuff - catch accidental writes to something we wanted read-only - make guarantees to clients and employers (our code *cannot* change your x) - be aware the opposite is probably true for non-invariants In all honesty though, we must know it's a bluff. The code *can* change out from under you. It just won't by your own hand. Regards, DanI've also heard (but never seen justification/explanation) that invariant helps with functional programming.I think it only helps in the sense that it makes automatic parallelization of functional-style programs possible. If I foreach over some data structure and all the inputs are invariant, then the compiler is free to do that in any order or in parallel because it knows that executing a function on one element cannot possibly change the value of another element by any means.
Jan 06 2008
Dan wrote:Bill Baxter Wrote:So you're saying the likely usage patterns will involve giving the compiler "hints" in the form of casts to invariant on data that actually isn't? --bbJason House wrote:[snip] Invariant allows for alot of assumptions to be made about something; but it's because we assume merrily that it's okay for the program to crash if something changes out from under us even if the program is paused. So we can: - fearlessly run parallel functions over the same data - cache something and expect it to be correct indefinitely - trust our libraries, and 3rd parties not to mangle our stuff - catch accidental writes to something we wanted read-only - make guarantees to clients and employers (our code *cannot* change your x) - be aware the opposite is probably true for non-invariants In all honesty though, we must know it's a bluff. The code *can* change out from under you. It just won't by your own hand.I've also heard (but never seen justification/explanation) that invariant helps with functional programming.I think it only helps in the sense that it makes automatic parallelization of functional-style programs possible. If I foreach over some data structure and all the inputs are invariant, then the compiler is free to do that in any order or in parallel because it knows that executing a function on one element cannot possibly change the value of another element by any means.
Jan 06 2008
Bill Baxter wrote:Dan wrote:I think he's trying to say that it's possible to cast away invariant.Bill Baxter Wrote:So you're saying the likely usage patterns will involve giving the compiler "hints" in the form of casts to invariant on data that actually isn't? --bbJason House wrote:[snip] Invariant allows for alot of assumptions to be made about something; but it's because we assume merrily that it's okay for the program to crash if something changes out from under us even if the program is paused. So we can: - fearlessly run parallel functions over the same data - cache something and expect it to be correct indefinitely - trust our libraries, and 3rd parties not to mangle our stuff - catch accidental writes to something we wanted read-only - make guarantees to clients and employers (our code *cannot* change your x) - be aware the opposite is probably true for non-invariants In all honesty though, we must know it's a bluff. The code *can* change out from under you. It just won't by your own hand.I've also heard (but never seen justification/explanation) that invariant helps with functional programming.I think it only helps in the sense that it makes automatic parallelization of functional-style programs possible. If I foreach over some data structure and all the inputs are invariant, then the compiler is free to do that in any order or in parallel because it knows that executing a function on one element cannot possibly change the value of another element by any means.
Jan 07 2008
guslay wrote:I would appreciate if Walter or maybe someone who assisted to the D conference could enlight me.This is clearly a frequently asked question, so I added it to the FAQ: http://www.digitalmars.com/d/faq.html#const
Jan 06 2008
Walter Bright wrote:guslay wrote:Some of those FAQs need updating: * Why doesn't D have an interface to C++ as well as C? * Why cannot D code directly call existing C++ code? Both should mention D2.0's C++ interfacing capabilities. (with link to here http://www.digitalmars.com/d/cpp_interface.html)I would appreciate if Walter or maybe someone who assisted to the D conference could enlight me.This is clearly a frequently asked question, so I added it to the FAQ: http://www.digitalmars.com/d/faq.html#const
Jan 07 2008
Bill Baxter wrote:Some of those FAQs need updating: * Why doesn't D have an interface to C++ as well as C? * Why cannot D code directly call existing C++ code? Both should mention D2.0's C++ interfacing capabilities. (with link to here http://www.digitalmars.com/d/cpp_interface.html)Done.
Jan 07 2008
Walter Bright wrote:Bill Baxter wrote:One more thing: "Doesn't C++ support strings, bit arrays, etc. with STL?"..."How much confidence can you have that this [std::string] is all working correctly, how do you fix it if it is not?" The "how do you fix it?" argument applies even moreso to a built-in implementation of string than a library one. So I'd strike that bit. --bbSome of those FAQs need updating: * Why doesn't D have an interface to C++ as well as C? * Why cannot D code directly call existing C++ code? Both should mention D2.0's C++ interfacing capabilities. (with link to here http://www.digitalmars.com/d/cpp_interface.html)Done.
Jan 07 2008
On Mon, 07 Jan 2008 20:38:51 -0000, Walter Bright <newshound1 digitalmars.com> wrote:Bill Baxter wrote:can I ask, which compiler combinations does interfacing work with? is it just when the executable formats are the same?Some of those FAQs need updating: * Why doesn't D have an interface to C++ as well as C? * Why cannot D code directly call existing C++ code? Both should mention D2.0's C++ interfacing capabilities. (with link to here http://www.digitalmars.com/d/cpp_interface.html)Done.
Jan 08 2008
BC wrote:DMD on Windows is compatible with DMC. DMD on Linux is compatible with some version of g++ (not sure which one). GDC should be compatible with g++ from the same version. The ones that are compatible with some version of g++ should also be compatible with "similar" g++ versions and IIRC the Intel C++ compiler on Linux (perhaps depending on version of g++ and icc).Bill Baxter wrote:can I ask, which compiler combinations does interfacing work with? is it just when the executable formats are the same?Some of those FAQs need updating: * Why doesn't D have an interface to C++ as well as C? * Why cannot D code directly call existing C++ code? Both should mention D2.0's C++ interfacing capabilities. (with link to here http://www.digitalmars.com/d/cpp_interface.html)
Jan 10 2008
Walter Bright wrote:guslay wrote:Small thing: "of little import" should probably read "of little importance"? L.I would appreciate if Walter or maybe someone who assisted to the D conference could enlight me.This is clearly a frequently asked question, so I added it to the FAQ: http://www.digitalmars.com/d/faq.html#const
Jan 07 2008
Lionello Lunesu wrote:Walter Bright wrote:It's valid English. The meaning is the same. --bbguslay wrote:Small thing: "of little import" should probably read "of little importance"?I would appreciate if Walter or maybe someone who assisted to the D conference could enlight me.This is clearly a frequently asked question, so I added it to the FAQ: http://www.digitalmars.com/d/faq.html#const
Jan 07 2008
Bill Baxter wrote:Lionello Lunesu wrote:Not all native english speakers recognize that as valid english (take me for example). It's probably better to use the more common wording to avoid people thinking it's poorly worded.Walter Bright wrote:It's valid English. The meaning is the same. --bbguslay wrote:Small thing: "of little import" should probably read "of little importance"?I would appreciate if Walter or maybe someone who assisted to the D conference could enlight me.This is clearly a frequently asked question, so I added it to the FAQ: http://www.digitalmars.com/d/faq.html#const
Jan 07 2008
Jason House wrote:Bill Baxter wrote:Yeh true. Effective communication is all about considering your target audience. --bbLionello Lunesu wrote:Not all native english speakers recognize that as valid english (take me for example). It's probably better to use the more common wording to avoid people thinking it's poorly worded.Walter Bright wrote:It's valid English. The meaning is the same. --bbguslay wrote:Small thing: "of little import" should probably read "of little importance"?I would appreciate if Walter or maybe someone who assisted to the D conference could enlight me.This is clearly a frequently asked question, so I added it to the FAQ: http://www.digitalmars.com/d/faq.html#const
Jan 07 2008
On Tue, 08 Jan 2008 03:51:28 -0000, Bill Baxter <dnewsgroup billbaxter.com> wrote:Jason House wrote:I'd hate to be stuck with a less rich vocabularly to suit the lowest common denominator. (me no like less words to make simple) Wikipedia has an interesting approach. There's a simplified form of English as a distinct language/translation http://en.wikipedia.org/wiki/Wikipedia:Simple_English_Wikipedia http://en.wikipedia.org/wiki/Simplified_EnglishBill Baxter wrote:Yeh true. Effective communication is all about considering your target audience. --bbLionello Lunesu wrote:Not all native english speakers recognize that as valid english (take me for example). It's probably better to use the more common wording to avoid people thinking it's poorly worded.Walter Bright wrote:It's valid English. The meaning is the same. --bbguslay wrote:Small thing: "of little import" should probably read "of little importance"?I would appreciate if Walter or maybe someone who assisted to the D conference could enlight me.This is clearly a frequently asked question, so I added it to the FAQ: http://www.digitalmars.com/d/faq.html#const
Jan 07 2008
Robert Fraser Wrote:*someone else said*:Dan wrote:So we can: - fearlessly run parallel functions over the same data - cache something and expect it to be correct indefinitely - trust our libraries, and 3rd parties not to mangle our stuff - catch accidental writes to something we wanted read-only - make guarantees to clients and employers (our code *cannot* change your x) - be aware the opposite is probably true for non-invariants In all honesty though, we must know it's a bluff. The code *can* change out from under you. It just won't by your own hand.When we program, we are making the assumption that data doesn't arbitrarily change between steps in our algorithm "deux est machina". If it does, most often the algorithm will fail to perform the desired function. What "invariant" does, is to define this assumption, and clarify which parts we're going to decide we'll accept changing out from under us, and which parts aren't allowed to. The parts that aren't allowed to, are asserted by the compiler; but that by no means makes the program's outcome certain. Just because your own code cannot tamper with it, doesn't mean that it can't be tampered with in a VM, by a debugger, or whatnot; and so the assumption is known to be false for at least some case. Hence we declare the results "undefined" and be done with it. This is honesty. Now, because we declare awareness of this assumption, we are able to cause the program to make some optimizations and restrictions which may do the things I stated in the above list. Hope that clarifies my view on it.So you're saying the likely usage patterns will involve giving the compiler "hints" in the form of casts to invariant on data that actually isn't? --bbI think he's trying to say that it's possible to cast away invariant.
Jan 07 2008