https://kotlinlang.org logo
#android
Title
# android
s

scruffyfox

07/24/2017, 2:31 PM
Copy code
fun searchParent(id: String): Module?
	{
		modules.forEach { module ->
			val ret = searchParent(module, id)

			if (ret != null)
			{
				return@searchParent ret
			}
		}

		return null
	}
k

kingsley

07/24/2017, 2:33 PM
return@searchParent
does a local return (within the lambda) rather than a function return. You need to
return ret
instead
s

scruffyfox

07/24/2017, 2:34 PM
i had that originally which was the problem
k

kingsley

07/24/2017, 2:34 PM
Oh. Looking at your code.
return@searchParent
isn’t even valid code
s

scruffyfox

07/24/2017, 2:34 PM
lol it compiles 😛
😱 1
k

kingsley

07/24/2017, 2:34 PM
What problem did you have with simple
return xyz
s

scruffyfox

07/24/2017, 2:35 PM
it continues the loop after i've returned
i have a recursive datastructure and this basically loops through each one until it finds the matching ID then returns the parent object
the recursive method:
Copy code
fun searchParent(root: Module, id: String): Module?
	{
		root.steps?.forEach { step ->
			val ret = searchParent(step, id)

			if (ret == null)
			{
				if (step.id == id)
				{
					return root
				}
			}
		}

		return null
	}
m

minus

07/24/2017, 2:36 PM
return@searchParent should be correct (non-local return), but just use a for loop, it's shorter too
s

scruffyfox

07/24/2017, 2:36 PM
yeah i think that'll probably be the best, just trying to use lambdas as much as possible to get used to them
ok I fixed it! after writing it in plain java then refactoring it to kotlin worked cheers 👍