Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The difference of 'in' and 'const' in D language

Tags:

constants

d

Reading this tutorial: http://ddili.org/ders/d.en/function_parameters.html

I cannot understand the difference between in and const: what is it?

dchar lastLetter(const dchar[] str) {
    return str[$ - 1];
}

dchar lastLetter(in dchar[] str) {
    return str[$ - 1];
}
like image 681
Merchuk Hul Avatar asked Jun 12 '14 09:06

Merchuk Hul


1 Answers

It is nicely explained in the D Language Reference ( http://dlang.org/function.html#parameters ) section which describes parameter storage classes (off-topic items are omitted):

  • scope : references in the parameter cannot be escaped (e.g. assigned to a global variable)
  • in : equivalent to const scope
  • const : argument is implicitly converted to a const type
  • ref : parameter is passed by reference (implicit for reference types, see below)

When they think about parameter storage class, D programmers should remember that D has value (structs, unions, static arrays and PODs) and reference (dynamic arrays, classes) types. The first group of types is always passed by value, unless you use the ref parameter storage class which is going to tell D compiler to explicitly pass by reference. Reference types will naturally be passed by reference, so ref is implicit in this case.

like image 171
DejanLekic Avatar answered Nov 09 '22 15:11

DejanLekic