<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Raji ac</title>
    <description>The latest articles on DEV Community by Raji ac (@raji_ac_547dee39c0395e6bf).</description>
    <link>https://dev.to/raji_ac_547dee39c0395e6bf</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3290123%2F050672eb-5e89-4147-8175-f00c987a1fcd.jpg</url>
      <title>DEV Community: Raji ac</title>
      <link>https://dev.to/raji_ac_547dee39c0395e6bf</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/raji_ac_547dee39c0395e6bf"/>
    <language>en</language>
    <item>
      <title>[Boost]</title>
      <dc:creator>Raji ac</dc:creator>
      <pubDate>Thu, 21 Aug 2025 06:23:01 +0000</pubDate>
      <link>https://dev.to/raji_ac_547dee39c0395e6bf/-2077</link>
      <guid>https://dev.to/raji_ac_547dee39c0395e6bf/-2077</guid>
      <description>&lt;div class="ltag__link"&gt;
  &lt;a href="/raji_ac_547dee39c0395e6bf" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__pic"&gt;
      &lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3290123%2F050672eb-5e89-4147-8175-f00c987a1fcd.jpg" alt="raji_ac_547dee39c0395e6bf"&gt;
    &lt;/div&gt;
  &lt;/a&gt;
  &lt;a href="https://dev.to/raji_ac_547dee39c0395e6bf/kotlingenerics-questions-228c" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__content"&gt;
      &lt;h2&gt;Kotlin:'Generics' Questions&lt;/h2&gt;
      &lt;h3&gt;Raji ac ・ Aug 19&lt;/h3&gt;
      &lt;div class="ltag__link__taglist"&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/a&gt;
&lt;/div&gt;


</description>
      <category>kotlin</category>
      <category>programming</category>
      <category>softwaredevelopment</category>
    </item>
    <item>
      <title>Kotlin:'Generics' Questions</title>
      <dc:creator>Raji ac</dc:creator>
      <pubDate>Tue, 19 Aug 2025 21:00:09 +0000</pubDate>
      <link>https://dev.to/raji_ac_547dee39c0395e6bf/kotlingenerics-questions-228c</link>
      <guid>https://dev.to/raji_ac_547dee39c0395e6bf/kotlingenerics-questions-228c</guid>
      <description>&lt;p&gt;In this blog post, we'll tackle some interesting coding questions related to Kotlin generics. But first, let's quickly recap the fundamentals.&lt;/p&gt;

&lt;p&gt;Generics in Kotlin allow us to write classes, interfaces, and functions with a placeholder for a type, so as to create reusable and type-safe code that can work with different types.&lt;/p&gt;

