How does Android Studio know the return type of a generic method in edit mode












0















In this Kotlin code for an Android app:



class PlantAdapter : ListAdapter<Plant, PlantAdapter.ViewHolder>(PlantDiffCallback()) {

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val plant = getItem(position)
holder.apply {
bind(createOnClickListener(plant.plantId), plant)
itemView.tag = plant
}
}
}


In Android Studio when I type plant followed by a dot, the list of member fields and methods for plant are listed. It isn't clear to me how Android Studio knows this. If I run the app and single step into the getItem method, the getItem method is a generic method that knows nothing about the data type for plant. It just grabs the item from the list and returns it. Only upon returning does plant variable show up as a Plant type.



So how does Android Studio know what type this is when the app is not running?










share|improve this question























  • Your list adapter is parametrized with Plant. What else can its items be?

    – Sergio Tulentsev
    Nov 21 '18 at 10:31











  • But val plant = is just a local variable that has nothing to do with the list adapter. It's type is dependent upon what getItem returns. I could just as well name plant to "p" and it still knows that it is a Plant.

    – AndroidDev
    Nov 21 '18 at 10:43













  • "and it still knows that it is a Plant." - Yes, because getItem returns T. The same T that is in the class template. Which is set to Plant. That's just how generics work.

    – Sergio Tulentsev
    Nov 21 '18 at 10:47


















0















In this Kotlin code for an Android app:



class PlantAdapter : ListAdapter<Plant, PlantAdapter.ViewHolder>(PlantDiffCallback()) {

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val plant = getItem(position)
holder.apply {
bind(createOnClickListener(plant.plantId), plant)
itemView.tag = plant
}
}
}


In Android Studio when I type plant followed by a dot, the list of member fields and methods for plant are listed. It isn't clear to me how Android Studio knows this. If I run the app and single step into the getItem method, the getItem method is a generic method that knows nothing about the data type for plant. It just grabs the item from the list and returns it. Only upon returning does plant variable show up as a Plant type.



So how does Android Studio know what type this is when the app is not running?










share|improve this question























  • Your list adapter is parametrized with Plant. What else can its items be?

    – Sergio Tulentsev
    Nov 21 '18 at 10:31











  • But val plant = is just a local variable that has nothing to do with the list adapter. It's type is dependent upon what getItem returns. I could just as well name plant to "p" and it still knows that it is a Plant.

    – AndroidDev
    Nov 21 '18 at 10:43













  • "and it still knows that it is a Plant." - Yes, because getItem returns T. The same T that is in the class template. Which is set to Plant. That's just how generics work.

    – Sergio Tulentsev
    Nov 21 '18 at 10:47
















0












0








0








In this Kotlin code for an Android app:



class PlantAdapter : ListAdapter<Plant, PlantAdapter.ViewHolder>(PlantDiffCallback()) {

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val plant = getItem(position)
holder.apply {
bind(createOnClickListener(plant.plantId), plant)
itemView.tag = plant
}
}
}


In Android Studio when I type plant followed by a dot, the list of member fields and methods for plant are listed. It isn't clear to me how Android Studio knows this. If I run the app and single step into the getItem method, the getItem method is a generic method that knows nothing about the data type for plant. It just grabs the item from the list and returns it. Only upon returning does plant variable show up as a Plant type.



So how does Android Studio know what type this is when the app is not running?










share|improve this question














In this Kotlin code for an Android app:



class PlantAdapter : ListAdapter<Plant, PlantAdapter.ViewHolder>(PlantDiffCallback()) {

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val plant = getItem(position)
holder.apply {
bind(createOnClickListener(plant.plantId), plant)
itemView.tag = plant
}
}
}


In Android Studio when I type plant followed by a dot, the list of member fields and methods for plant are listed. It isn't clear to me how Android Studio knows this. If I run the app and single step into the getItem method, the getItem method is a generic method that knows nothing about the data type for plant. It just grabs the item from the list and returns it. Only upon returning does plant variable show up as a Plant type.



So how does Android Studio know what type this is when the app is not running?







android-studio kotlin






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 21 '18 at 10:08









AndroidDevAndroidDev

10.2k2395167




10.2k2395167













  • Your list adapter is parametrized with Plant. What else can its items be?

    – Sergio Tulentsev
    Nov 21 '18 at 10:31











  • But val plant = is just a local variable that has nothing to do with the list adapter. It's type is dependent upon what getItem returns. I could just as well name plant to "p" and it still knows that it is a Plant.

    – AndroidDev
    Nov 21 '18 at 10:43













  • "and it still knows that it is a Plant." - Yes, because getItem returns T. The same T that is in the class template. Which is set to Plant. That's just how generics work.

    – Sergio Tulentsev
    Nov 21 '18 at 10:47





















  • Your list adapter is parametrized with Plant. What else can its items be?

    – Sergio Tulentsev
    Nov 21 '18 at 10:31











  • But val plant = is just a local variable that has nothing to do with the list adapter. It's type is dependent upon what getItem returns. I could just as well name plant to "p" and it still knows that it is a Plant.

    – AndroidDev
    Nov 21 '18 at 10:43













  • "and it still knows that it is a Plant." - Yes, because getItem returns T. The same T that is in the class template. Which is set to Plant. That's just how generics work.

    – Sergio Tulentsev
    Nov 21 '18 at 10:47



















