Example of Rust code

Johannes Pfau nospam at example.com
Sun Aug 12 01:17:23 PDT 2012


Am Sun, 12 Aug 2012 00:17:44 +0200
schrieb Timon Gehr <timon.gehr at gmx.ch>:

> On 08/11/2012 01:24 PM, Marco Leise wrote:
> > Am Fri, 10 Aug 2012 15:56:53 +0200
> > schrieb Timon Gehr<timon.gehr at gmx.ch>:
> >
> >> int eval(scope Expr* e){
> >>       final switch(e.tag) with(Expr.Tag){
> >>           case val:   return e.i;
> >>           case plus:  return eval(e.a) + eval(e.b);
> >>           case minus: return eval(e.a) - eval(e.b);
> >>       }
> >> }
> >
> > Can you quickly explain the use of scope here?
> > Does that mean "I wont keep a reference to e"?
> 
> It means "I won't keep a reference to *e", but I assume that is what
> was meant.
> 
> > What are the implications?
> 
> The caller has some confidence that passing a pointer to stack-
> allocated data is safe.
> 
> > Does scope change the method signature?
> 
> Yes. It is eg. impossible to override a method that has a scope
> parameter with a method that does not have a scope parameter.
> 
> > Does the compiler enforce something?
> 
> In this case and currently, it is merely documentation.
> I think it should be enforced and cast(scope) should be added
> to allow non- at safe code to escape the conservative analysis.

There are probably some more, less known use cases. For example this
recent thread on stackoverflow shows how scope might be necessary to
initialize an immutable variable.
http://stackoverflow.com/questions/11860584/changing-immutable-members-inside-the-constructor




More information about the Digitalmars-d mailing list