Extending the unsafe keyword

I have in the past proposed that the unsafe keyword only applies to a single function call, and method calls use a different syntax:

So you would write your example as a.b.unsafe c().d().

This wouldn't work with operators, only function calls and method calls.

In unsafe f(g()).h() it would only apply to f, not g or h. Note that there is currently no way to write this without introducing an extra variable or unnecessarily including g() in the unsafe block.

If anybody mistakenly writes it thinking it applies to more than the f call there will be no great harm done: they will just get a compile error.

In this example, assuming self.as_ptr is a safe function, it is unnecessarily included in the unsafe block. So there is more to analyze regarding safety than if the unsafe keyword only applied to the function call.

2 Likes