Multiple parameters in a generic function












0















In the Kotlin docs, the example shown for calling a generic function looks like this:



fun <T> singletonList(item: T): List<T> {

}

val l = singletonList<Int>(1)


I came across the following code:



val binding = DataBindingUtil.inflate<FragmentPlantDetailBinding>(
inflater, R.layout.fragment_plant_detail, container, false).apply {
}
}


and the inflate method looks like this:



public static <T extends ViewDataBinding> T inflate(@NonNull LayoutInflater inflater,
int layoutId, @Nullable ViewGroup parent, boolean attachToParent) {
return inflate(inflater, layoutId, parent, attachToParent, sDefaultComponent);
}


I thought I understood how calling a generic function works but in the second example, the function has 4 parameters. So what does FragmentPlantDetailBinding refer to? T isn't even being used in the inflate method. It should be noted that the inflate method is Java code while DataBindingUtil.inflate is Kotlin code. Is something going on here when a transition from Kotlin to Java is carried out?



In the Kotlin document example, it is clear that <T> is the type the function is using for both the parameter and the return value. But in that example there is only one parameter, so this is obvious. But if there are multiple parameters, what does it refer to?










share|improve this question

























  • it refers to any usage of 'T' in the function signature and body

    – Tim Castelijns
    Nov 21 '18 at 9:09













  • I updated my question to include the inflate method. "T" is not used in the body, so it isn't clear what it is being used for.

    – AndroidDev
    Nov 21 '18 at 9:17











  • T is used for the inferred return type

    – alijandro
    Nov 21 '18 at 9:23
















0















In the Kotlin docs, the example shown for calling a generic function looks like this:



fun <T> singletonList(item: T): List<T> {

}

val l = singletonList<Int>(1)


I came across the following code:



val binding = DataBindingUtil.inflate<FragmentPlantDetailBinding>(
inflater, R.layout.fragment_plant_detail, container, false).apply {
}
}


and the inflate method looks like this:



public static <T extends ViewDataBinding> T inflate(@NonNull LayoutInflater inflater,
int layoutId, @Nullable ViewGroup parent, boolean attachToParent) {
return inflate(inflater, layoutId, parent, attachToParent, sDefaultComponent);
}


I thought I understood how calling a generic function works but in the second example, the function has 4 parameters. So what does FragmentPlantDetailBinding refer to? T isn't even being used in the inflate method. It should be noted that the inflate method is Java code while DataBindingUtil.inflate is Kotlin code. Is something going on here when a transition from Kotlin to Java is carried out?



In the Kotlin document example, it is clear that <T> is the type the function is using for both the parameter and the return value. But in that example there is only one parameter, so this is obvious. But if there are multiple parameters, what does it refer to?










share|improve this question

























  • it refers to any usage of 'T' in the function signature and body

    – Tim Castelijns
    Nov 21 '18 at 9:09













  • I updated my question to include the inflate method. "T" is not used in the body, so it isn't clear what it is being used for.

    – AndroidDev
    Nov 21 '18 at 9:17











  • T is used for the inferred return type

    – alijandro
    Nov 21 '18 at 9:23














0












0








0


1






In the Kotlin docs, the example shown for calling a generic function looks like this:



fun <T> singletonList(item: T): List<T> {

}

val l = singletonList<Int>(1)


I came across the following code:



val binding = DataBindingUtil.inflate<FragmentPlantDetailBinding>(
inflater, R.layout.fragment_plant_detail, container, false).apply {
}
}


and the inflate method looks like this:



public static <T extends ViewDataBinding> T inflate(@NonNull LayoutInflater inflater,
int layoutId, @Nullable ViewGroup parent, boolean attachToParent) {
return inflate(inflater, layoutId, parent, attachToParent, sDefaultComponent);
}


I thought I understood how calling a generic function works but in the second example, the function has 4 parameters. So what does FragmentPlantDetailBinding refer to? T isn't even being used in the inflate method. It should be noted that the inflate method is Java code while DataBindingUtil.inflate is Kotlin code. Is something going on here when a transition from Kotlin to Java is carried out?



In the Kotlin document example, it is clear that <T> is the type the function is using for both the parameter and the return value. But in that example there is only one parameter, so this is obvious. But if there are multiple parameters, what does it refer to?










share|improve this question
















In the Kotlin docs, the example shown for calling a generic function looks like this:



fun <T> singletonList(item: T): List<T> {

}

val l = singletonList<Int>(1)


I came across the following code:



val binding = DataBindingUtil.inflate<FragmentPlantDetailBinding>(
inflater, R.layout.fragment_plant_detail, container, false).apply {
}
}


and the inflate method looks like this:



public static <T extends ViewDataBinding> T inflate(@NonNull LayoutInflater inflater,
int layoutId, @Nullable ViewGroup parent, boolean attachToParent) {
return inflate(inflater, layoutId, parent, attachToParent, sDefaultComponent);
}


I thought I understood how calling a generic function works but in the second example, the function has 4 parameters. So what does FragmentPlantDetailBinding refer to? T isn't even being used in the inflate method. It should be noted that the inflate method is Java code while DataBindingUtil.inflate is Kotlin code. Is something going on here when a transition from Kotlin to Java is carried out?



In the Kotlin document example, it is clear that <T> is the type the function is using for both the parameter and the return value. But in that example there is only one parameter, so this is obvious. But if there are multiple parameters, what does it refer to?







kotlin






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 9:15







AndroidDev

















asked Nov 21 '18 at 9:04









AndroidDevAndroidDev

10.2k2395167




