How to make a setter private in Kotlin for non-final variables?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I need to create a data class like so
data class Cat(var toys:Int=3, val type:String):Animal(){
...
}
Animal is an abstract class with fields for eyes and legs,etc.
I don't want callers to be able to set toys directly. But I want to be able to do some work with toys such as
fun addQuota(){toys+=4}
how do I complish this cleanly? I still want a public getter, a private settter, but I don't want the variable to be final. And also, this data class is an Entity for Room.
android kotlin android-room android-architecture-components
add a comment |
I need to create a data class like so
data class Cat(var toys:Int=3, val type:String):Animal(){
...
}
Animal is an abstract class with fields for eyes and legs,etc.
I don't want callers to be able to set toys directly. But I want to be able to do some work with toys such as
fun addQuota(){toys+=4}
how do I complish this cleanly? I still want a public getter, a private settter, but I don't want the variable to be final. And also, this data class is an Entity for Room.
android kotlin android-room android-architecture-components
add a comment |
I need to create a data class like so
data class Cat(var toys:Int=3, val type:String):Animal(){
...
}
Animal is an abstract class with fields for eyes and legs,etc.
I don't want callers to be able to set toys directly. But I want to be able to do some work with toys such as
fun addQuota(){toys+=4}
how do I complish this cleanly? I still want a public getter, a private settter, but I don't want the variable to be final. And also, this data class is an Entity for Room.
android kotlin android-room android-architecture-components
I need to create a data class like so
data class Cat(var toys:Int=3, val type:String):Animal(){
...
}
Animal is an abstract class with fields for eyes and legs,etc.
I don't want callers to be able to set toys directly. But I want to be able to do some work with toys such as
fun addQuota(){toys+=4}
how do I complish this cleanly? I still want a public getter, a private settter, but I don't want the variable to be final. And also, this data class is an Entity for Room.
android kotlin android-room android-architecture-components
android kotlin android-room android-architecture-components
asked Nov 26 '18 at 21:14
salyelasalyela
698
698
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
You can do this by using the property of the constructor as backing field for the toys
property:
data class Cat(val type: String, private var toysField: Int = 3 ):Animal(){
var toys = toysField
private set(value){
toysField = value
}
fun addQuota(){toys += 4}
}
abstract class Animal
I hope this works with room.
Source https://discuss.kotlinlang.org/t/private-setter-for-var-in-primary-constructor/3640/10
thanks for replying. Your answer should definitely work, but I found a simpler solution, which I am providing as an answer as well. Thanks! +1
– salyela
Nov 26 '18 at 23:48
I finally got a chance to test this code and it actually doesn't work. Here is the version that works:var toys = toysField *n*/ private set(value){ *n*/ toysField = value *n*/ } *n*/ get()=toysField
– salyela
Nov 30 '18 at 20:50
What part does not work and how does adding the getter change that?
– leonardkraemer
Nov 30 '18 at 21:03
add a comment |
It turns out the following works:
data class Cat(private var toys:Int=3, val type:String):Animal(){
...
fun getToys()=toys
}
it works, but it's not really idiomatic. the solution withprivate set
is preferred
– Lovis
Nov 27 '18 at 12:59
Hmmm. If the simplest solution is not idiomatic then the idiom is what's wanting not the solution, wouldn't you say? Unless there is a better explanation for why I should use the convolutedtoysField
and thentoys
and thenset(value)
– salyela
Nov 27 '18 at 18:30
add a comment |
I saw that you already have found solution that satisfy you, but maybe you can consider this one:
class Cat(toys: Int = 3, val type: String) : Animal() {
var toys = toys
private set
fun addQuota() {
toys += 4
}
}
I like this one, except it doesn't work for data class
– salyela
Nov 29 '18 at 20:06
I believe the only difference betweendata
class and usual that data class have inbuilttoString
andequals
methods, which you can generate via IDE. Moreover Animal class also can't be data class
– anber
Nov 30 '18 at 7:11
Try it with room withoutdata
. Android studio still complains.
– salyela
Nov 30 '18 at 21:00
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53489171%2fhow-to-make-a-setter-private-in-kotlin-for-non-final-variables%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can do this by using the property of the constructor as backing field for the toys
property:
data class Cat(val type: String, private var toysField: Int = 3 ):Animal(){
var toys = toysField
private set(value){
toysField = value
}
fun addQuota(){toys += 4}
}
abstract class Animal
I hope this works with room.
Source https://discuss.kotlinlang.org/t/private-setter-for-var-in-primary-constructor/3640/10
thanks for replying. Your answer should definitely work, but I found a simpler solution, which I am providing as an answer as well. Thanks! +1
– salyela
Nov 26 '18 at 23:48
I finally got a chance to test this code and it actually doesn't work. Here is the version that works:var toys = toysField *n*/ private set(value){ *n*/ toysField = value *n*/ } *n*/ get()=toysField
– salyela
Nov 30 '18 at 20:50
What part does not work and how does adding the getter change that?
– leonardkraemer
Nov 30 '18 at 21:03
add a comment |
You can do this by using the property of the constructor as backing field for the toys
property:
data class Cat(val type: String, private var toysField: Int = 3 ):Animal(){
var toys = toysField
private set(value){
toysField = value
}
fun addQuota(){toys += 4}
}
abstract class Animal
I hope this works with room.
Source https://discuss.kotlinlang.org/t/private-setter-for-var-in-primary-constructor/3640/10
thanks for replying. Your answer should definitely work, but I found a simpler solution, which I am providing as an answer as well. Thanks! +1
– salyela
Nov 26 '18 at 23:48
I finally got a chance to test this code and it actually doesn't work. Here is the version that works:var toys = toysField *n*/ private set(value){ *n*/ toysField = value *n*/ } *n*/ get()=toysField
– salyela
Nov 30 '18 at 20:50
What part does not work and how does adding the getter change that?
– leonardkraemer
Nov 30 '18 at 21:03
add a comment |
You can do this by using the property of the constructor as backing field for the toys
property:
data class Cat(val type: String, private var toysField: Int = 3 ):Animal(){
var toys = toysField
private set(value){
toysField = value
}
fun addQuota(){toys += 4}
}
abstract class Animal
I hope this works with room.
Source https://discuss.kotlinlang.org/t/private-setter-for-var-in-primary-constructor/3640/10
You can do this by using the property of the constructor as backing field for the toys
property:
data class Cat(val type: String, private var toysField: Int = 3 ):Animal(){
var toys = toysField
private set(value){
toysField = value
}
fun addQuota(){toys += 4}
}
abstract class Animal
I hope this works with room.
Source https://discuss.kotlinlang.org/t/private-setter-for-var-in-primary-constructor/3640/10
answered Nov 26 '18 at 21:32
leonardkraemerleonardkraemer
3,60011634
3,60011634
thanks for replying. Your answer should definitely work, but I found a simpler solution, which I am providing as an answer as well. Thanks! +1
– salyela
Nov 26 '18 at 23:48
I finally got a chance to test this code and it actually doesn't work. Here is the version that works:var toys = toysField *n*/ private set(value){ *n*/ toysField = value *n*/ } *n*/ get()=toysField
– salyela
Nov 30 '18 at 20:50
What part does not work and how does adding the getter change that?
– leonardkraemer
Nov 30 '18 at 21:03
add a comment |
thanks for replying. Your answer should definitely work, but I found a simpler solution, which I am providing as an answer as well. Thanks! +1
– salyela
Nov 26 '18 at 23:48
I finally got a chance to test this code and it actually doesn't work. Here is the version that works:var toys = toysField *n*/ private set(value){ *n*/ toysField = value *n*/ } *n*/ get()=toysField
– salyela
Nov 30 '18 at 20:50
What part does not work and how does adding the getter change that?
– leonardkraemer
Nov 30 '18 at 21:03
thanks for replying. Your answer should definitely work, but I found a simpler solution, which I am providing as an answer as well. Thanks! +1
– salyela
Nov 26 '18 at 23:48
thanks for replying. Your answer should definitely work, but I found a simpler solution, which I am providing as an answer as well. Thanks! +1
– salyela
Nov 26 '18 at 23:48
I finally got a chance to test this code and it actually doesn't work. Here is the version that works:
var toys = toysField *n*/ private set(value){ *n*/ toysField = value *n*/ } *n*/ get()=toysField
– salyela
Nov 30 '18 at 20:50
I finally got a chance to test this code and it actually doesn't work. Here is the version that works:
var toys = toysField *n*/ private set(value){ *n*/ toysField = value *n*/ } *n*/ get()=toysField
– salyela
Nov 30 '18 at 20:50
What part does not work and how does adding the getter change that?
– leonardkraemer
Nov 30 '18 at 21:03
What part does not work and how does adding the getter change that?
– leonardkraemer
Nov 30 '18 at 21:03
add a comment |
It turns out the following works:
data class Cat(private var toys:Int=3, val type:String):Animal(){
...
fun getToys()=toys
}
it works, but it's not really idiomatic. the solution withprivate set
is preferred
– Lovis
Nov 27 '18 at 12:59
Hmmm. If the simplest solution is not idiomatic then the idiom is what's wanting not the solution, wouldn't you say? Unless there is a better explanation for why I should use the convolutedtoysField
and thentoys
and thenset(value)
– salyela
Nov 27 '18 at 18:30
add a comment |
It turns out the following works:
data class Cat(private var toys:Int=3, val type:String):Animal(){
...
fun getToys()=toys
}
it works, but it's not really idiomatic. the solution withprivate set
is preferred
– Lovis
Nov 27 '18 at 12:59
Hmmm. If the simplest solution is not idiomatic then the idiom is what's wanting not the solution, wouldn't you say? Unless there is a better explanation for why I should use the convolutedtoysField
and thentoys
and thenset(value)
– salyela
Nov 27 '18 at 18:30
add a comment |
It turns out the following works:
data class Cat(private var toys:Int=3, val type:String):Animal(){
...
fun getToys()=toys
}
It turns out the following works:
data class Cat(private var toys:Int=3, val type:String):Animal(){
...
fun getToys()=toys
}
answered Nov 26 '18 at 23:19
salyelasalyela
698
698
it works, but it's not really idiomatic. the solution withprivate set
is preferred
– Lovis
Nov 27 '18 at 12:59
Hmmm. If the simplest solution is not idiomatic then the idiom is what's wanting not the solution, wouldn't you say? Unless there is a better explanation for why I should use the convolutedtoysField
and thentoys
and thenset(value)
– salyela
Nov 27 '18 at 18:30
add a comment |
it works, but it's not really idiomatic. the solution withprivate set
is preferred
– Lovis
Nov 27 '18 at 12:59
Hmmm. If the simplest solution is not idiomatic then the idiom is what's wanting not the solution, wouldn't you say? Unless there is a better explanation for why I should use the convolutedtoysField
and thentoys
and thenset(value)
– salyela
Nov 27 '18 at 18:30
it works, but it's not really idiomatic. the solution with
private set
is preferred– Lovis
Nov 27 '18 at 12:59
it works, but it's not really idiomatic. the solution with
private set
is preferred– Lovis
Nov 27 '18 at 12:59
Hmmm. If the simplest solution is not idiomatic then the idiom is what's wanting not the solution, wouldn't you say? Unless there is a better explanation for why I should use the convoluted
toysField
and then toys
and then set(value)
– salyela
Nov 27 '18 at 18:30
Hmmm. If the simplest solution is not idiomatic then the idiom is what's wanting not the solution, wouldn't you say? Unless there is a better explanation for why I should use the convoluted
toysField
and then toys
and then set(value)
– salyela
Nov 27 '18 at 18:30
add a comment |
I saw that you already have found solution that satisfy you, but maybe you can consider this one:
class Cat(toys: Int = 3, val type: String) : Animal() {
var toys = toys
private set
fun addQuota() {
toys += 4
}
}
I like this one, except it doesn't work for data class
– salyela
Nov 29 '18 at 20:06
I believe the only difference betweendata
class and usual that data class have inbuilttoString
andequals
methods, which you can generate via IDE. Moreover Animal class also can't be data class
– anber
Nov 30 '18 at 7:11
Try it with room withoutdata
. Android studio still complains.
– salyela
Nov 30 '18 at 21:00
add a comment |
I saw that you already have found solution that satisfy you, but maybe you can consider this one:
class Cat(toys: Int = 3, val type: String) : Animal() {
var toys = toys
private set
fun addQuota() {
toys += 4
}
}
I like this one, except it doesn't work for data class
– salyela
Nov 29 '18 at 20:06
I believe the only difference betweendata
class and usual that data class have inbuilttoString
andequals
methods, which you can generate via IDE. Moreover Animal class also can't be data class
– anber
Nov 30 '18 at 7:11
Try it with room withoutdata
. Android studio still complains.
– salyela
Nov 30 '18 at 21:00
add a comment |
I saw that you already have found solution that satisfy you, but maybe you can consider this one:
class Cat(toys: Int = 3, val type: String) : Animal() {
var toys = toys
private set
fun addQuota() {
toys += 4
}
}
I saw that you already have found solution that satisfy you, but maybe you can consider this one:
class Cat(toys: Int = 3, val type: String) : Animal() {
var toys = toys
private set
fun addQuota() {
toys += 4
}
}
answered Nov 29 '18 at 15:47
anberanber
1,41932550
1,41932550
I like this one, except it doesn't work for data class
– salyela
Nov 29 '18 at 20:06
I believe the only difference betweendata
class and usual that data class have inbuilttoString
andequals
methods, which you can generate via IDE. Moreover Animal class also can't be data class
– anber
Nov 30 '18 at 7:11
Try it with room withoutdata
. Android studio still complains.
– salyela
Nov 30 '18 at 21:00
add a comment |
I like this one, except it doesn't work for data class
– salyela
Nov 29 '18 at 20:06
I believe the only difference betweendata
class and usual that data class have inbuilttoString
andequals
methods, which you can generate via IDE. Moreover Animal class also can't be data class
– anber
Nov 30 '18 at 7:11
Try it with room withoutdata
. Android studio still complains.
– salyela
Nov 30 '18 at 21:00
I like this one, except it doesn't work for data class
– salyela
Nov 29 '18 at 20:06
I like this one, except it doesn't work for data class
– salyela
Nov 29 '18 at 20:06
I believe the only difference between
data
class and usual that data class have inbuilt toString
and equals
methods, which you can generate via IDE. Moreover Animal class also can't be data class– anber
Nov 30 '18 at 7:11
I believe the only difference between
data
class and usual that data class have inbuilt toString
and equals
methods, which you can generate via IDE. Moreover Animal class also can't be data class– anber
Nov 30 '18 at 7:11
Try it with room without
data
. Android studio still complains.– salyela
Nov 30 '18 at 21:00
Try it with room without
data
. Android studio still complains.– salyela
Nov 30 '18 at 21:00
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53489171%2fhow-to-make-a-setter-private-in-kotlin-for-non-final-variables%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown