How to refer to outer this on forEach in Kotlin
I have the following case
someThing.forEach{
someWidget.setOnClickListener{
//it is an View
//I need foreach it of someObject
}
}
I read this answer but it does not work
kotlin how to refer outer-scope this in multi-layer apply functions
add a comment |
I have the following case
someThing.forEach{
someWidget.setOnClickListener{
//it is an View
//I need foreach it of someObject
}
}
I read this answer but it does not work
kotlin how to refer outer-scope this in multi-layer apply functions
add a comment |
I have the following case
someThing.forEach{
someWidget.setOnClickListener{
//it is an View
//I need foreach it of someObject
}
}
I read this answer but it does not work
kotlin how to refer outer-scope this in multi-layer apply functions
I have the following case
someThing.forEach{
someWidget.setOnClickListener{
//it is an View
//I need foreach it of someObject
}
}
I read this answer but it does not work
kotlin how to refer outer-scope this in multi-layer apply functions
asked Nov 13 '18 at 16:33
Erick Daniel Juarez GilErick Daniel Juarez Gil
104
104
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
The problem is that you are not dealing with this here.
forEach has a parameter and for simplicity you can leave it away and just use it instead. Not using it is the same as using _ -> instead... you just discard it.
So your example written with named lambda parameters instead:
someThing.forEach{ some -> // 'it' was available here too, but will not be accessible from within the next setOnClickListener...
someWidget.setOnClickListener{
// some contains one of the someThings now and 'it' is still your View
}
}
add a comment |
You can name the variable in the forEach.
things.forEach { thing ->
someWidget.setOnClickListener {
thing.doSomething()
}
}
add a comment |
I think you mean something like this:
someThing.forEach{ x->
someWidget.setOnClickListener{
//use x
//I need foreach it of someObject
}
}
just use another name like x, you don't have to use it.
Here is an example:
val a = mutableListOf<Int>(1, 3)
val b = mutableListOf<Int>(2, 4)
a.forEach { x ->
b.forEach {
println("" + x + " " + it)
}
}
here x is each item from list a
and it is each item from list b
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%2f53285525%2fhow-to-refer-to-outer-this-on-foreach-in-kotlin%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
The problem is that you are not dealing with this here.
forEach has a parameter and for simplicity you can leave it away and just use it instead. Not using it is the same as using _ -> instead... you just discard it.
So your example written with named lambda parameters instead:
someThing.forEach{ some -> // 'it' was available here too, but will not be accessible from within the next setOnClickListener...
someWidget.setOnClickListener{
// some contains one of the someThings now and 'it' is still your View
}
}
add a comment |
The problem is that you are not dealing with this here.
forEach has a parameter and for simplicity you can leave it away and just use it instead. Not using it is the same as using _ -> instead... you just discard it.
So your example written with named lambda parameters instead:
someThing.forEach{ some -> // 'it' was available here too, but will not be accessible from within the next setOnClickListener...
someWidget.setOnClickListener{
// some contains one of the someThings now and 'it' is still your View
}
}
add a comment |
The problem is that you are not dealing with this here.
forEach has a parameter and for simplicity you can leave it away and just use it instead. Not using it is the same as using _ -> instead... you just discard it.
So your example written with named lambda parameters instead:
someThing.forEach{ some -> // 'it' was available here too, but will not be accessible from within the next setOnClickListener...
someWidget.setOnClickListener{
// some contains one of the someThings now and 'it' is still your View
}
}
The problem is that you are not dealing with this here.
forEach has a parameter and for simplicity you can leave it away and just use it instead. Not using it is the same as using _ -> instead... you just discard it.
So your example written with named lambda parameters instead:
someThing.forEach{ some -> // 'it' was available here too, but will not be accessible from within the next setOnClickListener...
someWidget.setOnClickListener{
// some contains one of the someThings now and 'it' is still your View
}
}
answered Nov 13 '18 at 16:44
RolandRoland
9,61911141
9,61911141
add a comment |
add a comment |
You can name the variable in the forEach.
things.forEach { thing ->
someWidget.setOnClickListener {
thing.doSomething()
}
}
add a comment |
You can name the variable in the forEach.
things.forEach { thing ->
someWidget.setOnClickListener {
thing.doSomething()
}
}
add a comment |
You can name the variable in the forEach.
things.forEach { thing ->
someWidget.setOnClickListener {
thing.doSomething()
}
}
You can name the variable in the forEach.
things.forEach { thing ->
someWidget.setOnClickListener {
thing.doSomething()
}
}
answered Nov 13 '18 at 16:42
CristanCristan
2,9432630
2,9432630
add a comment |
add a comment |
I think you mean something like this:
someThing.forEach{ x->
someWidget.setOnClickListener{
//use x
//I need foreach it of someObject
}
}
just use another name like x, you don't have to use it.
Here is an example:
val a = mutableListOf<Int>(1, 3)
val b = mutableListOf<Int>(2, 4)
a.forEach { x ->
b.forEach {
println("" + x + " " + it)
}
}
here x is each item from list a
and it is each item from list b
add a comment |
I think you mean something like this:
someThing.forEach{ x->
someWidget.setOnClickListener{
//use x
//I need foreach it of someObject
}
}
just use another name like x, you don't have to use it.
Here is an example:
val a = mutableListOf<Int>(1, 3)
val b = mutableListOf<Int>(2, 4)
a.forEach { x ->
b.forEach {
println("" + x + " " + it)
}
}
here x is each item from list a
and it is each item from list b
add a comment |
I think you mean something like this:
someThing.forEach{ x->
someWidget.setOnClickListener{
//use x
//I need foreach it of someObject
}
}
just use another name like x, you don't have to use it.
Here is an example:
val a = mutableListOf<Int>(1, 3)
val b = mutableListOf<Int>(2, 4)
a.forEach { x ->
b.forEach {
println("" + x + " " + it)
}
}
here x is each item from list a
and it is each item from list b
I think you mean something like this:
someThing.forEach{ x->
someWidget.setOnClickListener{
//use x
//I need foreach it of someObject
}
}
just use another name like x, you don't have to use it.
Here is an example:
val a = mutableListOf<Int>(1, 3)
val b = mutableListOf<Int>(2, 4)
a.forEach { x ->
b.forEach {
println("" + x + " " + it)
}
}
here x is each item from list a
and it is each item from list b
edited Nov 13 '18 at 16:48
answered Nov 13 '18 at 16:43
forpasforpas
10.4k1421
10.4k1421
add a comment |
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%2f53285525%2fhow-to-refer-to-outer-this-on-foreach-in-kotlin%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