What is the kotlin version of out keyword for arguments?

as far as I know, the arguments can’t be declared out’s because they are val’s, hence readonly, but is there a substitute method for the out keyword in java for kotlin other than writing wrappers for every class? Also, if the only way to overcome this is writing wrappers, wouldn’t it better to just integrate it into the language at this point?

I’m not sure there is much use for out (aka, output parameters) in Kotlin like there is in C#.

I’m not very familiar with C# so forgive me if I’m misrepresenting it.
If I understand correctly, the popular uses for out in C# are:

  1. To avoid checked exceptions and being forced to write try-catch blocks. Kotlin doesn’t have checked exceptions.
  2. To return multiple values from a method instead of making another container. In Kotlin we have data classes and value classes. Microsoft says this is bad in their guidance
  3. Some other reason I’m missing?

For those coming from Java, output parameters are a major code smell. Unlike C++ where APIs often use output parameters, Java rarely does. In Clean Code by Robert Martin, there are some good insights into output parameters. Even Microsoft puts caution into using out (see Avoid using out).

Over the years of language evolution, I think it’s safe to say that the consensus of “output params” is that they are to be avoided when possible, and they might be a good target for eliminating the need for them in your language (for a language designer).

Maybe you could describe what you’re trying to do with out in Kotlin code? There’s a chance an alternative exists that might get you what you want :slight_smile:

1 Like

+1 for saying out params is a very bad pattern. Functions have clearly defined input and outputs. Just don’t use inputs as they are outputs. So no, don’t use wrappers. Just return the value you need.

Out params make sense on low-level languages like C/C++, because it allows to pass data efficiently.


Why would you even need a wrapper for every class? You can have a generic ValueHolder<T> class which works for every type. Still this is a very bad idea and probably more typing still than returning an appropriate data class.

1 Like

the thing is, i have some optional outputs from a function, like the the result of the calculation of some other thing that gets calculated while the function is being processed

Cool! This is a more valuable question :slight_smile: Are those outputs optionally calculated or are they always calculated and optionally retrieved by the caller?

I’d still say output params are not a good answer for returning optional values (and according to Microsoft, is something you want to avoid in C# just as much as in Kotlin).

One solution is to return the calculation result in a container of some sort with all of the values. This means instead of wrapping the inputs you return a data class of results.
Alternatively, you could have your functions work within a contextual class that holds the state (ins/outs) of the calculation.

The nice thing is this kind of question is a common problem when designing functions and you’re not alone. The more info into what you’re trying to do the easier it is to suggest the best patterns. Output params and passing by reference will be close to last on the suggestion list :wink:

1 Like

whether or not it’s calculated really depends, like in one algorithm, I sometimes might want to output some of the variables that are used, then some other time i might calculate it if the given argument != null, or I can just create a data class or a wrapper for being able to modify primitives like @al3c suggested

Just create a data class with all your mandatory and optional data and return it from the function. This is much easier to follow by anyone who use this function.