Multiple parameters in a generic function
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
add a comment |
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
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
Tis used for the inferred return type
– alijandro
Nov 21 '18 at 9:23
add a comment |
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
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
kotlin
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
Tis used for the inferred return type
– alijandro
Nov 21 '18 at 9:23
add a comment |
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
Tis 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
add a comment |
1 Answer
1
active
oldest
votes
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.
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
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
add a comment |
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.
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
add a comment |
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.
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.
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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
Tis used for the inferred return type– alijandro
Nov 21 '18 at 9:23