last day (15 days later) » 

10:54 AM
0
Q: How I can move from Java singleton to Kotlin based singleton?

Andrew GoroshkoI'm trying to move from Java to Kotlin and in my project I have singleton. And I don't understand at all which analogue of getter() and setter() Kotlin has. For example at Java I have smth like that: private Context context; and public void setContext(Context context) { this.context = co...

 
@Roland, I have edited my question, can you check it pls?
 
how does your Singleton look like? what is the error message? Is it visibility?
 
@Roland, edited again :)
 
ok... you should have a look at Object declarations which is the Kotlin way of writing a singleton.
so basically... start from object Singleton ;-)
 
but all my object Singleton comes tottaly yellow :( I was said that I don't have to put context at static fields
 
10:54 AM
who said what?
 
context wouldn't be static just because of an objectdeclaration
 
my ide
yes, I got it, but how can I pass context to this singleton?
I will need some data from sharedPreferences
 
object Singleton {
  lateinit var context : Context
}
// somewhere:
S.c = Context() // or whatever...
 
hmmm.... I can't move my brain from java to kotlin :(
I will need to place a lot of methods at this singleton
 
10:58 AM
so what's the problem?
 
@Roland as I understand I will place object with only one field like context, and where I will be able to put all methods?
 
    object Singleton {
      lateinit var context : Context
      fun lotsOfMethods() = 42
      fun evenMore() = "still 42"
      fun aMethodWithoutDirectAssignment() : Int {
            return 42
      }
    }

    // or alternatively (more flexible (depends ;-)))
    fun Singleton.moreAndMore() = 42L
 
but my singleton is still yellow due to the problems with memory leaks
Do not place Android context classes in static fields (static reference to Singleton which has field context pointing to Context); this is a memory leak
 
ah... ok... that's now Android-related... ;-)
 
yes ))
I'm writing android app :)
 
11:02 AM
I have no (not enough) idea about Android ;-)
However as what you have shown wasn't even a real Kotlin singleton you may want to adapt the question (or maybe ask a new one, closing this one?) where you want to ask about that Context-thing ;-)
adapting it should probably suffice
 
I will edit my question, maybe it will help
thank you for your help :)
 
I have another question... why did you say "moving from Java singleton to Kotlin singleton"?
that error should therefore already happened in Java, right?
 
because I have similar singleton in Java
 
... "similar"? ;-)
 
yes, but in Java I don't have any errors
@Roland I'm moving from java to kotlin
 
11:06 AM
I understood that... I just wonder why the Java variant doesn't show any errors
could you share that variant as well?
 
I showed this variant at my question
 
I mean your Java singleton variant
as you got your first downvote, you may want to restructure the question... now there is much information but the relevant is at the bottom... and the previous not-really-singleton is in between....
 
so I have to place this singleton at my question too?
 
well... what answer do you want to get?
I think how a singleton should look like in Kotlin is basically already answered within this chat ;-)
 
I would like to understand how to assign context value to my variable and use it in further
 
11:18 AM
as I see it, the question is now probably rather the following:
Java singleton code transformed to Kotlin gives error XXX
or something like that
or:
how can I use my Context in a Kotlin object?
I get error X when...
 
How to assign context value to variable at my kotlin singleton without crashing?
what about it?
 
does it crash? or does it warn you about memory leak?
if crashing: then ok... if not: don't exaggerate ;-)
 
when I have class it crashes, when I have an object I receive only warn
but I think that warning is bad for me too :)
 
I wouldn't show that class anymore... I would rather show the Java equivalent which you translated to the object Singleton-variant
-> Java works... no warnings
-> Kotlin works, but shows warnings
if it is so
except you do not believe me, that object Singleton basically is the singleton counterpart of a Java singleton... in which case I will answer your current question ;-) ... probably not ;-)
 
I believe you :) I try to understand how to move from Java to kotlin
all activities I have already rewritten from java
and as I see kotlin is very interesting and powerful language
but I faced with some problems at my singleton
so I decided to go to SO
I have edited my question and I hope it is android-kotlin based now :)
 
11:29 AM
that's fine.... for the Kotlin-part I can assist you, but not for the Android-part ;-)
 
anyway thank you, maybe someone will help me with my problem from android side :)
 
I think your first part of the question isn't really relevant anymore, is it? regarding getter/setter, etc.?
 
but how I will ask about value assigning?
 
didn't I already answer that? you access the property directly... (property ~= member variable)
 
I have edited question again :)
 
11:32 AM
getter/setter will be there if you look at it from the Java side, but aren't really necessary in Kotlin... that doesn't mean that Kotlin doesn't require such a construct too... e.g. you may need a private setter, but public gettter... there it becomes important
 
yes, but as I see developers have removed from kotlin too much methods, it is useful
 
have to leave... good luck with your Kotlin journey... and with your question!
 
thank you, good luck too :)
 

last day (15 days later) »