www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - The COW Manifesto

reply AJG <AJG_member pathlink.com> writes:
Hi,

I have a suggestion regarding this whole COW vs. const/readonly/immutable
dilemma. How about a COW modifier for functions? A declaration of such a
function could look like this:

# cow void func(int[] arr);   // or
# void cow func(int[] arr);   // or
# [cow] void func(int[] arr); // or
# void func(int[] arr) cow;   // or

Now, the idea is to let the compiler automatically do the duping/no-duping
chicken dance for you. Some guidelines would be:
- cow is viral. A cow function can't call a non-cow one.
** Alternatively, it could be made that calling non-cows automatically dups.
- Non-cow, "unsafe" functions _could_ call cow functions.
** This is optional. Mixing of the two could be prohibited too.
- The compiler determines whether a param should be made a copy or not.

What are the advantages?
- Minimal extra syntax. It's 3 letters, and only per-function; not per-param.
- It could even be made the default, and then 'nocow' or could be introduced.
- In terms of optimization, it lets the compiler choose what's best.

What are the disadvantages?
- It is _not_ a substitute for readonly. Even though the compiler makes sure
that copies are made on modification, you could hide a bug like this.

Some examples:

cow void tolower(string str) {
foreach(inout char c; str)
if ((c >= 'A' && c <= 'Z')
c += ('a' - 'A'); // This triggers a dup. 

foo(str); // This is illegal [OR, could be made to trigger a dup]. 
}

void foo(string str) {
tolower(str); // This is fine: Non-cow to Cow.
}

What do you think?

Cheers,
--AJG.
Jul 16 2005
parent reply "Andrew Fedoniouk" <news terrainformatica.com> writes:
"AJG" <AJG_member pathlink.com> wrote in message 
news:dbcg4o$2ko8$1 digitaldaemon.com...
 Hi,

 I have a suggestion regarding this whole COW vs. const/readonly/immutable
 dilemma. How about a COW modifier for functions? A declaration of such a
 function could look like this:

 # cow void func(int[] arr);   // or
 # void cow func(int[] arr);   // or
 # [cow] void func(int[] arr); // or
 # void func(int[] arr) cow;   // or

 Now, the idea is to let the compiler automatically do the duping/no-duping
 chicken dance for you. Some guidelines would be:
 - cow is viral. A cow function can't call a non-cow one.
 ** Alternatively, it could be made that calling non-cows automatically 
 dups.
 - Non-cow, "unsafe" functions _could_ call cow functions.
 ** This is optional. Mixing of the two could be prohibited too.
 - The compiler determines whether a param should be made a copy or not.

 What are the advantages?
 - Minimal extra syntax. It's 3 letters, and only per-function; not 
 per-param.
 - It could even be made the default, and then 'nocow' or could be 
 introduced.
 - In terms of optimization, it lets the compiler choose what's best.

 What are the disadvantages?
 - It is _not_ a substitute for readonly. Even though the compiler makes 
 sure
 that copies are made on modification, you could hide a bug like this.

 Some examples:

 cow void tolower(string str) {
 foreach(inout char c; str)
 if ((c >= 'A' && c <= 'Z')
 c += ('a' - 'A'); // This triggers a dup.

 foo(str); // This is illegal [OR, could be made to trigger a dup].
 }

 void foo(string str) {
 tolower(str); // This is fine: Non-cow to Cow.
 }

 What do you think?

 Cheers,
 --AJG.

Objections about syntax, your honour. cow and COW are different animals. Propose to use 'gag' for that which is abbreviated form of Gentleman's AGreement. Andrew.
Jul 16 2005
parent reply AJG <AJG_member pathlink.com> writes:
Objections about syntax, your honour.
cow and COW are different animals.

Propose to use 'gag' for that which is
abbreviated form of Gentleman's AGreement.

You, Sir, win this thread. ;) --AJG.
Jul 16 2005
parent "Andrew Fedoniouk" <news terrainformatica.com> writes:
"AJG" <AJG_member pathlink.com> wrote in message 
news:dbcu35$2v1v$1 digitaldaemon.com...
Objections about syntax, your honour.
cow and COW are different animals.

Propose to use 'gag' for that which is
abbreviated form of Gentleman's AGreement.

You, Sir, win this thread. ;) --AJG.

:) Hug then, as an action and new proposal for the COW. ------------- Shots I've made on last weekend in Vancouver on Tallships Festival: Russian tallship Pallada in Vancouver Harbour: http://terrainformatica.com/photos/pallada/PICT0104.JPG http://terrainformatica.com/photos/pallada/PICT0071.JPG http://terrainformatica.com/photos/pallada/PICT0061.JPG
Jul 16 2005