Kotlin / kotlin-by-example

The sources of Kotlin by Example.
https://play.kotlinlang.org/byExample/overview
MIT License
460 stars 897 forks source link

The documentation for Null Safety doesn't work this way. #212

Open Nirajan1-droid opened 8 months ago

Nirajan1-droid commented 8 months ago

Null Safety In an effort to rid the world of NullPointerException, variable types in Kotlin don't allow the assignment of null. If you need a variable that can be null, declare it nullable by adding ? at the end of its type.

fun main() { //sampleStart var neverNull: String = "This can't be null" // 1

neverNull = null                                        // 2

var nullable: String? = "You can keep a null here"      // 3

nullable = null                                         // 4

var inferredNonNull = "The compiler assumes non-null"   // 5

inferredNonNull = null                                  // 6

fun strLength(notNull: String): Int {                   // 7
    return notNull.length
}

It should be this in documentation : println(strLength(neverNull)); // 8 println(strLength(nullable)); // 9

instead of this: strLength(neverNull) // 8 strLength(nullable) // 9 //sampleEnd }

i have already created the pull request for this.

Ahmadpansota commented 8 months ago

neverNull = null // 2

var nullable: String? = "You can keep a null here" // 3

nullable = null // 4

var inferredNonNull = "The compiler assumes non-null" // 5

inferredNonNull = null // 6

fun strLength(notNull: String): Int { // 7 return notNull.length }

Ahmadpansota commented 8 months ago

3