Is casting the idiomatic way of dealing with nil?

Let me show pass by reference in Perl:

sub change_me {
    $_[0] = 1;

my $a = 0;
print "$a\n"; # => 1, the contents of $a were changed by change_me

# errs with "Modification of a read-only value attempted"

In case you are not familiar with Perl 5, a function gets its arguments in the array @_, and $_[0] means the first element of said array.

Perl programmers normally do not work with @_ directly like that, except for trivial functions. The first thing you do is to copy the array elements into local variables:

sub change_me {
    my ($x) = @_;
    $x = 1;

and that one does not modifify the $a in the caller.

The problem with the name pass by reference is that the word “reference” is kinda overloaded. Java is a clear case of this, it is part of the Java interface that you work with references, which are kinda opaque pointers, opaque indirections. At the same time, it is also part of the language that when a method is called, the invoked method gets a copy of the arguments (pass by value). You can change the state of a mutable object in the caller, but you cannot change the reference itself in the caller.

On the side, mutate a struct:

Guess it was a side note on the matter of a technique to be able to reach a mutable struct from within a method call and mutate it.

But for people following the thread re call semantics, the method is not receiving the struct to be mutated, it is getting a wrapper. The problem now has just been moved to mutate the ValueRef you receive, you can’t.