www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - const/invariant/pure/mutable living happily together

reply "Steven Schveighoffer" <schveiguy yahoo.com> writes:
In the "invariant const sucks" thread, there has been a lot of activity, and 
so I wanted to post my ideas on this outside the thread so it doesn't get 
lost in the noise.

Basically, I think const/invariant/pure/mutable can all be used at the same 
time and everyone can get what they want.

BEGIN DEFINITION

const and invariant work as Walter described in his original post "const 
sucks", with the added feature that a class method can be declared invariant 
if it does not modify any members of a class except members declared as 
mutable.

mutable is a new keyword that can be used only on a class member.  A class 
member defined as mutable can be changed by a const function.

Now, I have 2 different possible definitions of pure.  The first is if the 
compiler is assuming the responsibility of thread concurrency.  That is, the 
compiler assumes that if it has 2 pure function calls in sequence, it can 
call them simultaneously on parallel processors without worrying that 
another thread or signal will modify the data.

Here is that definition:
A function can be declared pure with the pure keyword.  A pure function 
cannot modify any data that is not scoped in the function.  A pure function 
can only call other pure functions.  A pure function can only access data 
outside its scope that is declared invariant.

The second definition is if the compiler does not assume the responsibility 
of thread concurrency:
A function can be declared pure with the pure keyword.  A pure function 
cannot modify any data that is not scoped in the function.  A pure function 
can only call other pure functions.  A pure function can read any data 
outside its scope that is declared const.

END DEFINITION

Can functional programming not work with at least one of the 2 definitions? 
If not, why?  I have no real experience with functional programming, so I 
may have that stuff wrong.

And PLEASE PLEASE if you want to show that something is wrong with my 
proposal, present a real example.  I have a hard time accepting arguments 
like "that won't work because functional programming requires no mutable 
data."  It's like saying "because I said so" :)

And I totally don't mind if I'm wrong.  I would LOVE to be proven wrong so 
that I can stop thinking about this issue.  I just can't understand why this 
doesn't work.

-Steve 
Sep 12 2007
next sibling parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
"Steven Schveighoffer" wrote
 In the "invariant const sucks" thread,
Should have read "transitive const sucks", sorry -Steve
Sep 12 2007
prev sibling parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
If you're thinking of responding to this thread, don't.  I no longer think 
this is correct.

Look at my 'logical const is a subset of transitive const' thread instead.

-Steve 
Sep 14 2007