Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

261
Visualizações
Kotlin equivalent for "with" keyword in C# (data class)

In C# there is a kind of class that is called "record" which is more or less the same as a "data" class in Kotlin.

When using a record in C# you can use the keyword "with" to create a new instance of the your record with some properties set to specific values (see https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/with-expression )

I was wondering if there is a similar way to do it in kotlin ? I cannot find anything regarding that, and the way I do it for now is by defining function that do the job, but it can be kind of boilerplate sometimes, and using data class is supposed to avoid me that boilerplate work.

Also I'd prefer to avoid using "var" properties (to have immutable instances), hence my question.

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

Kotlin has Data classes, that are close to C# records. As you can see from the Kotlin documentation:

It is not unusual to create classes whose main purpose is to hold data. In such classes, some standard functionality and some utility functions are often mechanically derivable from the data. In Kotlin, these are called data classes and are marked with data:

data class User(val name: String, val age: Int)

The compiler automatically derives the following members from all properties declared in the primary constructor:

equals()/ hashCode() pair

toString() of the form "User(name=John, age=42)"

componentN() functions corresponding to the properties in their order of declaration.

copy() function (see below).

The last line shows that the compiler automatically generates a copy function for a Data class, and this is what you are looking for

Again from the documentation, the usage would be:

Use the copy() function to copy an object, allowing you to alter some of its properties while keeping the rest unchanged. [...] You can then write the following:

val jack = User(name = "Jack", age = 1)
val olderJack = jack.copy(age = 2)
over 4 years ago · Santiago Trujillo Relatório

0

With a data class, you can use the copy method:

val someData = SomeClass(a = 1, b = 2)
val modifiedData = someData.copy(b = 0) // modifiedData = SomeClass(a = 1, b = 0)

See the official data class documentation.

over 4 years ago · Santiago Trujillo Relatório

0

Do you mean something like copy() function that is auto-generated for data classes?

fun main() {
    val foo1 = Foo("foo", 5)
    val foo2 = foo1.copy(value1 = "bar")
    val foo3 = foo2.copy(value2 = 10)
}

data class Foo(
    val value1: String,
    val value2: Int,
)
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda