6 | Modifying Arguments
What is ModifyArg?
Consider the following method
As you can clearly observe, it consists of a set print statements, which outputs 10 strings with no noticeable pattern.
So, what if I wanted to change the text "Hi! 5!"
to "Hello world!"
?
With our current knowledge all we could do is @Overwrite
it, but this becomes very painful with larger method bodies with functions/variables that need @Shadow
'ing (I will talk about shadowing later)
So what do we do?
May I introduce you to @ModifyArg
; a neat function that allows us to modify the arguments of a method in a function
In the example provided, we can use @ModifyArg
like so
Since we've already covered most of what's going on inside that annotation, all we need to cover is the ordinal
and the index
.
ordinal = 4
(optional)This allows you to set an offset of which
println
statement you actually want to modify, indexed by zero, in this case, it was set to 4, meaning that the 4th zero indexed println statement's arguments will be modified
index = 0
This is the last parameter that we have set, which isn't inside the
@At
statement, this allows us to select which argument in the function we want to modify (indexed by zero)
Lastly, the return statement, return "Hello World!"
, just returns what we want that argument to be
Modify Arg cont.
What happens if the argument we want to modify depends on all of the other arguments being passed in?
Say, instead of println
accepting 1 argument (String
), rather, it accepted 2 arguments, your message, and the number of times you want it to be printed
In this example, once again, we want to change the text of "Hi! 5!"
to "Hello world!"
, but this time we also want to add the number of times it will repeat to it (ie: "Hello World! (34)"
)
It's as simple as adding the extra arguments to the function, like shown below
ModifyArg vs Modify Args
While going through this tutorial, you might've noticed that there's another annotation called ModifyArgs
instead of ModifyArg
. So, what's the difference?
Well, using the former example where println
has 2 arguments, a message and a number of times to print that message, we can modify multiple arguments like so
Args? What's that?
Args
is a class provided by Mixin
that provides 2 major methods
args.get(int index)
This allows you to retrieve one of the function's arguments be index
args.set(int index, T value)
This allows you to set an arguments value by index
An annotated example of ModifyArgs
is shown below
Last updated
Was this helpful?