diff --git a/src/intro_to_programming/intro_to_git.md b/src/intro_to_programming/intro_to_git.md index 01a74c7..a6079c3 100644 --- a/src/intro_to_programming/intro_to_git.md +++ b/src/intro_to_programming/intro_to_git.md @@ -212,5 +212,5 @@ Once any conflicts are resolved, and you are ready to merge the branches (potentially after getting approval from your team), select the `Merge pull request` button to accept the pull request. -*Last updated: 2024-08-06* +*Last updated: 2024-10-06* diff --git a/src/misc/why_kotlin/why_kotlin.md b/src/misc/why_kotlin/why_kotlin.md index e76299a..904c281 100644 --- a/src/misc/why_kotlin/why_kotlin.md +++ b/src/misc/why_kotlin/why_kotlin.md @@ -4,9 +4,10 @@ *Are you curious about why some FTC programmers like to use Kotlin for their code bases?* -Kotlin is a language with very high cross compatability with Java, which means it can be used to write your FTC code. +Kotlin is a language with very high cross-compatability with Java, which means it can be used to write your FTC code. -FIRST provides official instructions for adding Kotlin to your project [here](https://ftc-docs.firstinspires.org/en/latest/programming_resources/shared/installing_kotlin/Installing-Kotlin.html) +FIRST provides official instructions +for adding Kotlin to your project [here.](https://ftc-docs.firstinspires.org/en/latest/programming_resources/shared/installing_kotlin/Installing-Kotlin.html) ## Ingredients @@ -15,12 +16,12 @@ FIRST provides official instructions for adding Kotlin to your project [here](ht ## The Recipe -Kotlin is a language that makes a very solid attempt at modernising Java. -It makes writing common Java patterns extremely concise. +Kotlin is a language that makes a very solid attempt at modernizing Java. +It makes writing common Java patterns concise. Kotlin also makes it easy to write safer code that is less likely to have strange bugs or throw confusing NullPointerExceptions. Kotlin is unlikely to be particularly useful to you if you are not using Object-Oriented aspects of Java already. -If you are just writing \[Linear\]OpModes, but are not writing your own classes, Kotlin is probably not for you. +If you are just writing \[Linear\]OpModes but are not writing your own classes, Kotlin is probably not for you. While Kotlin certainly does offer some nice features in this environment, the challenges that come with using Kotlin may also prove hard to overcome unless you are writing more complex and involved code. It is also advisable not to try to switch to Kotlin at the same time as learning more Object-Oriented skills. @@ -126,8 +127,27 @@ fun nullSafeMethodCall(a: Int?): Double { } ``` -We'll combine the concept above with the `?.` operator, which performs a null safe method call. -If a is null, Kotlin won't try to call `.toDouble()` on it and will just return null, which will then be caught by the Elvis operator! +We combine the concept above with the `?.` operator, which performs a null safe method call. +If `a` is null, Kotlin won't try to call `.toDouble()` on it and will just return null, +which will then be caught by the Elvis operator! + +### Accessing Hardware +Because of Kotlin's null safety system, accessing hardware must be done differently. +There are a few ways to do this, but the one we'll show here is to use `by lazy`: +```kt + // by lazy will only initialize this variable the first time it is used. + // This prevents it from ever being null, but also allows you to initialize it only after your opmode begins. + val arm by lazy { hardwareMap["arm"] as DcMotorEx } // Alternately hardwareMap.get(DcMotorEx::class.java, "arm") also works here + + override fun init() { // or runOpMode() for LinearOpModes + // Since we used by lazy, accessing the arm in any way will automatically initialize it: + arm.power = 1.0 + telemetry.addData("armPos", arm.currentPosition) + telemetry.update() + // Note that, since we used by lazy, we do NOT need to put !! after arm. + } +``` +There are other options to do this as well such as the `lateinit` keyword. ### Overview