Kotlin 1.7.0-Beta Released

The first preview of Kotlin 1.7.0 is now available. Some highlights of this preview:

  • The builder infers changes.
  • min() and max() aggregate function regression.
  • Absolutely non-nullable types are stabilized.
  • New Kotlin/Native memory manager update.

Install 1.7.0-Beta to try out these features, and report any issues you find to help us improve Kotlin 1.7.0.

We'll cover other exciting features in subsequent blog posts. stay tuned!

Starting with 1.7.0, we've updated our release cadence terminology: changed "Milestone" to "Beta". There are several reasons behind this decision:

  • We want the Kotlin build terminology to be more in line with the standard terminology of the software release cycle . More precisely, "Beta" means that our work on adding new features to that particular release is complete and we are working on stabilization. However, we will also implement final changes, including those based on user feedback.
  • Some time ago, the M-release compiler has been generating "pre-release" code, which makes these versions harder to test. It's completely different now. We want to avoid any confusion and stress that the process of trying out the Kotlin Beta version will be simple and the Kotlin team highly encourages you to try it out.
  • Last but not least, the word "Beta" itself is a call for feedback from the community. By this term, we want to let you know that we want you to share your feedback with us.

Builder infers changes

Builder inference is a special type of type inference that is helpful when calling generic builder functions. It helps the compiler infer the type arguments of the call by using the relevant type information of other calls in its lambda arguments.

More changes to builder inference are included in Kotlin 1.7.0-Beta. These changes stabilize our builder inference and close one of our roadmap tasks.

In this release, builder inference is automatically activated without specifying a -Xenable-builder-inference compiler option ( which we introduced in version 1.6.0 ) if regular type inference cannot get enough information about the type  .

This means that now you can write your own builders that use builder type inference without applying any extra annotations or options. Learn how to write custom generic builders .

min() and max() aggregate function regression

In  Kotlin 1.4  , we   renamed  the min() sum  set function to  sum  . These new names better reflect their behavior - if the receiver collection is empty, it is returned  . It also helps to align the behavior of functions with the naming conventions used throughout the Kotlin Collections API. , ,  and max()minOrNull()maxOrNull()nullminBy()maxBy()minWith()maxWith() 

Likewise, each has its own  *OrNull() synonym in Kotlin 1.4. Older functions affected by this change have been gradually deprecated.

Kotlin 1.7.0-Beta reintroduced the original function names, but added a non-nullable return type. The updated  min(), max(), minBy(), maxBy(), minWith() and  now maxWith() strictly return the collection element or throw an exception.

fun main() {
    val numbers = listOf<Int>()
    println(numbers.maxOrNull()) // "null"
    println(numbers.max()) // "Exception in… Collection is empty."
}

See this YouTrack issue for details.

Absolutely non-nullable types are stabilized

Kotlin 1.7.0 will have stable absolutely non-nullable types, which were introduced in Kotlin 1.6.20 .

The purpose of adding these types is to provide better interoperability when extending generic Java classes and interfaces.

As of Kotlin 1.6.20, you have been able to use the new syntax  T & Any to mark generic type parameters as absolutely non-nullable on the usage site. This syntactic form comes from intersecting type notation, and is now restricted to a single type parameter, & with a nullable upper bound on the left and a non-nullable on the right  Any:

fun <T> elvisLike(x: T, y: T & Any): T & Any = x ?: y

fun main() {
    elvisLike<String>("", "").length // OK
    elvisLike<String>("", null).length // Error: 'null' cannot be a value of a non-null type

    elvisLike<String?>(null, "").length // OK
    elvisLike<String?>(null, null).length // Error: 'null' cannot be a value of a non-null type
}

In this beta, absolutely non-nullable types will be enabled by default. No additional steps are required.

Learn more about absolutely non-nullable types in  KEEP  .

Regular expression to match at a specific location

The  Regex.matchAt() sum  function introduced in 1.5.30Regex.matchesAt()  has now reached a stable release. They provide a way to check if a regular expression   has an exact match at a specific position in String or  .CharSequence

  • matchesAt() You can check for a match and return a boolean result:
fun main(){
    val releaseText = "Kotlin 1.7.0 is on its way!"
    // regular expression: one digit, dot, one digit, dot, one or more digits
    val versionRegex = "\\d[.]\\d[.]\\d+".toRegex()

    println(versionRegex.matchesAt(releaseText, 0)) // "false"
    println(versionRegex.matchesAt(releaseText, 7)) // "true"
}
  • matchAt()will return a match if a match is found, or null if no match is found:
