How to ensure no arguments are unintentionally mutated when testing a function in Julia

When testing Julia functions I sometimes want to check that none of the arguments are unintentionally mutated. It’s a bit labour intensive at the moment: I write code to backup the arguments via copy, call the function, compare arguments with their backups.

Is there a macro that can do that for me? Something like @checkargsnotmutated myfunction(arg1, arg2, arg3).

Note that the original poster on Slack cannot see your response here on Discourse. Consider transcribing the appropriate answer back to Slack, or pinging the poster here on Discourse so they can follow this thread.
(Original message :slack:) (More Info)

Thanks to Benoît Richard over on Julia Slack for his answer, which worked nicely:

You don’t actually need a macro a function should be sufficient. Something like

function test_no_mutation(func, args...)
    backups = copy.(args)
    for (arg, backup) in zip(args, backups)
        @test arg == backup

Should do the trick.

1 Like