Unsafe Block
unsafe_block.nr31 lines
unsafe_block.nr· neuro
// Neuro Example: unsafe blocks
// Demonstrates: `unsafe { }` as an expression that evaluates to its trailing
// value, used both bound to a `val` and as the function's implicit return.
//
// The one thing `unsafe` permits is a raw index read or write of a
// `KernelOut` inside a `@kernel` (see examples/tensors/tensor_kernel.nr).
// Anywhere else it behaves exactly like a bare block. Each of the three uses
// prints its value, which is the clearest way to show it really is just a block.
func main() -> i32 {
// unsafe block bound to a value: evaluates to its trailing expression
val base = unsafe {
val a = 10
val b = 7
a + b // 17
}
println("unsafe as value = {base}")
// unsafe block used as a void statement
mut total: i32 = 0
unsafe {
total = base + 8 // 25
}
println("unsafe as statement -> total = {total}")
val returned = unsafe { total - 1 }
println("unsafe as return = {returned}")
// unsafe block as the implicit return value
unsafe { returned } // 24
}
neurc run unsafe_block.nr
unsafe as value = 17
unsafe as statement -> total = 25
unsafe as return = 24