Your list adapter is parametrized with Plant. What else can its items be?

– Sergio Tulentsev
Nov 21 '18 at 10:31





Your list adapter is parametrized with Plant. What else can its items be?

– Sergio Tulentsev
Nov 21 '18 at 10:31













But val plant = is just a local variable that has nothing to do with the list adapter. It's type is dependent upon what getItem returns. I could just as well name plant to "p" and it still knows that it is a Plant.

– AndroidDev
Nov 21 '18 at 10:43







But val plant = is just a local variable that has nothing to do with the list adapter. It's type is dependent upon what getItem returns. I could just as well name plant to "p" and it still knows that it is a Plant.

– AndroidDev
Nov 21 '18 at 10:43















"and it still knows that it is a Plant." - Yes, because getItem returns T. The same T that is in the class template. Which is set to Plant. That's just how generics work.

– Sergio Tulentsev
Nov 21 '18 at 10:47







"and it still knows that it is a Plant." - Yes, because getItem returns T. The same T that is in the class template. Which is set to Plant. That's just how generics work.

– Sergio Tulentsev
Nov 21 '18 at 10:47














1 Answer
1






active

oldest

votes


















3














ListAdapter is parameterized with Plant type as a presentation model type that the list will receive in this case. So, if getItem's method definition in ListAdapter is specified as follows:



protected T getItem(int position) {
return mHelper.getItem(position);
}


