Crow programming language

andy andy-hanson at protonmail.com
Fri Feb 16 03:37:10 UTC 2024


On Friday, 16 February 2024 at 03:21:48 UTC, andy wrote:
> It still seems to be considered mutable?

I got this working using a function pointer:

```
@safe:

void main() {
	string a = "a";
	string ab = "ab";
	string ab2 = a ~ "b";
	assert(ab.ptr != ab2.ptr);
	assert(internString(ab).ptr == internString(ab2).ptr);
}

@trusted pure string internString(string s) =>
	(cast(string function(string) pure) &internString_impure)(s);

string internString_impure(string a) {
	foreach (string x; strings) {
		if (x == a)
			return x;
	}
	strings ~= a;
	return a;
}
string[] strings;
```


More information about the Digitalmars-d-announce mailing list