Stepping back and looking at constness from another angle.

Leandro Lucarella llucax at gmail.com
Tue Jun 5 10:53:28 PDT 2007


Jarrett Billingsley, el  4 de junio a las 23:01 me escribiste:
> "Bill Baxter" <dnewsgroup at billbaxter.com> wrote in message 
> news:f42c51$1ap8$1 at digitalmars.com...
> > Here's a thought I had a while back.  Actually, the #1 practical thing I 
> > want const for is to make it possible to pass a big struct efficiently to 
> > a function and know for certain that the caller's value won't be modified.
> >
> > Taking a cue from the removal of 'virtual' from D, one thing D could do 
> > would be to automatically choose to pass variables by reference based on 
> > performance criteria.  Have the compiler let me write:
> >     Vec4f add(Vec4f rbga1, Vec4f rgba2) { }
> > and not have to worry about whether it would be more efficient to pass a 
> > const ref there.  People pass const refs all over the place in C++ not 
> > because they want reference semantics, but because they want reference 
> > performace.  So let the compiler worry about the optimization, not me. 
> > I'll specify the semantics, and you, Mr. Compiler, figure out how to make 
> > it efficient. That's basically what Walter's done with virtual, which is 
> > great.  One less low-level thing for me to worry about.
> >
> > The crux there is that I really don't care how it gets passed under the 
> > hood as long as it's efficient and behaves as if it were passed by value.
> 
> I really like that idea.  It also implicitly reduces maintenance.  If today, 
> passing a 16-byte struct on the stack is more efficient to pass by 
> reference, or if a struct doesn't fit in a register, but five years from now 
> that's no longer the case, we don't have to go around removing all the refs 
> to improve performance.  The compiler knows what's better.

+1

> > In terms of a real, comprehensive answer to your question (as opposed to 
> > just a trick for struct parameters), I think the two main alternatives you 
> > see are 1) functional programing languages 2) the ostrich approach.
> > In pure functional languages, everything is immutable, so problem solved. 
> > It's the compiler's job to try to make it efficient.  But approach 2), put 
> > your head in the sand and ignore it, is also quite popular.
> 
> Aww, but I like the ostrich approach :)
> 
> I'm actually more interested in the const-by-default idea, which is a lot 
> like functional languages.  I'd be interested to see how it would pan out. 
> It's also a _new idea_ -- nothing like this has ever been done in a 
> non-functional language AFAIK, and we'd be breaking new ground.  Who knows, 
> it could be what we've been looking for.  And if it isn't -- hey, we've got 
> the current const ideas to fall back on.

+1 here too

AFAIK C# also adds a 'ref' keyword in 'call-time', when a parameter is
passed, like:

inplace_sort(ref some_container); // call

This could be some more work to write, but much more clearer to read (you
know explicitly that some_container could be modified). If you omit the
keyword when calling, you have to be take a look at inplace_sort()
definition to see if it can modify some_container. Maybe 'ref' is not
a great keyword, because it could be used for performance too (which is
not the idea). 'mutable' maybe it's a better keyword, but a little long.

I think this could improve auto-documentating code (and prevent bugs).

-- 
LUCA - Leandro Lucarella - Usando Debian GNU/Linux Sid - GNU Generation
------------------------------------------------------------------------
E-Mail / JID:     luca at lugmen.org.ar
GPG Fingerprint:  D9E1 4545 0F4B 7928 E82C  375D 4B02 0FE0 B08B 4FB2 
GPG Key:          gpg --keyserver pks.lugmen.org.ar --recv-keys B08B4FB2
------------------------------------------------------------------------
Americans, let's face it: We've been a spoiled country for a long time.
Do you know what the number one health risk in America is?
Obesity, obesity! They say we're in the middle of an obesity epidemic.
An epidemic, like it's polio. Like we'll be telling our grand kids about
it one day: The Great Obesity Epidemic of 2004.
"How'd you get through it grandpa?"
"Oh, it was horrible Johnny, but there was cheesecake
and pork chops everywhere." 



More information about the Digitalmars-d mailing list