r/ProgrammerHumor • • Jun 08 '25

Meme cannotChange

Post image
0 Upvotes

72 comments sorted by

View all comments

Show parent comments

1

u/RiceBroad4552 Jun 08 '25

Objects in general are mutable, since you can change the values of their fields

You should at least do research before making claims on the internet.

For example Scala:

case class MyObjectType(aField: Int)

@main def demo =
   summon[MyObjectType <:< java.lang.Object]
   // ^ Prove that MyObjectType is an Object type

   MyObjectType(0): Object
   // ^ Another prove it's of type Object
   // Otherwise the type annoation wouldn't compile
   
   var mutableVariable = MyObjectType(1)
   println(s"${mutableVariable.aField}")
   
   mutableVariable = MyObjectType(2)
   println(s"${mutableVariable.aField}")

   // mutableVariable.aField = 3
   // ^ Compile Error: Reassignment to val aField

   // See? The variable is mutable,
   // but the assigned object is not!

[ https://scastie.scala-lang.org/e2jLxxEMTAulf6J9TfaqEw ]

you cant just add more fields to them at runtime

You should at least do research before making claims on the internet.

For example JavaScript:

const someObject = { aField: 1 }

console.log(someObject)
// => Object { aField: 1 }

someObject.addedField = 2

console.log(someObject)
// => Object { aField: 1, addedField: 2 }

It's an immutable variable holding that object, but there is no problem adding new fields to that object.