Reputation: 2836
Im new to Kotlin and wonder what does the get() = login_email.txt.toString()
do?
Does it set email
String?
Upvotes: 6
Views: 18112
Reputation: 951
get()
is used to define a custom getter method. Anytime you access the property you are using the custom getter methodget()
, it is important that we get a proper understanding of what properties
actually are.We all know that in object oriented programming the main idea of a class is to encapsulate data and code that works on data in a single class. In a language like Java (don't worry we will get back to Kotlin soon), the data of a class is stored in private fields, we then use accessor method (getters and setters) to access the data. In Java the combination of accessor methods and a field is called a property
Now in Kotlin does things a little differently, it entirely replaces the traditional idea of defining accessor methods and fields. By using the val
or var
keyword Kotlin will automatically generate the corresponding field and appropriate accessor methods for us.
There will come a time when either your code or someone else's code needs a more robust solution to the automatic accessor methods created by Kotlin. This is where get()
and set()
come into play. By using get() you are defining your own custom accessor method(getter for get()) to be used when you are accessing this property.
I would also like to point out that since val
is immutable it does not allow you to define a set()
method, only a get()
Upvotes: 5
Reputation: 28258
get()
and set(value)
after a field means the declaration of a custom getter and/or setter. Here's a basic example using default values:
class Demo{
var something: String
get() = field
set(value) {
field = value;
}
constructor(something: String){
this.something = something;
}
}
These two are, however, redundant. You don't actually need them unless you're doing something custom with it. They're automatically added for vars, though that only applies to getters for val
s (because they can't be changed, they don't have setters).
The line you were asking about is a custom getter.
get() // declares a custom getter
= // if you don't know how this works, see my explanation below
login_email.text.toString() // you should be familiar with this part already; gets the string value of the field
If you're not familiar with the syntax, this is the equivalent without =
:
get(){
return login_email.text.toString()
}
if you have a single return, you can replace the brackets and return keyword with =
. If it helps you remember, just remember the alternative to using =
(a body + the return
keyword)
TL;DR: it declares a custom setter that returns the value of a TextView/EditText (not sure which it is, you didn't include that in the question)
In your case, you're using a custom getter or setter to handle property data. The fields themselves don't actually contain any data, but you have getters for a different object.
Take this as an example:
class Demo(private val someObject: MyCustomObjectWithSomeData){
val text: String
get() = someObject.text
... same for other stuff. Could also have setters, if the properties are mutable
}
Here the object is private, but it could be public or internal for that matter.
Kotlin supports quite a lot with custom getters. For an instance, you can declare a field to display specific fields of a private variable. For an instance, in your case, you have the email. It doesn't need to be a variable, since you have a custom getter, and the field isn't initialized. If you change var email
to a val
, you can make it non-null:
val email: String
get() = login_email.text.toString()
That also helps with null-safety.
And for the error
field, it's slightly more complicated. It can't be a val
because you declare a custom setter, but if you add a getter, you can make it non-null:
var error: String
get() = login_error.text.toString()
set(value){
login_error.text = value;
}
Upvotes: 18