10.2k2395167













  • it refers to any usage of 'T' in the function signature and body

    – Tim Castelijns
    Nov 21 '18 at 9:09













  • I updated my question to include the inflate method. "T" is not used in the body, so it isn't clear what it is being used for.

    – AndroidDev
    Nov 21 '18 at 9:17











  • T is used for the inferred return type

    – alijandro
    Nov 21 '18 at 9:23



















  • it refers to any usage of 'T' in the function signature and body

    – Tim Castelijns
    Nov 21 '18 at 9:09













  • I updated my question to include the inflate method. "T" is not used in the body, so it isn't clear what it is being used for.

    – AndroidDev
    Nov 21 '18 at 9:17











  • T is used for the inferred return type

    – alijandro
    Nov 21 '18 at 9:23

















it refers to any usage of 'T' in the function signature and body

– Tim Castelijns
Nov 21 '18 at 9:09







it refers to any usage of 'T' in the function signature and body

– Tim Castelijns
Nov 21 '18 at 9:09















I updated my question to include the inflate method. "T" is not used in the body, so it isn't clear what it is being used for.

– AndroidDev
Nov 21 '18 at 9:17





I updated my question to include the inflate method. "T" is not used in the body, so it isn't clear what it is being used for.

– AndroidDev
Nov 21 '18 at 9:17













T is used for the inferred return type

– alijandro
Nov 21 '18 at 9:23





T is used for the inferred return type

– alijandro
Nov 21 '18 at 9:23












1 Answer
1






active

oldest

votes


















1














As you can see here:



public static <T extends ViewDataBinding> T inflate(
LayoutInflater inflater,
int layoutId,
@Nullable ViewGroup parent,
boolean attachToParent
) {
return inflate(inflater, layoutId, parent, attachToParent, sDefaultComponent);
}


T specifies the return type of inflate.



So, your variable binding will be of type FragmentPlantDetailBinding.






share|improve this answer
























  • After updating my question and noted that the inflate method was Java, I suddenly realized that while your answer is true, what is missing is that behind-the-scenes, Android is providing a mapping between Kotlin and Java that causes the "T" type to also be used as the return type for the Java method.

    – AndroidDev
    Nov 21 '18 at 9:33











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%2f53408498%2fmultiple-parameters-in-a-generic-function%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









1














As you can see here:



public static <T extends ViewDataBinding> T inflate(
LayoutInflater inflater,
int layoutId,
@Nullable ViewGroup parent,
boolean attachToParent
) {
return inflate(inflater, layoutId, parent, attachToParent, sDefaultComponent);
}


T specifies the return type of inflate.



So, your variable binding will be of type FragmentPlantDetailBinding.






share|improve this answer
























  • After updating my question and noted that the inflate method was Java, I suddenly realized that while your answer is true, what is missing is that behind-the-scenes, Android is providing a mapping between Kotlin and Java that causes the "T" type to also be used as the return type for the Java method.

    – AndroidDev
    Nov 21 '18 at 9:33
















1














As you can see here:



public static <T extends ViewDataBinding> T inflate(
LayoutInflater inflater,
int layoutId,
@Nullable ViewGroup parent,
boolean attachToParent
) {
return inflate(inflater, layoutId, parent, attachToParent, sDefaultComponent);
}


T specifies the return type of inflate.



So, your variable binding will be of type FragmentPlantDetailBinding.






share|improve this answer
























  • After updating my question and noted that the inflate method was Java, I suddenly realized that while your answer is true, what is missing is that behind-the-scenes, Android is providing a mapping between Kotlin and Java that causes the "T" type to also be used as the return type for the Java method.

    – AndroidDev
    Nov 21 '18 at 9:33














1












1








1







As you can see here:



public static <T extends ViewDataBinding> T inflate(
LayoutInflater inflater,
int layoutId,
@Nullable ViewGroup parent,
boolean attachToParent
) {
return inflate(inflater, layoutId, parent, attachToParent, sDefaultComponent);
}


T specifies the return type of inflate.



So, your variable binding will be of type FragmentPlantDetailBinding.






share|improve this answer













As you can see here:



public static <T extends ViewDataBinding> T inflate(
LayoutInflater inflater,
int layoutId,
@Nullable ViewGroup parent,
boolean attachToParent
) {
return inflate(inflater, layoutId, parent, attachToParent, sDefaultComponent);
}


T specifies the return type of inflate.



So, your variable binding will be of type FragmentPlantDetailBinding.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 21 '18 at 9:21









Willi MentzelWilli Mentzel

10.3k114771




10.3k114771













  • After updating my question and noted that the inflate method was Java, I suddenly realized that while your answer is true, what is missing is that behind-the-scenes, Android is providing a mapping between Kotlin and Java that causes the "T" type to also be used as the return type for the Java method.

    – AndroidDev
    Nov 21 '18 at 9:33



















  • After updating my question and noted that the inflate method was Java, I suddenly realized that while your answer is true, what is missing is that behind-the-scenes, Android is providing a mapping between Kotlin and Java that causes the "T" type to also be used as the return type for the Java method.

    – AndroidDev
    Nov 21 '18 at 9:33

















After updating my question and noted that the inflate method was Java, I suddenly realized that while your answer is true, what is missing is that behind-the-scenes, Android is providing a mapping between Kotlin and Java that causes the "T" type to also be used as the return type for the Java method.

– AndroidDev
Nov 21 '18 at 9:33





After updating my question and noted that the inflate method was Java, I suddenly realized that while your answer is true, what is missing is that behind-the-scenes, Android is providing a mapping between Kotlin and Java that causes the "T" type to also be used as the return type for the Java method.

– AndroidDev
Nov 21 '18 at 9:33




















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%2f53408498%2fmultiple-parameters-in-a-generic-function%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







這個網誌中的熱門文章

Academy of Television Arts & Sciences

L'Équipe

1995 France bombings