1. Getting and Setting with Active Bindings
Data values stored in the private element of an R6 class are not directly accessible by the user. However sometimes
2. Danger_IMG
you may wish to provide controlled access to these data fields. There are two access cases. You may want to retrieve the data field, or you may want to change it. In object-oriented programming,
3. getting and setting
these are known simply as "getting the data", and "setting the data".
In R6, this controlled access to private fields is achieved through
4. Active Bindings
active bindings. Active bindings are defined like functions, but are accessed like data variables. This sounds complicated, but it actually gives a really easy to use syntax.
Let's start with how you define them.
5. thing_factory
Active bindings are added to the active element of a class. Just like the private and public elements, the active element must be a named list.
One of the restrictions of R6 is that the elements of private, public, and active must all have different names. A useful convention to help distinguish private and active elements is to start all private fields with a double dot. For you, as a programmer, this makes the private fields stand out, so you have a quick visual way of signifying that these variables are not for consumption by the user.
The simplest case is to create a read-only active binding.
That means that you want only want to retrieve a data field, rather than being able to change it. In this case, the function takes no arguments, and can simply return the corresponding private field. Here, the active binding a_field returns the private dot dot a_field.
Since the a_field binding is a function, you can include custom logic. For example, if the data field was missing, you could return a default value.
The second, more complex case is when you want users to be able to change the value of the data field as well. In this case, the binding function should take a single argument, by convention named "value".
If value is missing, the function just returns the private data field as before.
However, when value is passed to the active binding, you need some logic to set the private value. The code pattern now includes an else clause.
As I mentioned at the start of the video, the point of active bindings is to allow controlled access to the private fields. This means that you can add custom logic to check the value before you assign it. For example, if another_field should only contain a single number, you can use assert_is_a_number from the assertive package to check this condition, and throw an error if the value is something else.
assertive contains around 400 checks for different conditions, so it makes checking your variables easy.
Now you know how to create active bindings, let's take a look at how to use them.
6. a_thing
First you create an R6 thing object. To retrieve the value of dot dot a_field, you call the active binding a_field. Notice that this is accessed as though it were a data variable, not a function. That is, you don't write any parentheses at the end.
Since a_field was defined as a read-only variable, if you try to change it, you get an error.
7. $another_field
By contrast, you can set another_field.
However, the logic in the binding states that the value must be a single number. If you try to set the value to a string, you get an error.
8. Summary
To summarize, active bindings allow controlled access to private data fields. They are defined as functions, but the user accesses them like data variables. You can use custom logic inside active bindings, typically to check that a variable is being set to a suitable value. The assertive package provides a wide selection of checks for this purpose.
9. Let's practice!