Questions tagged [kotlin-inline-class]

From version 1.3+, Kotlin introduces a special kind of class called an inline class, which is declared by placing an 'inline' modifier before the name of the class. They provide a way to wrap a type, thus adding functionality and creating a new type by itself. As opposed to regular wrappers, they will benefit from improved performance. This happens because the data is inlined into its usages, and object instantiation is skipped in the compiled code.

Inline classes provide us with a way to wrap a type, thus adding functionality and creating a new type by itself.

As opposed to regular (non-inlined) wrappers, they will benefit from improved performance. This happens because the data is inlined into its usages, and object instantiation is skipped in the resulting compiled code.

17 questions
8
votes
1 answer

Unresolved reference: JvmInline In kotlin playground

This is an exact code snippet of value classes taken from kotlin official website. interface I @JvmInline value class Foo(val i: Int) : I fun asInline(f: Foo) {} fun asGeneric(x: T) {} fun asInterface(i: I) {} fun asNullable(i: Foo?) {} fun…
Sourav Kannantha B
  • 2,860
  • 1
  • 11
  • 35
8
votes
2 answers

Kotlin methods accepting inline classes as parameters - how access from Java?

Let's say I have: inline class Email(value: String) and fun something(email: Email) now if I want to call something() from Java I can't. Because any method that is accepting an inline class as a parameter is "mangled" (more about this here:…
WindRider
  • 11,958
  • 6
  • 50
  • 57
7
votes
3 answers

Kotlin inline class for @Id-annotated properties

In my business logic I have to deal with a lot of entity IDs, all of them of type String, which can cause confusion especially when you pass a couple of them as method parameters. So I thought about introducing a little type safety with inline…
Jan B.
  • 6,030
  • 5
  • 32
  • 53
4
votes
2 answers

Parcelize value (inline) class in Kotlin

At first glance it seems to be okay to decorate a kotlin value (inline) class like that: @JvmInline @Parcelize value class TestClass(val value: Long) : Parcelable But the build fails reporting: error: unexpected type public static final…
Casaflowa
  • 43
  • 4
4
votes
2 answers

Store Kotlin inline class to MongoDB with Spring Data

I'm trying to store a Kotlin's (v1.3.61) inline class to a MongoDB using Spring Data MongoDB (2.2.3-RELEASE), with no luck so far. This is the set up: inline class UserId(@NotBlank val id: String) and @Document(collection = "data") class Data( …
Jan B.
  • 6,030
  • 5
  • 32
  • 53
3
votes
1 answer

Kotlin inline class that implements interface under the hood

In kotlin vesion 1.3.0 inline classes were made available for developers. "Normal" inline classes are promised not to be a real objects at runtime but exist only at compile time for type checking. So for instance inline class MyWrapper(val…
GV_FiQst
  • 1,487
  • 14
  • 30
2
votes
1 answer

Kotlin inline class in JUnit tests

I am trying to understand concept of inline classes - they are a simple object wrapper of single property that is being inlined during runtime. That means, that the actual initialization of the class is not happening at runtime I was trying to write…
K.Os
  • 5,123
  • 8
  • 40
  • 95
1
vote
1 answer

Force inlining of Kotlin inline-class?

@LukasEder, this question is more Kotlin related, you can probably skip it in favour of the more jOOQ-related continuation in https://github.com/jOOQ/jOOQ/issues/14972 :) So part of a legacy database has a pretty over-engineered design, but we…
User1291
  • 7,664
  • 8
  • 51
  • 108
1
vote
1 answer

What is the best use case to understand crossinline

I wanted to understand what is actual use case when we would want to add crossinline keyword to a lambda I have following code where I added crossinline. I understand that crossinline will force any of the calling method to force it to local-return…
Hack123
  • 95
  • 7
1
vote
2 answers

Cannot use 'T' as reified type parameter even I declared that T should be any class inherited by Parcelable

Since I am not yet good in generics I would like to ask: Question: why I cannot inform the function getResult that the return value from bundle.getParcelableCompat(BUNDLE_KEY) would be type of T (which is any class inheriting by Parcelable)? I…
deadfish
  • 11,996
  • 12
  • 87
  • 136
1
vote
1 answer

Kotlin: inline function and shared type

I want to call multiple API calls at once using coroutines. So far I used coroutines only to call single API call which returned result in form off success or error. Its using inline function. But I need to understand how to use this inline function…
martin1337
  • 2,384
  • 6
  • 38
  • 85
1
vote
0 answers

Value classes in generics - interoperability with Java/Groovy

I’m struggling with value classes in generics and interoperability with Java or Groovy. Value class are inlined: Inline classes | Kotlin 1 except for generics. Given following value class and interface (Kotlin): @JvmInline value class ValueClass(val…
1
vote
1 answer

Is it possible to make safe inline Optional in Kotlin?

In Kotlin sometimes I have to work with double nullability. For example, I need double nullability, when I want to use T? where T may be a nullable type. There are a few approaches for doing this: Holder? where Holder is data class Holder
IlyaMuravjov
  • 2,352
  • 1
  • 9
  • 27
0
votes
1 answer

Kotlin working with value classes in libraries such as hibernate / jackson

Is there a way to get kotlin value classes (updated inline classes) to work with libraries such as Jackson and Hibernate and have them treat the value class simply as the underlying type? Currently I get a bunch of errors saying that there is not…
Vidde
  • 11
  • 1
  • 2
0
votes
0 answers

Is it possible to disable inlining of value classes in Kotlin?

Goal I would like to globally disable inlining of @JvmInline value class classes via a compiler flag or something similar. I would want to do this when running unit tests but not in production. Motivation I would like to use mockk with value…
dta
  • 654
  • 4
  • 19
1
2