https://kotlinlang.org logo
Title
r

Roberto Fernandez

01/11/2018, 5:33 PM
guys, i have a question about kotlin + fold + function..here it is:
fun getView(cardGraphs: List<Graph>): View {

		val graphsContainer = RelativeLayout(context)
		graphsContainer.layoutParams = RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)

		cardGraphs.fold(null) { view: View?, graph: Graph ->
			return addView(graphsContainer, view, getView(graph))
		}

		return graphsContainer
	}
With this code, my function getView (the main function) is returning whatever is the first return of the addView() inside fold..If i set return@fold it needs return Nothing? but my method addView() returns View so it has a type parameter errors.. Can someone explain what is happening here in both cases? and why?
c

Czar

01/11/2018, 5:46 PM
You have
return
in your fold, it returns from
getVeiw
as it should, if you want to fold, just have
addView
in there without
return
r

Roberto Fernandez

01/11/2018, 5:48 PM
ok, and what about return @fold? does it mean that i’m returning that value to the next usage of fold, or is that returning that value to the main stack call?
c

Czar

01/11/2018, 5:49 PM
no you do not need return there
r

Roberto Fernandez

01/11/2018, 5:50 PM
ok
thanks!
c

Czar

01/11/2018, 5:50 PM
😉