Kotlin 1.7.0 Beta Enables Definitely Non-nullable Types

Kotlin 1.7.0 Beta Enables Definitely Non-nullable Types

JetBrains has published Kotlin 1.7.0-Beta, which includes the reintroduction of the min(), max(), minBy(), maxBy(), minWidth(), and maxWith() functions and enables builder inference and definitely non-nullable types by default.

In Kotlin/JS and Kotlin/Native, regular expressions now support named capturing groups and can check for exact matches at specific positions.

Beginning with version 1.7.0, "Milestone" releases are now referred to as "Beta" releases to align with standard software release life cycle terminology, where "Beta" means that no new features are added and the focus is on strengthening the functionalities with the support of community input.

You can configure the build scripts from the Early Access Preview to use these new features in version 1.7.0-Beta.

The re-introduction of min(), max(), minBy(), maxBy(), minWidth(), and maxWith()

The min(), max(), minBy(), maxBy(), minWidth(), and maxWith() functions have been brought back in this release, and they now return the collection element or throw an exception:

fun main() {
    val emptyList = listOf<Int>()


    // This prints null
    println(emptyList.minOrNull())

    // This throws an exception 
    //     "Exception in… Collection is empty."
    println(emptyList.min())
}

Support for definitely non-nullable types

To enhance interoperability when extending generic Java classes and interfaces, Kotlin 1.7.0 automatically provides support for definitely non-nullable types, which were introduced in Kotlin 1.6.20.

For instance, when overriding a Java method with a @NotNull argument, the syntax T & Any can be used to indicate that a generic type parameter is definitely non-nullable:

// Java method:
@NotNull
public T calculate(@NotNull T x) {}

// Kotlin method:
override fun calculate(x: T1 & Any): T1 & Any

Regex.matchAt() and Regex.matchesAt() are now Stable

Regex.matchAt() and Regex.matchesAt() are now Stable and can be used to check whether a regular expression matches at a specific position in a String or CharSequence:

myRegex.matchesAt(text, 42) // returns true or false
myRegex.matchAt(text, 42) // returns the match or null

By employing the (?name>group) syntax in a regular expression, named capturing groups are now supported on Kotlin/JVM, Kotlin/JS, and Kotlin/Native.

By supplying the group name, the new MatchGroupCollection.get() function can be used to extract the content matched by a group:

val regex = "(?<movie>[A-Za-z\\s]+):(?<year>[0-9]{4})".toRegex()
val movie = "The Fellowship of the Ring:2001"

val match = regex.find(movie)!!

// The following statement prints The Fellowship of the Ring
println(match.groups["movie"]?.value)

// The following statements print 2001
println(match.groups["year"]?.value)
println(match.groups[2]?.value)

A new Kotlin/Native Memory manager

The latest Kotlin/Native Memory manager, which improves performance and developer experience, is now available as an Alpha release. The new memory manager simplifies the developer experience when developing cross-platform applications for platforms such as Android and iOS by eliminating distinctions between the JVM and Native platforms. In an upcoming update, this solution will be the default option, and it can be evaluated by following the migration instructions.

Conclusion

The Kotlin Blog has a comprehensive summary of this release, and any issues discovered during testing can be reported on YouTrack.


✋If you have any questions about this article, ask them in our GitHub Discussions 👈 community. You can also Gitter

❤️ Like our page and subscribe to our feed for updates!