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 5 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*

35 changes: 29 additions & 6 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 @@ -127,7 +128,29 @@ 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!
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!
nsure that you access your arm variable in init in some way to trigger the by lazy function.
### 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
// To ensure that your hardware is initialized in the init stage, access it in some way to trigger the by lazy function
arm
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note that in this example you already initialize it when you call arm.power
maybe make this more clear, that's about it

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just decided to remove the whole initialization example. This would slow down the first loop in some scenarios, but it would also ensure G405 compliance if the user isn't accessing in init. Kind of uncertain about this change though.

// Now that arm has been initialized, we can access it as normal:
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 roo such as the `lateinit` keyword.
j5155 marked this conversation as resolved.
Show resolved Hide resolved

### Overview

Expand Down