Last active
March 18, 2022 22:36
-
-
Save yenerm/8534d7adcc6a3f7b4790b710acf56d22 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- 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 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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()
tofield = value.toLowerCase().capitalize()
and
lastname = value.toLowerCase().capitalize()
tofield = value.toLowerCase().capitalize()