Kotlin 1.8.20 has been launched and we’re going to discover some new options/enhancements.
- We are going to solely embody New language function And Replace the usual library.
- Please check with Presenter part to verify the complete particulars of this model
listing class entries
operate
Why do we’d like this new performance?
-
values()
– Give againArray
and more often than not we convert it to alisting
to work with it. Simply as in comparison withLists
,Arrays
much less efficient. - Please verify This to study extra about efficiency points.
enum class Language(val extension: String) {
Kotlin(".KT"),
Java(".java"),
Dart(".dart")
}
// values - Returns Array
val languageValues:Array = Language.values()
// New operate entries - Returns Record
val languageEntries:Record = Language.entries
knowledge object
- For higher readability, we have got this new function
- It has a neat and clear
toString()
signify
object EmployeeObject
knowledge object EmployeeDataObject
// Output
println(EmployeeObject)
println(EmployeeDataObject)
// EmployeeObject@50040f0c
// EmployeeDataObject 💜
Sub constructors with physique in inline lessons
- Now beginning with 1.8.20, We will use
Secondary constructors with our bodies
INinline lessons
🎉
@JvmInline
worth class Worker(non-public val fullName: String) {
// Allowed since Kotlin 1.4.30:
init {
verify(fullName.isNotBlank()) {
"Full title should not be empty"
}
}
// Preview out there since Kotlin 1.8.20:
constructor(firstName: String, middleName:String, lastName: String) : this("$firstName $middleName $lastName") {
verify(lastName.isNotBlank()) {
"Final title should not be empty"
}
}
}
Self-closing interface
- To shut the useful resource,
AutoCloseable interface
added frequent customary library. - prolonged operate
use()
can also be included, which executes a sure block operate on the chosen useful resource after which closes it correctly, whether or not aexception is thrown or not
.
Base64 encoding and decoding
Now we now have
Base64
assist inKotlin
. So no extra Java
3 sorts out there
-
Base64.Default,
Base64.UrlSafe
Base64.Mime
@OptIn(ExperimentalEncodingApi::class)
enjoyable base64Experimental() {
// Base64.Default
val nameBytes = "Nav".map { it.code.toByte() }.toByteArray()
val encodedValue = Base64.Default.encode(nameBytes)
// Encode worth: TmF2
println("Encoded: $encodedValue")
// Decoded worth: Nav
println("Decoded: ${String(Base64.Default.decode(encodedValue))}")
// Base64.UrlSafe
val googleIOUrlBytes = "google.io".map { it.code.toByte() }.toByteArray()
// Encode worth: Z29vZ2xlLmlv
val encodedURLSafe = Base64.UrlSafe.encode(googleIOUrlBytes)
println("Encoded UrlSafe: $encodedURLSafe")
// Decoded worth: google.io
println("Decoded UrlSafe: ${String(Base64.UrlSafe.decode(encodedURLSafe))}")
}
Assist for @Unstable
Feedback in Kotlin/Native
Earlier than this 1.8.20 notice in solely out there in frequent customary library And environment friendly in JVM
- Test This to study extra about
@Unstable annotation
class Developer {
@Unstable
non-public var isAndroidDev: Boolean = false
enjoyable isAndroidDevloper(): Boolean = isAndroidDev
enjoyable setAndroidDev(isAndroidDev: Boolean) {
this.isAndroidDev = isAndroidDev
}
}
Previewing Java Composite Property References
public class Developer {
non-public String title;
non-public String coreLanguage;
public Developer(String title, String coreLanguage) {
this.title = title;
this.coreLanguage = coreLanguage;
}
public String getName() {
return title;
}
public String getCoreLanguage() {
return coreLanguage;
}
}
- Entry properties utilizing presenter
val developer = Developer("Nav", "Kotlin")
// references to Java artificial properties
print("Developer title is ${developer::title}")