Skip to content

Instantly share code, notes, and snippets.

@yenerm
Last active March 18, 2022 22:36
Show Gist options
  • Save yenerm/8534d7adcc6a3f7b4790b710acf56d22 to your computer and use it in GitHub Desktop.
Save yenerm/8534d7adcc6a3f7b4790b710acf56d22 to your computer and use it in GitHub Desktop.
<!-- Copyright 2019 Google LLC.
SPDX-License-Identifier: Apache-2.0 -->
class Person(name: String, lastname: String) {
var name: String = name
set(value) {
field = value.toLowerCase().capitalize()
updateCount++
}
var lastname: String = lastname
set(value) {
field = value.toLowerCase().capitalize()
updateCount++
}
var updateCount = 0
}
@dturner
Copy link

dturner commented Feb 15, 2022

When setting either properties, the setter methods will be called recursively (resulting in a runtime error) because they attempt to write to the member, rather than the backing field.

To fix this change:

name = value.toLowerCase().capitalize() to field = value.toLowerCase().capitalize()

and

lastname = value.toLowerCase().capitalize() to field = value.toLowerCase().capitalize()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment