Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix typos in why_kotlin and add section on by lazy with hardware #26

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/intro_to_programming/intro_to_git.md
Original file line number Diff line number Diff line change
Expand Up @@ -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*

34 changes: 27 additions & 7 deletions src/misc/why_kotlin/why_kotlin.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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.

Expand Down Expand Up @@ -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

Expand Down