Is there a built in Kotlin method to apply void function to value?

Michael Pardo

I wrote this method to apply a void function to a value and return the value.

public inline fun <T> T.apply(f: (T) -> Unit): T {
    f(this)
    return this
}

This is useful in reducing something like this:

return values.map {
    var other = it.toOther()
    doStuff(other)
    return other
}

To something like this:

return values.map { it.toOther().apply({ doStuff(it) }) }

Is there a language feature or method like this already build in to Kotlin?

Kirill Rakhman

I ran into the same problem. My solution is basicly the same as yours with a small refinement:

inline fun <T> T.apply(f: T.() -> Any): T {
    this.f()
    return this
}

Note, that f is an extension function. This way you can invoke methods on your object using the implicit this reference. Here's an example taken from a libGDX project of mine:

val sprite : Sprite = atlas.createSprite("foo") apply {
    setSize(SIZE, SIZE)
    setOrigin(SIZE / 2, SIZE / 2)
}

Of course you could also call doStuff(this).

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

In F#, is there a built-in function that returns a function returning a given value when invoked with no arguments?

来自分类Dev

Is there a in built method from python csv module to enumerate all possible value for a specific column?

来自分类Dev

Casting an address to a function: using "void * void "

来自分类Dev

将std :: function <void()>转换为void(*)()

来自分类Dev

如何为Kotlin本机创建void *指针

来自分类Dev

PHP- Get each value of an associative array in a foreach loop, then apply a function and echo the result

来自分类Dev

TypeError: Object #<Object> has no method 'apply' Error: Argument 'fn' is not a function, got Object when running karma/jasmine tests

来自分类Dev

void * function(void * arguments),如何返回函数结果?

来自分类Dev

如何使用std :: function <void(void)>调用方法

来自分类Dev

通过function.apply关闭

来自分类Dev

Apply function to each pixel of the image

来自分类Dev

Java 8 Function <String,Void>与Consumer <String>

来自分类Dev

如何在gdb中调用function(void)

来自分类Dev

void Function()在Dart中做什么?

来自分类Dev

未知语法void(Type :: m_function)()

来自分类Dev

c struct size with void * value 成员

来自分类Dev

$ scope.apply();之间的区别 和$ scope.apply(function(){});

来自分类Dev

void Function(int)不是void Function(dynamic)的有效替代

来自分类Dev

Kotlin在伴侣对象中使用Apply引发意外错误

来自分类Dev

为什么静态void method()const会编译错误?

来自分类Dev

substitute built-in ColdFusion function between server versions

来自分类Dev

How to empty a numeric value from a form built with WTForm

来自分类Dev

Function.prototype.apply.bind用法?

来自分类Dev

Function.apply在connect的源代码中

来自分类Dev

Function.prototype.apply.bind用法?

来自分类Dev

Javascript:如何获取function.apply()的键

来自分类Dev

Kotlin cannot access protected abstract method

来自分类Dev

Override Java method with complex type in Kotlin

来自分类Dev

Function to inverse kron function value

Related 相关文章

  1. 1

    In F#, is there a built-in function that returns a function returning a given value when invoked with no arguments?

  2. 2

    Is there a in built method from python csv module to enumerate all possible value for a specific column?

  3. 3

    Casting an address to a function: using "void * void "

  4. 4

    将std :: function <void()>转换为void(*)()

  5. 5

    如何为Kotlin本机创建void *指针

  6. 6

    PHP- Get each value of an associative array in a foreach loop, then apply a function and echo the result

  7. 7

    TypeError: Object #<Object> has no method 'apply' Error: Argument 'fn' is not a function, got Object when running karma/jasmine tests

  8. 8

    void * function(void * arguments),如何返回函数结果?

  9. 9

    如何使用std :: function <void(void)>调用方法

  10. 10

    通过function.apply关闭

  11. 11

    Apply function to each pixel of the image

  12. 12

    Java 8 Function <String,Void>与Consumer <String>

  13. 13

    如何在gdb中调用function(void)

  14. 14

    void Function()在Dart中做什么?

  15. 15

    未知语法void(Type :: m_function)()

  16. 16

    c struct size with void * value 成员

  17. 17

    $ scope.apply();之间的区别 和$ scope.apply(function(){});

  18. 18

    void Function(int)不是void Function(dynamic)的有效替代

  19. 19

    Kotlin在伴侣对象中使用Apply引发意外错误

  20. 20

    为什么静态void method()const会编译错误?

  21. 21

    substitute built-in ColdFusion function between server versions

  22. 22

    How to empty a numeric value from a form built with WTForm

  23. 23

    Function.prototype.apply.bind用法?

  24. 24

    Function.apply在connect的源代码中

  25. 25

    Function.prototype.apply.bind用法?

  26. 26

    Javascript:如何获取function.apply()的键

  27. 27

    Kotlin cannot access protected abstract method

  28. 28

    Override Java method with complex type in Kotlin

  29. 29

    Function to inverse kron function value

热门标签

归档