www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Const unbackled

reply Georg Wrede <georg nospam.org> writes:
I might have a function to test some property of a value. I expect to 
usually need this function inside some other functions.

Since I know this function does not change its input data, I now want to 
_advertise_ this fact, according to the prevailing /mode du jour/.

Let's say my function validates a string or a stream of UTF for internal 
correctness. In stand-alone usage it would look like

     vutf(s);  // validate string or stream s, and throw if bad
     S sf = translateToFinnish(removeWhitespace(s));

But usually I'd use it inside something else, like

     S sf = translateToFinnish(removeWhitespace(vutf(s)));

In the first case I discarded the output of vutf but want to be sure 
that s stays the same, in the second case I used it as input to the 
enclosing function. Since vutf does not modify its input, I should 
somehow be able to advertise this fact.

I admit this example is contrived, but you get the idea.


We should stop discussing whether/how this is implementable 
syntactically/semantically, and instead start discussing USE CASES, and 
DO WE NEED THIS IN THE FIRST PLACE.

(One might also ponder on whether making deeply nested function calls in 
assignments is desirable in the first place.)

UNLESS we think this out BEFORE ending up on a consensus on 
syntax/semantics, we might end up with what the C++ folks did with 
multiple inheritance. (They were too busy figuring out how to implement 
it, instead of whether there's any real-world use for it or whether it 
is implementable at all in a practical and usable way.)

Use cases mentioned in the relevant NG discussions are checking for 
properties, returning slices, what else? Where is this indispensable?
Mar 24 2008
parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
"Georg Wrede" wrote
 We should stop discussing whether/how this is implementable 
 syntactically/semantically, and instead start discussing USE CASES, and DO 
 WE NEED THIS IN THE FIRST PLACE.
For starters, let's look at some functions in std.string: invariant(char)[][] split(string s); invariant(char)[][] split(string s, string delim); invariant(char)[][] splitlines(string s); The use case could be said that one might want to pass a mutable string to these functions, and have it return mutable slices into the argument WITHOUT modifying the argument. string stripl(string s); string stripr(string s); string strip(string s); string chop(string s); These could all be usable on mutable strings, with the result being a slice of the original, and hopefully mutable. You should be able to specify that the function does not modify the argument. And in tango.text.Util: trim (source) // trim whitespace triml (source) // trim whitespace trimr (source) // trim whitespace strip (source, match) // trim elements stripl (source, match) // trim elements stripr (source, match) // trim elements chopl (source, match) // trim pattern match chopr (source, match) // trim pattern match delimit (src, set) // split on delims split (source, pattern) // split on pattern splitLines (source); // split on lines head (source, pattern, tail) // split to head & tail These all could be specified that source is not modified. -Steve
Mar 24 2008