admin管理员组

文章数量:1291009

I have a function like:

private fun Logging.ILoggingEvent.populateLoggingEvent(eventId: String, data: MutableMap<String, String?> = mutableMapOf()) =
    LoggingEventData(
        eventId = eventId,
        eventName = "LoggingEvent_$eventId",
        data = data
    )

while:

data class LoggingEventData(
    val eventId: String,
    val eventName: String,
    val data: MutableMap<String, String?>,
)

and

interface ILoggingEvent {

    val timeStamp: Instant

    fun updateTimeStamp(now: Instant) {
        timeStamp = now
    }
}

Why can I not convert upper populateLoggingEvent() function into brackets:

private fun Logging.ILoggingEvent.populateLoggingEvent(eventId: String, data: MutableMap<String, String?> = mutableMapOf()) {
    LoggingEventData(
        eventId = eventId,
        eventName = "LoggingEvent_$eventId",
        data = data
    )
}

I have a function like:

private fun Logging.ILoggingEvent.populateLoggingEvent(eventId: String, data: MutableMap<String, String?> = mutableMapOf()) =
    LoggingEventData(
        eventId = eventId,
        eventName = "LoggingEvent_$eventId",
        data = data
    )

while:

data class LoggingEventData(
    val eventId: String,
    val eventName: String,
    val data: MutableMap<String, String?>,
)

and

interface ILoggingEvent {

    val timeStamp: Instant

    fun updateTimeStamp(now: Instant) {
        timeStamp = now
    }
}

Why can I not convert upper populateLoggingEvent() function into brackets:

private fun Logging.ILoggingEvent.populateLoggingEvent(eventId: String, data: MutableMap<String, String?> = mutableMapOf()) {
    LoggingEventData(
        eventId = eventId,
        eventName = "LoggingEvent_$eventId",
        data = data
    )
}
Share Improve this question asked Feb 13 at 16:46 Ralf WickumRalf Wickum 3,29411 gold badges65 silver badges123 bronze badges 1
  • 1 what do you mean by you can't? Do you get an warning/error in the function? Do you get a warning/error in the function call? The difference between your first and last example is that in your first example you are returning the instance of the LoggingEventData that you create. And in your last example you are basically just creating an instance and not doing anything with it. If you want them to be equivalent you will have to write return LoggingEventData... and also to specify the return type. – AlexT Commented Feb 13 at 16:53
Add a comment  | 

1 Answer 1

Reset to default 3

When a function declaration is of the form fun f() = expression, it means that f is a function that returns expression.

To rewrite this using a block as the function body, you need to

  • write the return type explicitly
  • rewrite the expression into a return statement.

This means adding : LoggingEventData before the { ... }, and adding return before LoggingEventData(...).

private fun Logging.ILoggingEvent.populateLoggingEvent(
    eventId: String, data: MutableMap<String, String?> = mutableMapOf()
): LoggingEventData {
    return LoggingEventData(
        eventId = eventId, 
        eventName = "LoggingEvent_$eventId", 
        data = data
    )
}

In fact, converting between expression-bodied and block-bodied functions is available as a context action in IntelliJ. Right click the expression and select "Show Context Actions" (or Cmd+Enter on macOS), and select "convert to block body".

In your attempt, the function does not have a return type, so it becomes a Unit-returning function. The expression LoggingEventData(...) in the body is simply creating a LoggingEventData, and then discards it.

See also the documentation.

本文标签: Kotlin inline function intobracketsStack Overflow