pBook + eBook The Kotlin programming language offers an expressive syntax, a strong intuitive type system, and great tooling support along with seamless interoperability with existing Java code, libraries, and frameworks. Coding with Kotlin means less hassle. And with an efficient compiler and a small standard … { positiveButton("Yes") { process() } negativeButton("No") { cancel() } } } Can you spot three lambdas in this code? Kotlin can be compiled to Java bytecode, so you can use it everywhere Java is used, including Android. Kotlin in Action MEAP V11 - … Email. You’ll receive a link in your inbox to access your eBook. () -> Op) : Query object SqlExpressionBuilder { infix fun Column.eq(t: T) : Op // } The SqlExpressionBuilder object defines many ways to express conditions: compare values, check for being not null, perform arithmetic operations etc You never refer to it explicitly in the code, but you regularly call its methods when it’s an implicit receiver The select function takes a lambda with receiver as a parameter, and the SqlExpressionBuilder object is an implicit receiver in this lambda That allows you to use in the body of the lambda all the possible extension methods defined in this object, such as eq We’ve seen two types of extensions on columns: the ones that should be used for declaring a Table, and the ones used to compare the values in a condition Without member extensions, we would have to make all these methods extensions or members of Column, which would allow to use them in any context The approach with member extensions gives us a way to control that NOTE Our first encounter with Exposed framework: Delegated properties In section 7.5.6 we also looked at some code working with Exposed while talking about using delegated properties in frameworks Delegated properties often come up in DSLs, and the Exposed framework illustrates that well We won’t repeat the discussion of delegated properties here, since we’ve covered them in detail before However, if you’re eager to create a DSL for your own needs or just improve your API and make it cleaner, keep this feature in mind 11.4.4 Anko: Creating Android UIs dynamically While talking about lambdas with receiver, we mentioned that they are great for laying out UI components Let’s have a look at how the Anko 28 library can help build the UI of Android applications Footnote 28 https://github.com/Kotlin/anko First, let’s see how Anko wraps familiar Android APIs into a DSL-like structure The following code snippet defines an alert dialog showing a somewhat bothersome message and two options: to proceed further or to stop the operation fun Activity.showAreYouSureAlert(process: () -> Unit) { alert(title = "Are you sure? Explains high-level concepts and provides all the necessary details as well. Read on to learn more and become a Kotlin expert We hope that soon you’ll see such code in your own projects, not only in this book 1.2 Kotlin’s primary traits You probably already have an idea what kind of language Kotlin is Let’s look at its key attributes in more detail First of all, let’s see what kinds of applications you can build with Kotlin 1.2.1 Target platforms: server-side, Android, anywhere Java runs The primary goal of Kotlin is to provide a more concise, more productive, safer alternative to Java that’s suitable in all contexts where Java is used today Java is an extremely popular language, and it’s used in a broad variety of environments, from smart cards (Java Card technology) to the largest data centers run by Google, Twitter, LinkedIn, and other internet-scale companies In most of these places, using Kotlin can help developers achieve their goals with less code and fewer annoyances along the way The most common areas to use Kotlin are these: Building server-side code (typically, backends of web applications) Building mobile applications that run on Android devices But Kotlin works in other contexts as well For example, you can use the Intel MultiOS Engine to run Kotlin code on iOS devices To build desktop applications, you can use Kotlin together with JavaFX Footnote https://software.intel.com/en-us/multi-os-engine Footnote "JavaFX: Getting Started with JavaFX," Oracle, http://mng.bz/500y In addition to Java, Kotlin can also be compiled to JavaScript, allowing you to run Kotlin code in the browser But as of this writing, JavaScript support is still being explored and prototyped at JetBrains, so it’s out of scope for this book Other platforms are also under consideration for future versions of the language As you can see, Kotlin’s target is quite broad Kotlin doesn’t focus on a single problem domain or address a single type of challenge faced by software developers today Instead, it provides across-the-board productivity improvements for all tasks that come up during the development process It gives you an excellent level of integration with libraries that support specific domains or programming paradigms Let’s look next at the key qualities of Kotlin as a programming language 1.2.2 Statically typed Just like Java, Kotlin is a statically typed programming language This means the type of every expression in a program is known at compile time, and the compiler can validate that the methods and fields you’re trying to access exist on the objects you’re using This is in contrast to dynamically typed programming languages, which are represented on the JVM by, among others, Groovy and JRuby Those languages let you define variables and functions that can store or return data of any type and resolve the method and field references at runtime This allows for shorter code and greater flexibility in creating data structures But the downside is that problems like misspelled names can’t be detected during compilation and lead to runtime errors On the other hand, in contrast to Java, Kotlin doesn’t require you to specify the type of every variable explicitly in your source code In many cases, the type of a variable can automatically be determined from the context, allowing you to omit the type declaration Here’s the simplest possible example of this: val x = You’re declaring a variable, and because it’s initialized with an integer value, Kotlin automatically determines that its type is Int The ability of the compiler to determine types from context is called type inference Following are some of the benefits of static typing: Performance - Calling methods is faster because there’s no need to figure out at runtime which method needs to be called Reliability - The compiler verifies the correctness of the program, so there are fewer chances for crashes at runtime Maintainability - Working with unfamiliar code is easier because you can see what kind of objects the code is working with Tool support - Static typing enables reliable refactorings, precise code completion, and other IDE features Thanks to Kotlin’s support for type inference, most of the extra verbosity associated with static typing disappears, because you don’t need to declare types explicitly If you look at the specifics of Kotlin’s type system, you’ll find many familiar concepts Classes, interfaces, and generics work in a way very similar to Java, so most of your Java knowledge should easily transfer to Kotlin Some things are new, though The most important of those is Kotlin’s support for nullable types, which lets you write more reliable programs by detecting possible null pointer exceptions at compile time We’ll come back to nullable types later in this chapter and discuss them in detail in chapter Another new thing in Kotlin’s type system is its support for functional types To see what this is about, let’s look at the main ideas of functional programming and see how it’s supported in Kotlin 1.2.3 Functional and object-oriented As a Java developer, you’re no doubt familiar with the core concepts of object-oriented programming, but functional programming may be new to you The key concepts of functional programming are as follows: First-class functions - You work with functions (pieces of behavior) as values You can store them in variables, pass them as parameters, or return them from other functions Immutability - You work with immutable objects, which guarantees that their state can’t change after their creation No side effects - You use pure functions that return the same result given the same inputs and don’t modify the state of other objects or interact with the outside world What benefits can you gain from writing the code in the functional style?


Vintage Dress Patterns, Insert Image On All Pages In Word, Northcote Pottery Nz, Tata Tiago Xe On Road Price In Ludhiana, Chainsaw Carved Animals For Sale, El Farol Midvale,