Doubt Regarding Context Objects In Scope Functions

Hey there,I was going through the documentation for Scope Functions and there is something that I don’t understand regarding the it context object. So in the documentation, there’s a line that says:

However, when calling the object functions or properties you don’t have the object available implicitly like this. Hence, having the context object as it is better when the object is mostly used as an argument in function calls.

Can anyone explain this part to me? I’m having some trouble understanding it.

Thanks for all the help 🙂

submitted by /u/racrisnapra666
[link] [comments]

Continue Reading Doubt Regarding Context Objects In Scope Functions

Jetpack Compose Linux performance

Hello everyone.

I wanted to try Jetpack Compose for Desktop on the only laptop I have right now which runs Ubuntu 14.

When I run an empty app, my fps goes down to ~5fps or so. I saw an issue on github where someone managed to resolve this problem by unplugging his second monitor but I still have the problem without mine.

Do you have the same problem if you use Compose on Linux ?

submitted by /u/Lazard_
[link] [comments]

Continue Reading Jetpack Compose Linux performance

The coolest thing I’ve written in Kotlin so far…

Wrote a reactive transaction manager for Spring so I can make transactions across multiple repositories inside a service and not just be tied to making a transaction only on one repository like what

ReactiveTransactionManager

restricts you to. Very useful if a lot of your business logic goes in the service layer:

The gist:

https://preview.redd.it/nhma09798j061.png?width=863&format=png&auto=webp&s=2379f93230ecf7612270d5abba65e15266736cf5

The implementation:

https://preview.redd.it/bz3hzrm48j061.png?width=1169&format=png&auto=webp&s=482af17a342542f85a39bec4737e5b651e32a7fb

The usage: (not the best use case, but at least it fits in one screenshot; it can be used across multiple repositories too)

https://preview.redd.it/yw4duqui7j061.png?width=1171&format=png&auto=webp&s=07f9960cc3248b9acaf99d6d16f4e7e7dea5e942

The usecases may be small, but damn that shitcode was fun to write

submitted by /u/wtf_kyle
[link] [comments]

Continue Reading The coolest thing I’ve written in Kotlin so far…

Server-Side Development with Kotlin: Frameworks and Libraries

It is quite common to use existing frameworks and libraries for backend application development. If we look at most of the popular frameworks in the Java ecosystem, many of them actually support Kotlin out of the box. There are a lot of Java libraries too. You can use these frameworks and libraries in your Kotlin programs thanks to Kotlin’s flawless Java interoperability.


Kotlin is an excellent choice for server-side application development. Go to the Kotlin for server-side page to learn more!


This blog post is partially based on presentation content from the Kotlin 1.4 Online Event. You can watch the full video on our YouTube channel.

Spring Framework

The most popular and feature-rich framework in the ecosystem is Spring. Approximately half of all projects in the JVM ecosystem now use Spring as the framework of choice. The previous blog post about Kotlin for server-side development includes stories from Atlassian and Expedia who use Spring Boot for their projects.

Kotlin is one of the four major themes for Spring Framework 5.x. A number of extensions were added to the Spring core API specifically to provide better support for Kotlin.

In fact, Spring’s official documentation features code samples in both Java and Kotlin. This is very convenient if you’re new to Kotlin, because you can see the code sample in Java and then immediately see its Kotlin equivalent and so understand the idioms faster.

The start.spring.io project generator also supports Kotlin along with Java and Groovy.

The number of Spring tutorials is growing. We recommend that you start with the Building web applications with Spring Boot and Kotlin tutorial.

As support for Kotlin in Spring Framework is being improved, we had the privilege to host Sebastien Deleuze with his presentation about The State of Kotlin Support in Spring at the Kotlin 1.4 Online Event this year. See the video below.

An interesting new addition to Spring is the project Spring Fu. It is designed to configure Spring Boot with code in a declarative way. The Kotlin variety of the configuration DSL looks quite nice:

val app = webApplication {
	logging {
		level = LogLevel.DEBUG
	}
	beans {
		bean<SampleService>()
	}
	webMvc {
		port = if (profiles.contains("test")) 8181 else 8080
		router {
			val service = ref<SampleService>()
			GET("/") {
				ok().body(service.generateMessage())
			}
			GET("/api") {
				ok().body(Sample(service.generateMessage()))
			}
		}
		converters {
			string()
			jackson {
				indentOutput = true
			}
		}
	}
}

This new approach to configuration in Spring promises faster startup times and lower memory consumption, and it is a good fit with GraalVM native image thanks to its reflection-less approach.

Ktor

Ktor is a framework created and maintained by JetBrains. It was built with Kotlin from the ground up. Ktor encourages a minimalistic approach to application design, as you only need to configure the functionality that your project requires. Using Kotlin coroutines, Ktor is truly asynchronous and highly scalable. Ktor also includes an HTTP client that is implemented as a multiplatform library and is widely used in mobile application development with Kotlin.

Our previous blog post covers JetBrains Space and Adobe Experience Platform projects that use Ktor because of its support for asynchronous programming.

At Kotlin 1.4 Online Event, Hadi Hariri gave a presentation about the current state of the Ktor project and its future plans. See the video below.

Here are a few tutorials we recommend for learning about Ktor:

More web frameworks

Of course, Spring and Ktor are not the only frameworks in the JVM ecosystem and there are plenty of other frameworks that you can use with Kotlin. Take a look at Micronaut, Quarkus, Javalin, SparkJava, Vaadin, CUBA, and Vert.x. This is just a small list of the frameworks available. All these frameworks have either code samples in Java and Kotlin side by side or have dedicated tutorials on how to use the framework with Kotlin.

There are also a number of frameworks that are implemented in Kotlin. For instance, http4k, and Hexagon.