&lt;p&gt;Kotlin has &lt;strong&gt;declaration-site variance&lt;/strong&gt;(with the generic class or interface's declaration) and &lt;strong&gt;use-site variance, aka type projections&lt;/strong&gt;(in function signature).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;out&lt;/strong&gt; - keyword used to mention the type is a producer(can return values of type T).&lt;br&gt;
&lt;strong&gt;in&lt;/strong&gt; - used to mention the type is a consumer(can accept values of type T).&lt;br&gt;
&lt;strong&gt;where&lt;/strong&gt; - used to indicate that the type has more than one constraint.&lt;br&gt;
&lt;strong&gt;reified&lt;/strong&gt; - to preserve the type information at runtime, facilitates safe type-check with 'is' and 'as' at runtime.&lt;/p&gt;

&lt;p&gt;** Any? is the super type of all types.&lt;br&gt;
** Nothing is the ultimate subtype of all types.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;interface Source&amp;lt;out T&amp;gt; {
    fun nextT(): T
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Covariance&lt;/strong&gt;:- declared with the keyword 'out', denotes that the type parameter T is a covariant parameter or the class is covariant in the parameter 'T', or C can safely be a supertype of C, or as in Java -  C&amp;lt;? extends Base&amp;gt;.&lt;br&gt;
eg:- List&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;interface Comparable&amp;lt;in T&amp;gt; {
    operator fun compareTo(other: T): Int
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Contravariance&lt;/strong&gt;:- In contrast to 'out', 'in' makes the type contravariant. C can be assigned to C&lt;br&gt;
eg:-Comparable&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Invariance&lt;/strong&gt;:- Some classes accepts and produces types, they can be neither co- nor contravariant in T&lt;br&gt;
eg:- Array&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Type Erasure&lt;/strong&gt;:- At runtime, the instance of the generic class does not hold any information about the type of parameter T, this is to ensure interoperability with old JVMs.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Star Projections&lt;/strong&gt;:- Solves the problem of type safety when a generic type is unknown.&lt;/p&gt;

&lt;p&gt;for &lt;code&gt;interface Function&amp;lt;in T, out U&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Function&amp;lt;*, String&amp;gt;&lt;/code&gt; means &lt;code&gt;Function&amp;lt;in Nothing, String&amp;gt;&lt;/code&gt;.&lt;br&gt;
&lt;code&gt;Function&amp;lt;Int, *&amp;gt;&lt;/code&gt; means &lt;code&gt;Function&amp;lt;Int, out Any?&amp;gt;&lt;/code&gt;.&lt;br&gt;
&lt;code&gt;Function&amp;lt;*, *&amp;gt;&lt;/code&gt; means &lt;code&gt;Function&amp;lt;in Nothing, out Any?&amp;gt;&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;** To declare a generic type T as definitely non-nullable, declare the type with '&amp;amp; Any' (T &amp;amp; Any)&lt;br&gt;
** Underscore(_) can be used for type arguments to automatically infer the type of the argument when other types are explicitly specified.&lt;/p&gt;



&lt;p&gt;Now we know enough, let's dive in.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Q:&lt;/strong&gt;  Why isn't this allowed?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Box&amp;lt;T&amp;gt;(val value: T)

fun takeBox(box: Box&amp;lt;Any&amp;gt;) { println(box.value) }

fun main() {
    val strBox = Box("Hello")
    takeBox(strBox) 
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Answer:&lt;/strong&gt; &lt;br&gt;
 Generics are invariant by default. An &lt;code&gt;Int&lt;/code&gt; is a subtype of &lt;code&gt;Any&lt;/code&gt;, but a &lt;code&gt;Box&amp;lt;Int&amp;gt;&lt;/code&gt; is not a subtype of &lt;code&gt;Box&amp;lt;Any&amp;gt;&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Q:&lt;/strong&gt;  What would be the output?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fun &amp;lt;T&amp;gt; isString(value: T): Boolean {
    return value is String
}

fun main() {
    println(isString("Hello"))
    println(isString(42))
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Answer:&lt;/strong&gt; &lt;br&gt;
true&lt;br&gt;
false&lt;br&gt;
Even though the type information of T is erased to be available at runtime, &lt;code&gt;value&lt;/code&gt; is checked against the concrete type &lt;code&gt;String&lt;/code&gt;, thus &lt;code&gt;is&lt;/code&gt; results in &lt;code&gt;true&lt;/code&gt; for a &lt;code&gt;String&lt;/code&gt; and &lt;code&gt;false&lt;/code&gt; for an &lt;code&gt;Int&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Q:&lt;/strong&gt;  What does this mean?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fun &amp;lt;T&amp;gt; onlyNumbers(a: T, b: T): T
        where T : Number, T : Comparable&amp;lt;T&amp;gt; {
    return if (a &amp;gt; b) a else b
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Answer:&lt;/strong&gt;&lt;br&gt;
Both parameters should be a &lt;code&gt;Number&lt;/code&gt;. &lt;code&gt;where&lt;/code&gt; is used to indicate multiple constraints, where &lt;code&gt;T&lt;/code&gt; is mentioned to be a subtype of &lt;code&gt;Number&lt;/code&gt;, also implement &lt;code&gt;Comparable&lt;/code&gt; interface and be able to compare itself to another instance of its own type, &lt;code&gt;T&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Q:&lt;/strong&gt;  Why wouldn't it compile?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fun copy(first: Array&amp;lt;out Any&amp;gt;, second: Array&amp;lt;Any&amp;gt;) {
    for (i in first.indices) {
        first[i] = second[i]
    }
}

fun main() {
    val strings = arrayOf("A", "B", "C")
    val anys = Array&amp;lt;Any&amp;gt;(3) { "" }
    copy(strings, anys) 
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Answer:&lt;/strong&gt;&lt;br&gt;
 &lt;code&gt;out&lt;/code&gt; keyword on &lt;code&gt;first&lt;/code&gt; parameter type makes it a producer only; it cannot accept a value.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Q:&lt;/strong&gt;  Will this compile?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Box&amp;lt;T&amp;gt;(val value: T) {
    fun &amp;lt;T&amp;gt; printTwice(t: T) {
        println("$value $t")
    }
}

fun main() {
    val box = Box("Kotlin")
    box.printTwice(42)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Answer:&lt;/strong&gt;&lt;br&gt;
Yes. It will print &lt;code&gt;Kotlin 42&lt;/code&gt;, because the type parameter T of the class Box is shadowed in the function &lt;code&gt;printTwice&lt;/code&gt;, here it is considered as a new type.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Q:&lt;/strong&gt;  Will this code block compile?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fun fill(list: MutableList&amp;lt;out Number&amp;gt;) {
    list.add(42) 
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Answer:&lt;/strong&gt;&lt;br&gt;
It will not, since the type parameter of the argument &lt;code&gt;list&lt;/code&gt; is denoted with the &lt;code&gt;out&lt;/code&gt; keyword, which means &lt;code&gt;list&lt;/code&gt; is a producer of &lt;code&gt;Number&lt;/code&gt;, and it can not accept.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Q:&lt;/strong&gt;  Why wouldn't this work?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fun mystery(list: MutableList&amp;lt;*&amp;gt;) {
    list.add(null)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Answer:&lt;/strong&gt; &lt;br&gt;
&lt;code&gt;MutableList&amp;lt;*&amp;gt;&lt;/code&gt; is a star projection, which means &lt;code&gt;MutableList&amp;lt;out Any?&amp;gt;&lt;/code&gt;; it is a producer of &lt;code&gt;Any?&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Q:&lt;/strong&gt;  Why is this not allowed?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fun &amp;lt;T : Number&amp;gt; sum(a: T, b: T): T {
    return a + b 
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Answer:&lt;/strong&gt;&lt;br&gt;
The operator function &lt;code&gt;plus&lt;/code&gt; is not defined in &lt;code&gt;Number&lt;/code&gt;, but in its subtypes such as &lt;code&gt;Int&lt;/code&gt;, &lt;code&gt;Double&lt;/code&gt;, etc. To make it work, these two &lt;code&gt;Number&lt;/code&gt; types will have to be converted to their subtypes. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Q:&lt;/strong&gt;  What will be the output?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;inline fun &amp;lt;reified T&amp;gt; check(value: Any) {
    println(value is T)
}

fun main() {
    check&amp;lt;List&amp;lt;String&amp;gt;&amp;gt;(listOf("a", "b"))
    check&amp;lt;List&amp;lt;Int&amp;gt;&amp;gt;(listOf("a", "b"))
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Answer:&lt;/strong&gt;&lt;br&gt;
true&lt;br&gt;
true&lt;br&gt;
Even though &lt;code&gt;reified&lt;/code&gt; is mentioned, only the top-level type &lt;code&gt;List&lt;/code&gt; class is preserved; it can't overcome the JVM's fundamental type erasure for the arguments inside the angled brackets. In both cases, &lt;code&gt;T&lt;/code&gt; is only a &lt;code&gt;List&lt;/code&gt; type.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Q:&lt;/strong&gt;  Why is this not allowed?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;object Cache&amp;lt;T&amp;gt; {  
    private val items = mutableListOf&amp;lt;T&amp;gt;()
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Answer:&lt;/strong&gt;&lt;br&gt;
Because &lt;code&gt;object&lt;/code&gt; creates a singleton, it cannot have a different value, so generics are not allowed.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Q:&lt;/strong&gt;  Why do we need this?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;interface ComparableSelf&amp;lt;T : ComparableSelf&amp;lt;T&amp;gt;&amp;gt; {
    fun compareTo(other: T): Int
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Answer:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This enforces the classes that implement this interface to have the method &lt;code&gt;compareTo&lt;/code&gt;, where it should accept its own type only; in short, it enforces a strict comparable.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Q:&lt;/strong&gt;  Is this a correct declaration?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Box&amp;lt;T : Int&amp;gt;(val value: T)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Answer:&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;Int&lt;/code&gt; is a final type, so the value of the type parameter is predetermined; it is enough to write &lt;code&gt;class Box(val value: Int)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Q:&lt;/strong&gt;  What will be the output?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fun &amp;lt;T&amp;gt; printType(value: T) {
    println("Generic")
}

fun printType(value: String) {
    println("String")
}

fun main() {
    printType("Hello")
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Answer:&lt;/strong&gt;&lt;br&gt;
String&lt;/p&gt;

&lt;p&gt;The Kotlin compiler selects the most specific function that matches the provided arguments.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Q:&lt;/strong&gt;  What will be the output?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;inline fun &amp;lt;reified T&amp;gt; printClass(list: List&amp;lt;T&amp;gt;) {
    println(T::class.java)
}

fun main() {
    printClass(listOf("A", "B"))
    printClass(listOf(1, 2, 3))
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Answer:&lt;/strong&gt;&lt;br&gt;
class java.lang.String&lt;br&gt;
class java.lang.Integer&lt;br&gt;
&lt;code&gt;reified&lt;/code&gt; makes the type &lt;code&gt;T&lt;/code&gt; preserved.&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
