-
I am trying to generate a function with the help of KotlinPoet similar to that fun someFunction(
dateTime: ZonedDateTime = ZonedDateTime.of(1989,1,23,0,0,0,0, ZoneId.of("UTC"))
) { ... } So I write something like that FileSpec.builder(packageName = "somePackage", fileName = "someFilename")
.addFunction(
FunSpec.builder(name = "someFunction")
addParameter.(
ParameterSpec.builder(
name = "dateTime",
type = classType
).defaultValue("%L", "ZonedDateTime.of(1989,1,23,0,0,0,0, ZoneId.of("UTC"))")
)
) The class is generated but compilation fails due to missing import. More specifically, the generated source does not include an import for the |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 6 replies
-
Imports are automatically added for types (as described in %T for Types). In your case, "ZonedDateTime" and "ZoneId" are simply parts of an expression, KotlinPoet has no way of knowing that those are actually types. Use the defaultValue("%T.of(1989,1,23,0,0,0,0, %T.of(\"UTC\"))", ZonedDateTime::class, ZoneId::class) |
Beta Was this translation helpful? Give feedback.
Imports are automatically added for types (as described in %T for Types). In your case, "ZonedDateTime" and "ZoneId" are simply parts of an expression, KotlinPoet has no way of knowing that those are actually types. Use the
%T
modifier to resolve this: