Skip to main content

CreateOnGo (Kt)

Important

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))
}
danger

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

Params
  • <reified T> - The type you want to construct
  • args: vararg () -> Any - Any number of lambda functions that return the parameters you want to pass to the constructor of the class you want to create. Not evaluated until the OpMode is started
  • Returns
  • T - The class constructed by invoking the constructor of said class with the corresponding parameters
  • evalOnGo

    Params
  • <T> - The type you want to construct
  • constructor: () -> T - A lambda function that returns the object you want to create. Not evaluated until the OpMode is started
  • Returns
  • T - The object returned by the given lambda
  • Practical example

    We use it extensively in our base opmodes, especially our base TeleOp

    Link to our base TeleOp