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 Answer
Reset to default 3When 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
版权声明:本文标题:Kotlin inline function into {} brackets? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741515958a2382889.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
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 writereturn LoggingEventData...
and also to specify the return type. – AlexT Commented Feb 13 at 16:53