In the case of kotlin, rust and many other programming languages...

There are variables with direct implicit type declarations... Where you can see the type of a variable at the same line where it's defined and initialized.

val a = 1

if you change this code to this...

val a = 1.5f

the change can be easily addressed down the function where this variable is used...

Now if we look at this code...

fun f(): <first type> {
    return ...

val d = f()
val e = f2()

and if you change return type of function to this...

fun f(): <other type compatible with first type> {
    return ...

val d = f()   <--- Still compiles but the change is not being addressed by the programmer.
val e = f2()

All variables that store return type from the function and are defined with indirect implicit type declaration will not be addressed by the programmer and this will result in a vulnerable, fragile, or even dangerous codebase over time...

My question is similar to:

But my problem is about the indirect implicit variable declarations where you store the return type of a function to a variable with an implicit declaration which I think is dangerous and the compiler should not allow this kind of implicit declaration.

What are you guys thinking? Should be this problem addressed on the compiler level or I'm overreacting?


1 Answer 1


“Second level” isn’t really a thing. d and e are type Any in your examples. This becomes apparent when f becomes polymorphic or its implementation is in a dll. If the compiler can’t see the implementation, it can’t infer the return type.

But when the compiler can see the implementation (function bodies, generally) it can do all sorts of good inference across many different operations, including stuff like “this is an int or a string” or “this is something with a .Name”.

  • Thank you for your reply, but unfortunately, you didn't address the case when the function return type changes after some time, and the change stays hidden and unaddressed in the codebase because the programmer uses a variable with implicit type declaration where he assigns the function return value to the variable.
    – user426780
    Feb 11 at 6:58
  • I have updated my question so that it's more clear about the problem that is bothering me, thank you for you help.
    – user426780
    Feb 11 at 7:09
  • 1
    @UrošJarc - that is because you edited it after my answer… if you change the return type of a function, then the compiler will infer a different type for the implicit variable (usually; there are cases where the compiler might infer an interface type and changing the function return type has no effect). I don’t think there is much risk here. If there is a type mismatch after the change, the compiler will still warn you. It won’t be hidden.
    – Telastyn
    Feb 11 at 16:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.