CreateOnGo (Kt)
This only works if you extend the BlackOp
class. More info about that here. It's essentially LinearOpMode with
a few extra utilities, and it's made for use with the Blacksmith Library.
The BlackOp class two different methods to keep your component creation simple and idiomatic:
createOnGo
evalOnGo
You know how in a typical OpMode you have to be all like this or something?
private lateinit var myArm: MyArmClass // Unidiomatic unneccesarily mutable state
override fun init() {
myArm = MyArmClass(...) // Ugly and also duplication of the name/class.
}
With createOnGo
, you can simply do this instead:
// Immutable state, no duplication
private val myArm by createOnGo<MyArmClass>()
// Keep in mind you need to use lambdas though
private val myClaw by createOnGo<MyClawClass> { param1 }
// for each parameter
private val myLift by createOnGo<MyLiftClass>({ param1 }, { param2 })
If you need to call a method or have a lot of parameters or do something more advanced, you can use evalOnGo
instead,
to call a function which returns the object you want to create at go-time.
// No need to explicitly say '<DcMotorEx>' here since Kotlin can infer it from the function return type
private val myThing by evalOnGo {
createSomethingThroughAFunction(1, 2, 3, 4, 5, hwMap, anotherFunction(hwMap))
}
createOnGo
uses reflection, so it can occasionally be a bit finicky. If you're having issues, try evalOnGo
.
Make sure that the argument's types are the same as the desired constructor, and that the order is the same, as the method parameters are NOT type safe
createOnGo
evalOnGo
Practical example
We use it extensively in our base opmodes, especially our base TeleOp