then after providing Plant as the type argument this method will "transform" (let's skip type erasure and bridge methods in this example) into this:



protected Plant getItem(int position) {
return mHelper.getItem(position);
}


Kotlin supports type inference in variable initialization. It means that if a type of the variable was not specified explicitly (e.g. val plant: Plant = ...), then the type will be inferred from the expression result type. As we know that after parameterizing ListAdapter with Plant method getItem will return us a Plant instance, then this type will be inferred for the declared variable in val plant = getItem(position).



In Java a corresponding declaration would look like this:



Plant plant = this.getItem(position);





share|improve this answer
























  • Doesn't modern java have type inference too?

    – Sergio Tulentsev
    Nov 21 '18 at 11:05











  • @SergioTulentsev AFAIK Java 11 supports it in the context of variable declaration and initialization (val num = 5). But in previous version we are forced to specify the type directly (int num = 5).

    – Andrey Ilyunin
    Nov 21 '18 at 11:07











  • Java supports type inference in the context of generics (type parameterizing), sure, but these concepts aren't same.

    – Andrey Ilyunin
    Nov 21 '18 at 11:09











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
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53409640%2fhow-does-android-studio-know-the-return-type-of-a-generic-method-in-edit-mode%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









3














ListAdapter is parameterized with Plant type as a presentation model type that the list will receive in this case. So, if getItem's method definition in ListAdapter is specified as follows:



protected T getItem(int position) {
return mHelper.getItem(position);
}


then after providing Plant as the type argument this method will "transform" (let's skip type erasure and bridge methods in this example) into this:



protected Plant getItem(int position) {
return mHelper.getItem(position);
}


Kotlin supports type inference in variable initialization. It means that if a type of the variable was not specified explicitly (e.g. val plant: Plant = ...), then the type will be inferred from the expression result type. As we know that after parameterizing ListAdapter with Plant method getItem will return us a Plant instance, then this type will be inferred for the declared variable in val plant = getItem(position).



In Java a corresponding declaration would look like this:



Plant plant = this.getItem(position);





share|improve this answer
























  • Doesn't modern java have type inference too?

    – Sergio Tulentsev
    Nov 21 '18 at 11:05











  • @SergioTulentsev AFAIK Java 11 supports it in the context of variable declaration and initialization (val num = 5). But in previous version we are forced to specify the type directly (int num = 5).

    – Andrey Ilyunin
    Nov 21 '18 at 11:07











  • Java supports type inference in the context of generics (type parameterizing), sure, but these concepts aren't same.

    – Andrey Ilyunin
    Nov 21 '18 at 11:09
















3














ListAdapter is parameterized with Plant type as a presentation model type that the list will receive in this case. So, if getItem's method definition in ListAdapter is specified as follows:



protected T getItem(int position) {
return mHelper.getItem(position);
}


then after providing Plant as the type argument this method will "transform" (let's skip type erasure and bridge methods in this example) into this:



protected Plant getItem(int position) {
return mHelper.getItem(position);
}


Kotlin supports type inference in variable initialization. It means that if a type of the variable was not specified explicitly (e.g. val plant: Plant = ...), then the type will be inferred from the expression result type. As we know that after parameterizing ListAdapter with Plant method getItem will return us a Plant instance, then this type will be inferred for the declared variable in val plant = getItem(position).



In Java a corresponding declaration would look like this:



Plant plant = this.getItem(position);





share|improve this answer
























  • Doesn't modern java have type inference too?

    – Sergio Tulentsev
    Nov 21 '18 at 11:05











  • @SergioTulentsev AFAIK Java 11 supports it in the context of variable declaration and initialization (val num = 5). But in previous version we are forced to specify the type directly (int num = 5).

    – Andrey Ilyunin
    Nov 21 '18 at 11:07











  • Java supports type inference in the context of generics (type parameterizing), sure, but these concepts aren't same.

    – Andrey Ilyunin
    Nov 21 '18 at 11:09














3












3








3







ListAdapter is parameterized with Plant type as a presentation model type that the list will receive in this case. So, if getItem's method definition in ListAdapter is specified as follows:



protected T getItem(int position) {
return mHelper.getItem(position);
}


then after providing Plant as the type argument this method will "transform" (let's skip type erasure and bridge methods in this example) into this:



protected Plant getItem(int position) {
return mHelper.getItem(position);
}


Kotlin supports type inference in variable initialization. It means that if a type of the variable was not specified explicitly (e.g. val plant: Plant = ...), then the type will be inferred from the expression result type. As we know that after parameterizing ListAdapter with Plant method getItem will return us a Plant instance, then this type will be inferred for the declared variable in val plant = getItem(position).



In Java a corresponding declaration would look like this:



Plant plant = this.getItem(position);





share|improve this answer













ListAdapter is parameterized with Plant type as a presentation model type that the list will receive in this case. So, if getItem's method definition in ListAdapter is specified as follows:



protected T getItem(int position) {
return mHelper.getItem(position);
}


then after providing Plant as the type argument this method will "transform" (let's skip type erasure and bridge methods in this example) into this:



protected Plant getItem(int position) {
return mHelper.getItem(position);
}


Kotlin supports type inference in variable initialization. It means that if a type of the variable was not specified explicitly (e.g. val plant: Plant = ...), then the type will be inferred from the expression result type. As we know that after parameterizing ListAdapter with Plant method getItem will return us a Plant instance, then this type will be inferred for the declared variable in val plant = getItem(position).



In Java a corresponding declaration would look like this:



Plant plant = this.getItem(position);






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 21 '18 at 11:02









Andrey IlyuninAndrey Ilyunin

1,368220




1,368220













  • Doesn't modern java have type inference too?

    – Sergio Tulentsev
    Nov 21 '18 at 11:05











  • @SergioTulentsev AFAIK Java 11 supports it in the context of variable declaration and initialization (val num = 5). But in previous version we are forced to specify the type directly (int num = 5).

    – Andrey Ilyunin
    Nov 21 '18 at 11:07











  • Java supports type inference in the context of generics (type parameterizing), sure, but these concepts aren't same.

    – Andrey Ilyunin
    Nov 21 '18 at 11:09



















  • Doesn't modern java have type inference too?

    – Sergio Tulentsev
    Nov 21 '18 at 11:05











  • @SergioTulentsev AFAIK Java 11 supports it in the context of variable declaration and initialization (val num = 5). But in previous version we are forced to specify the type directly (int num = 5).

    – Andrey Ilyunin
    Nov 21 '18 at 11:07











  • Java supports type inference in the context of generics (type parameterizing), sure, but these concepts aren't same.

    – Andrey Ilyunin
    Nov 21 '18 at 11:09

















Doesn't modern java have type inference too?

– Sergio Tulentsev
Nov 21 '18 at 11:05





Doesn't modern java have type inference too?

– Sergio Tulentsev
Nov 21 '18 at 11:05













@SergioTulentsev AFAIK Java 11 supports it in the context of variable declaration and initialization (val num = 5). But in previous version we are forced to specify the type directly (int num = 5).

– Andrey Ilyunin
Nov 21 '18 at 11:07





@SergioTulentsev AFAIK Java 11 supports it in the context of variable declaration and initialization (val num = 5). But in previous version we are forced to specify the type directly (int num = 5).

– Andrey Ilyunin
Nov 21 '18 at 11:07













Java supports type inference in the context of generics (type parameterizing), sure, but these concepts aren't same.

– Andrey Ilyunin
Nov 21 '18 at 11:09





Java supports type inference in the context of generics (type parameterizing), sure, but these concepts aren't same.

– Andrey Ilyunin
Nov 21 '18 at 11:09




















draft saved

draft discarded




















































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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53409640%2fhow-does-android-studio-know-the-return-type-of-a-generic-method-in-edit-mode%23new-answer', 'question_page');
}
);

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







Popular posts from this blog

How to change which sound is reproduced for terminal bell?

Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

Can I use Tabulator js library in my java Spring + Thymeleaf project?