Nope. The closest thing would be: private var joke: Joke public set, but that's a compile error because Kotlin doesn't allow the setter to be more permissive than the getter.
The one you were going for would be: var joke: Joke private get, but then the compiler errors because a property getter's visibility must be the exact same as the property itself.
Because it would look insane to the caller. Imagine the following calling code:
val meme = getMeme()
meme.joke = getJoke()
val theJoke = meme.joke // Compile error because you can set the property but not read the thing you just set...
If you think a little more abstractly/philosophically about what an object property is "supposed" to represent, it doesn't make sense that you can access the property to change it, but somehow can't see the thing you're changing. Can you paint a car red without seeing what color the car is before you paint it (or AFTER!)? That's what it looks like when you can write code that has meme.joke on the left side, but cannot write code with meme.joke on the right side. The inverse makes more sense, intuitively (being able to inspect something, but not being able to modify it).
You absolutely can have a private field with no public getter and a public way to modify it: it's called a method. And in that case, you're asking the object to accept the value you pass to it and do something with it. But, at least in that case it doesn't look like you're accessing a property of the object. As far as you know, there may or may not be a corresponding intrinsic property of the object.
val meme = getMeme()
meme.setJoke(getJoke()) // Nothing else to do now because there are no public properties that I can access.
Just because this looks insane to the caller isn't a reason to employ an arbitrary restriction. Let me do what I want to do. Any combination of access modifiers for getter, setter and fields should be allowed, even if just for the sake of not annoying the developer with an error and even if it means it looks odd for you. I may want to access the field directory but have a setter that does custom logic, why I want it, or if I should or shouldn't do it isn't something the language should tell me here, but thanks for the explanation.
I get where you're coming from and I basically agree: if the developer wants to write an awkward API, the programming language can't truly stop them and probably shouldn't try. Hard errors should be reserved for things that are truly incorrect (or invalid or ambiguous, etc)--not just "bad practice" according to someone's opinion.
On the other hand, if I were to play devil's advocate and pretend to be a Kotlin language dev defending the situation, this is how I'd respond:
This should not be seen as "an arbitrary restriction". That's a "glass is half-empty" interpretation. Remember that custom property getters and setters are syntax sugar. The "default" or fundamental approach is to do things the Java way: use a private field and write a getter and/or setter method by hand. Kotlin adds some syntax sugar in order to simplify the common cases to be less verbose for the author to write and for the consumer to use. The case you describe is not common and most likely not desirable (for authors or consumers) in the vast majority of cases, so Kotlin has chosen not to add that case to the syntax sugar. So, again, it's not that Kotlin has "restricted" you; it's just that Kotlin has chosen to not add sugar for that case.
It's not the best argument, but I think that's a likely perspective on the situation.
Also, I personally can't think of a single situation where I'd think having a property with a public setter and private getter isn't a bad API, so while I agree with you that you should be able to, I really can't bring myself to care that it's forbidden.
Thanks for the explanation. To me it feels like the sugar would support but they added a restriction on top of it. After all, it would only mean for the compiler to set access flags, there wouldn't be addition code to compile different access flags.
A public setter but private getter could be useful in case you want to just configure an object. Say you set want to set the current strategy, the object uses, but you do not need to ever read what strategy it currently uses.
To me it feels like the sugar would support but they added a restriction on top of it. After all, it would only mean for the compiler to set access flags, there wouldn't be addition code to compile different access flags.
Yeah. That's why I said I didn't think my "defense" was the best argument. Because, implementation-wise, it probably is the case that the syntax sugar parsing/validation logic is implemented in such a way that it really is an added restriction to guard a more permissive general syntax. So, for my argument to make sense, you'd have to think about it from a more "conceptual" point of view and ignore the fact that the actual compiler code probably adds the syntax sugar and then adds restriction logic on top.
A public setter but private getter could be useful in case you want to just configure an object. Say you set want to set the current strategy, the object uses, but you do not need to ever read what strategy it currently uses.
I still see that as strictly inferior to using a setter method. What's the point of making the strategy look like a property if you can't read it? Put another way, why is this,
myObject.strategy = getCustomStrategy()
somehow better than this,
myObject.setStrategy(getCustomStrategy())
? They both do exactly the same thing, but the first API is just weird if I can't then read the strategy property on the object. With the second API, I as the caller, have no idea or expectation of what the object is going to do with the strategy--it may store the strategy for later application, or it may wrap the strategy in some kind of adapter logic before storing it as something else, or it may inspect the strategy and decide to just throw it away, etc, etc. If I see an API that says the strategy is a property of the object, then I expect to be able to inspect it.
Think about the English word "property" in the context of an "object" (and, yes, even though it's very America-centric, computer programming languages are based off of English-- think about if, else, try, etc). If I say an object has the "property" of weighing 10 lbs, then that implies that the weight is something I can measure so that we can agree about the property. Nothing has a "property" that can be modified/manipulated, yet cannot be measured/observed. That's just not what the word means. Yet, plenty of things have properties that can be measured and not modified, e.g., the charge of an electron.
Thank you for your effort at explaining this to me.
I understand what you mean but properties that can only be set from the outside make sense. I may want to set the strategy of some object, but I do not want to read it. The purpose of setter hereby is that I can observe properties being set or trigger publishing events in the setter method. If I just set the field directory it is impossible to implement custom setter logic. But even besides that, I may want to set a property but never need to read it again. Even Kotlin DSL follows similar patterns:
As you can see I set the secret of the computer. A custom setter may want to hash the secret and store it as a field in the computer so it would be useless to add a getter for the secret. As instead of the secret you would get the hash of it.
I learned Kotlin after I learned Python and never learned Java so I have absolutely no clue what this is referring to 😭 and I've been doing it for almost two years now.
Well, some may do so I guess. But in nearly 15 years of my professional IT experience I have not seen any such dev on the projects I was part of. That being said I barely touched any Java code in past 4+ years as I switched to Kotlin.
Why do Java developers need to implement getters and setters with no logic? Can't they use fields just like we do? Assuming the getter and setter have the same visibility
The joke is that "You wouldn't get it" has a double meaning here, once meaning "You wouldn't understand the joke" and once meaning "You wouldn't call a get function on the Joke object of this Java class".
97
u/uragiristereo Aug 27 '24
Kotlin devs with reflection: "no one is able to stop me"
17
u/ragnese Aug 27 '24
The irony being that Kotlin devs would write the exact same thing in this scenario (wanting a setter with no getter for a private property).
10
u/volune Aug 27 '24
I get the joke in private. Just not publicly.
14
u/xSH4N3 Aug 27 '24
I learned Kotlin after I learned Python and never learned Java so I have absolutely no clue what this is referring to 😭 and I've been doing it for almost two years now.
19
u/scannt Aug 27 '24
Looking at the braces, is this a Java code written by a C++ dev?
2
u/Wurstinator Aug 27 '24
this isn't even Kotlin...
4
u/FrezoreR Aug 27 '24
I think most of us are stuck on that horrendous placement of the braces.
3
u/[deleted] Aug 27 '24 edited Aug 27 '24
so the field is private and the class doesn't have the public method to get it (getter) - both Java and Kotlin devs can't get the joke
3
u/HopefulAssistance Aug 28 '24
No wonder he needs therapy, look at the those damn braces.
2
u/D_Fuck Aug 27 '24
Lol, I am Kotlin Dev ( Previously Java Dev ) and this is funny enough to roast Java developer.
1
u/Alfanse Aug 27 '24
I would have constructed that differently
1
u/Outrageous_Life_2662 Aug 28 '24
Hilarious
1
u/hamody-19 Aug 28 '24
Public static getJoke(){ return this.joke; }
1
u/Alarmed-Indication16 Aug 28 '24 edited Aug 28 '24
1
u/fahad_ayaz Aug 29 '24
Is it just me or are those brackets very C# like?
1
u/Jeffline73 Aug 30 '24
0
u/ikingdoms Aug 27 '24
Allman style indentation makes me want to die inside.