Example of Rust code

Timon Gehr timon.gehr at gmx.ch
Sat Aug 11 15:17:44 PDT 2012


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.

> Will generated code differ?

Only the mangled symbol name will differ. (unlike when scope is used on
delegate parameters, in this case it prevents closure allocation at the
call site.)

> Does it prevent bugs or is it documentation for the user of the function?

It is just documentation, both for the user and the maintainer.

> Thanks in advance for some insight!
>


More information about the Digitalmars-d mailing list