return @ map如何工作

时间:2019-04-16 08:14:59

标签: kotlin

我刚刚开始使用kotlin学习Java流API,并且将Project Reactor和r2dbc API一起使用。我正在编写下面给出的简单代码,希望能理解它的工作原理。

我想知道t.let{}使用return@map返回MovieCeleb对象的行。我想知道这行的用途,即它是否创建列表/地图以及如何执行此操作。

fun listAllMoviesBy(actor: String, client: DatabaseClient) {
    client.execute()
            .sql { "SELECT fin.id, full_name, movie_title, make_year, celeb_id, birth_date " +
                    "FROM featured_in fin JOIN film_celeb fc ON " +
                    " fin.celeb_id = fc.id WHERE fc.full_name = $1"
            }.bind(0, actor).map { t, u ->
                t.let {
                   val featuredIn = FeaturedIn(it.get("id") as Int, it.get("movie_title") as String, it.get("make_year") as Int)
                   val celeb = MovieCeleb(it.get("celeb_id") as Int, it.get("full_name") as String, it.get("birth_date") as LocalDate, featuredIn)
                    return@map celeb
                }
            }.all()
            .subscribe{
                println(it)
            }
}

1 个答案:

答案 0 :(得分:1)

给出

.map { t, u ->
            t.let {
               val featuredIn = FeaturedIn(it.get("id") as Int, it.get("movie_title") as String, it.get("make_year") as Int)
               val celeb = MovieCeleb(it.get("celeb_id") as Int, it.get("full_name") as String, it.get("birth_date") as LocalDate, featuredIn)
                return@map celeb
            }
        }

return@map celeb将从celeb函数(而不是map!)返回let参考值。这只是所谓的non-local return,而您可以手动指定语句的返回范围。