简体   繁体   English

arguments 的默认值 lambda function 在 Z539A3A5859D24FB7B129E74D61F09

[英]default value for arguments of lambda function in Kotlin

I have such enum with lambda in constructor我在构造函数中有这样的枚举与 lambda

enum class EventType(
    val getInfoBlocks: (Book, AuditEntity?) -> List<InfoBlock>? = { _, _ -> emptyList() }
) {...}

Now I use this lambda function like this:现在我像这样使用这个 lambda function :

data = it.getInfoBlocks.invoke(deal, null)

Can I do without null?我可以不用 null 吗? Is there a way to set the default value for null lambda function arguments?有没有办法设置 null lambda function ZDBC11CAA5BDA29F77E6FB4EDABD8 的默认值?

TL;DR : function types cannot have default parameter values associated with them. TL;DR : function类型不能有与之关联的默认参数值。
This is something only named (non-anonymous) functions can do.这是只有命名(非匿名)函数才能做到的事情。

However , there is likely a better solution for your problem.但是,您的问题可能有更好的解决方案。


No, anonymous functions (ie lambda expressions and fun blocks) are not allowed to specify default values for their parameters.不可以,匿名函数(即 lambda 表达式和fun块)不允许为其参数指定默认值。

However, what you propose is even more involved than this: you seem to want the default value to be associated with the function's type , so it would remain the same, regardless of which function is assigned to this property.但是,您的建议甚至比这更复杂:您似乎希望默认值与函数的type相关联,因此无论将哪个 function 分配给此属性,它都将保持不变。

Because this default value would depend on the type of the variable that the function has been assigned to, for this to be possible, the type system would need a way to store the default argument value in the function's type information (ie something like (Book, AuditEntity? = null) -> List<InfoBlock>? ), and there's no way to do this.因为这个默认值取决于 function 已分配给的变量的类型,所以为了实现这一点,类型系统需要一种方法来将默认参数值存储在函数的类型信息中(即类似于(Book, AuditEntity? = null) -> List<InfoBlock>? ),没有办法做到这一点。


However, given that this is an enum, there's no need to store an anonymous function as a property;但是,鉴于这是一个枚举,因此无需将匿名 function 存储为属性; simply declare a virtual function and override it as necessary:只需声明一个虚拟 function 并根据需要覆盖它:

enum class EventType {

    // Type1 has the default implementation
    Type1,
    
    // Type2 has its own implementation
    Type2 {
        override fun getInfoBlocks(book: Book, entity: AuditEntity?): List<InfoBlock>? {
            // Do something else
            return foo()
        }
    };
    
    // Default implementation
    open fun getInfoBlocks(book: Book, entity: AuditEntity? = null): List<InfoBlock>? {
        return emptyList()
    }

}

(note that the semicolon is required to terminate the list of constants.) (请注意,需要分号来终止常量列表。)


This is similar to (but not the same as) why it's impossible to specify a default argument within an anonymous function itself ( see also this question ), ie:类似于(但不一样)为什么无法在匿名 function 本身中指定默认参数( 另请参见此问题),即:

{ a: Any, b: Any? = null -> foo() }

As not even this is possible, it makes sense that there wouldn't be a way to specify a default argument for an entire family of functions.因为即使这是不可能的,所以没有办法为整个函数系列指定默认参数是有道理的。

Yeah, do not use a lambda but make it an ordinary function:)是的,不要使用 lambda,而是使用普通的 function :)

fun getInfoBlocks (x: Book, y: AuditEntity? = null) : List<InfoBlock>  = emptyList() 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM