in parameter - do we still need to add -preview=in ?

Brother Bill brotherbill at mail.com
Mon Nov 10 13:00:37 UTC 2025


https://dlang.org/changelog/2.094.0.html#preview-in

My question is whether to get the optimal `const scope` behavior 
with `in`, do we still need to add:
    `-preview=in` to the CLI when compiling our D program?

The program sample is derived from Programming in D, page 152.

source/app.d
```
import std.stdio;

void main()
{
	const(int[]) constSlice = [10, 20, 30, 40];
	print(constSlice); 		// ← compilation ERROR with first and third 
print functions
}

// This won't compile as the parameter allows mutation, even 
though no mutation is occurring
// void print(int[] slice) {
// 	writefln("%s elements: ", slice.length);
// 	foreach (i, element; slice) {
// 		writefln("%s: %s", i, element);
// 	}
// }

// This will compile for all of int[] slice, const(int[])slice 
and immutable(int[]) slice
// This is "const-correct"
// `in` is equivalent to `const scope`
// This is the preferred D way to state that the slice parameter 
won't be mutated, transitively, that is, "all the way down"
void print(in int[] slice)
{
	// slice ~= 50;			// ← compilation ERROR: Can't append to slice
	// slice[0] = 11;		// ← compilation ERROR: Can't change any 
element
	// slice.length = 2;	// ← compilation ERROR: Can't change length

	writefln("%s elements: ", slice.length);
	foreach (i, element; slice)
	{
		writefln("%s: %s", i, element);
	}
}

// This won't compile as immutable is too strong
// void print(immutable int[] slice) {
// 	writefln("%s elements: ", slice.length);
// 	foreach (i, element; slice) {
// 		writefln("%s: %s", i, element);
// 	}
// }

```

Console output
```
4 elements:
0: 10
1: 20
2: 30
3: 40
```



More information about the Digitalmars-d-learn mailing list