首页 > 解决方案 > 如何在 Kotlin 中为文档编写注释?

问题描述

我想在不附加到任何特定 Kotlin 类或方法的源代码中添加文档,它就像概念性的高级文档。

我想把它保存在 Kotlin 源文件中,接近它所描述的东西。

理想情况下,我希望能够将其写为:

...
doc(
  title = "Some title",
  text  = "Some text"
)
...

但这是不可能的,因为 Kotlin 不允许顶级调用,我也尝试使用注释,但如果不附加一些东西也是不允许的。

@Doc(
  title = "Some title",
  text  = "Some text"
)

因此,下面的代码有效,但看起来并不好。有更好的吗?

@Doc private val doc1 = doc(
  title = "Some title",
  text  = "Some text"
)

或者可能

@Doc(
  title = "Some title",
  text  = "Some text"
) val tmp = 0

标签: kotlin

解决方案


推荐阅读