Database access

Backend applications often rely on database access. One of the most popular libraries today for working with databases is the Exposed library. It is an ORM framework for Kotlin that offers two levels of database access: a typesafe SQL-wrapping DSL, and lightweight data access objects.

Here’s a very simple example using Exposed:

object Cities : Table() {
   val id = integer("id").autoIncrement().primaryKey() // Column<Int>
   val name = varchar("name", 50) // Column<String>
}

Database.connect("jdbc:h2:mem:test", driver = "org.h2.Driver")

transaction {
   create(Cities)

   val saintPetersburgId = Cities.insert {
       it[name] = "St. Petersburg"
   } get Cities.id

   val munichId = Cities.insert {
       it[name] = "Munich"
   } get Cities.id

   Cities.insert {
       it[name] = "Prague"
   }

println("All cities:")

for (city in Cities.selectAll()) {
   println("${city[Cities.id]}: ${city[Cities.name]}")
}

As you might have guessed, Exposed is not the only library that you can use with Kotlin for database access. You can use jdbi, jooq, JPA with Spring Data, and even just pure JDBC API to access databases from Kotlin code.

Testing

Testing a vital part of application development. JUnit and TestNG are perfectly fine for implementing unit tests in Kotlin. But there are more additional libraries that are great for testing in Kotlin:

  • Spek – a Kotlin-based Specification Testing framework for the JVM.
  • Kotest – a testing library with support for Kotlin multiplatform projects.
  • MokK – a mocking library for Kotlin tests.
  • Kotlin Power Assert – an amazing compiler plugin for Kotlin that enables the diagramming of assert function calls, making diagnosis of test failures a breeze.

Other libraries

Aside from handling HTTP requests and accessing databases, applications are often required to communicate with other services using specific protocols, perform decoding/encoding of data in various formats, and many other things. Here are a few libraries that will be useful for application development with Kotlin.

JSON is the universally accepted standard format for sending data between web servers and browsers. There are awesome JSON libraries available in the Java ecosystem and they are great for use in Kotlin programs too. Jackson and gson are the most popular Java libraries for working with JSON. However, there are also libraries that are implemented with Kotlin from scratch. Klaxon is a good example. Also kotlinx.serialization was recently released and it provides JSON support.

gRPC is a high-performance, open-source, universal RPC framework. Kotlin is now supported by gRPC.

RSocket is also gaining popularity as a protocol for use in building microservices. Kotlin integration was recently added to RSocket.

To work with GraphQL there’s graphql-kotlin library maintained by Expedia Group engineers.

These are just a few examples. In fact, there is a long list of Kotlin libraries maintained by the community where you’re sure to find a library for the task at hand. However, we’d like to point out that, thanks to Java interoperability, it is also possible to use virtually any Java library with Kotlin.

Additional Tools

Code quality is an important topic for Kotlin too! Thanks to the type system, especially null-safety, Kotlin is quite a safe programming language. However, there is still a need for code style checks. That’s the part that is currently implemented via additional tools.

Ktlint is a static code analysis tool maintained by Pinterest. It is a 2-in-1 tool that provides linting and formatting (supported via EditorConfig) for Kotlin code.

Detekt is a static code analysis tool that operates with the abstract syntax tree provided by the Kotlin compiler. It is easy to set up with Gradle and there’s also an IntelliJ IDEA plugin that simplifies configuration.

Generating API documentation from source code is also an important part of software project development. In Kotlin, we can use Dokka for this task. It is a documentation engine for Kotlin. Just like Kotlin itself, Dokka fully supports mixed-language Java/Kotlin projects. It understands standard Javadoc comments in Java files and KDoc comments in Kotlin files, and can generate documentation in multiple formats including standard Javadoc, HTML, and Markdown.

Summary

Kotlin was initially designed as an alternative JVM programming language, meaning that Kotlin can be used anywhere where Java is used. Thanks to Java interoperability, it is easy to use any of the existing frameworks and libraries that are familiar to Java developers. This all greatly simplifies the adoption of Kotlin for application development on the JVM.

The current state of Kotlin support in the ecosystem is at the stage where even big projects and companies are confidently adopting it. You can read about a few notable examples of such projects in the previous blog post, Productive Server-Side Development With Kotlin: Stories From The Industry

Continue Reading Server-Side Development with Kotlin: Frameworks and Libraries

Kotlin for Interviews series

Hello and happy Friday!

In the first half of November, we published a series of 5 Kotlin for Interviews articles written by Sherry Yuan. They cover data structures, collection functions, math, common algorithms, and other parts of Kotlin that are important to know for technical interviews.

You will find them all below!

But first, check “Kotlin for Interviews” cheat sheet that compiles an overview of code snippets that are coming up often during the interviews.

Part 1: Common Data Types

It covers a few common data types that appear often in algorithms and data structures questions, eg. MutableList, HashMap, or PriorityQueue.

Part 2: Collection Functions

This part covers Collection Functions topics: getters, search and find, sorting, boolean summary and statistical summary.

Part 3: Numbers and Math

It covers numbers and math topics, like number types and math operators, as well as useful functions and constants.

Part 4: Iteration

This part covers Refresher on Range, 1D arrays/lists, 2D arrays/lists, Maps and PriorityQueues.

Part 5: Frequently Used Code Snippets

It covers frequently used code snippets, like creating graphs in adjacency list form, breadth and depth-first search, tree traversal and dynamic programming/memoization.

Happy coding!
Kt. Academy Team


Kotlin for Interviews series was originally published in Kt. Academy on Medium, where people are continuing the conversation by highlighting and responding to this story.

Continue Reading Kotlin for Interviews series

End of content

No more pages to load