fun main(){
    val releaseText = "Kotlin 1.7.0 is on its way!"
    val versionRegex = "\\d[.]\\d[.]\\d+".toRegex()

    println(versionRegex.matchAt(releaseText, 0)) // "null"
    println(versionRegex.matchAt(releaseText, 7)?.value) // "1.7.0"
}

We would greatly appreciate your feedback in this YouTrack issue .

New Kotlin/Native memory manager

We will continue to collect feedback and improve the new Kotlin/Native memory manager. Currently, you can try out the Alpha version in your project. Kotlin 1.7.0-Beta brings more performance improvements that will improve the developer experience.

The new memory manager eliminates the differences between the JVM and the Native platform. It provides a consistent developer experience across multi-platform projects. For example, you can more easily develop new cross-platform mobile applications for both Android and iOS platforms.

The new Kotlin/Native memory manager removes the restriction on object sharing between threads. It also provides leak-free concurrent programming language primitives that are safe and do not require any special management or annotations. The new memory manager will be the default manager in future releases, so we encourage you to try it now. Learn more about the new memory manager and explore the demo project, or jump straight to the migration instructions to try it out for yourself.

 Try out the new memory manager in your projects to see how it works, and share your feedback in our issue tracker,  YouTrack .

Named Capture Group Support in JS and Native

As of Kotlin 1.7.0-Beta, named capture groups will be supported not only on JVM (1.8 and above), but also on JS and Native.

(?<name>group) To name capturing groups, use syntax in regular expressions  . To get the text matched by a group, call the newly introduced  MatchGroupCollection.get() function and pass the group name.

Retrieve matching group value by name

Consider this example of matching city coordinates. To get a collection of groups matched by a regular expression, use  groups. Compare using  value by-group number (index) and by-group name to retrieve the contents of a group:

fun main() {
    val regex = "\\b(?<city>[A-Za-z\\s]+),\\s(?<state>[A-Z]{2}):\\s(?<areaCode>[0-9]{3})\\b".toRegex()
    val input = "Coordinates: Austin, TX: 123"
 
    val match = regex.find(input)!!
    println(match.groups["city"]?.value) // "Austin" — by name
    println(match.groups[2]?.value) // "TX" — by number
}

named backreference

You can now also use group names when referencing groups backwards. Backreferences match the same text that was previously matched by the capturing group. \k<name> To do this, use the syntax in your regular expression  :

fun backRef() {
    val regex = "(?<title>\\w+), yes \\k<title>".toRegex()
    val match = regex.find("Do you copy? Sir, yes Sir!")!!
    println(match.value) // "Sir, yes Sir"
    println(match.groups["title"]?.value) // "Sir"
}

Replace named groups in expressions

Finally, named group references can be used with replacement expressions. Think of functions that replace all occurrences of regular expressions in the input with replacement expressions  replace() , and functions that swap only the first occurrence  replaceFirst() .

Each occurrence in the replacement string  ${name} is replaced with the subsequence corresponding to the capturing group with the specified name. Compare group references by name and by index:

fun dateReplace() {
    val dateRegex = Regex("(?<dd>\\d{2})-(?<mm>\\d{2})-(?<yyyy>\\d{4})")
    val input = "Date of birth: 27-04-2022"
    println(dateRegex.replace(input, "\${yyyy}-\${mm}-\${dd}")) // "Date of birth: 2022-04-27"  — by name
    println(dateRegex.replace(input, "\$3-\$2-\$1")) // "Date of birth: 2022-04-27" — by number
}

Try out new features and provide feedback

You can use these new features in the 1.7.0 preview Kotlin 1.7.0-Beta. You can easily install it in  IntelliJ IDEA  or  Android Studio  IDE.

Due to Android Studio plugin renaming (beta), plugin installation is supported in 1.6.20 and later.

Please install Kotlin 1.7.0-Beta by either:

  • If you are using the Early Access Preview update channel, the IDE will suggest an automatic update to 1.7.0-Beta as soon as it becomes available.
  • If you are using the stable update channel, you can  switch to the early access preview channel at any time by selecting Tools  |  Kotlin  |  Configure Kotlin Plugin Updates in your IDE. Then you will be able to install the latest preview version. See these instructions for details.

You can always download the latest versions of these IDEs to guarantee full Kotlin support:

  • IntelliJ IDEA  - For developing Kotlin applications for different platforms.
  • Android Studio  - For developing Android and cross-platform mobile applications.

Don't forget to change the .

If you encounter any problems:

read more

Guess you like

Origin www.oschina.net/news/197632/kotlin-1-7-0-beta-released