代码之家  ›  专栏  ›  技术社区  ›  Andrew Churilo

批注不能用作批注参数

  •  0
  • Andrew Churilo  · 技术社区  · 4 年前

    我得到了一个错误,尽管我的操作与文档中所说的完全相同。 Documentation :

    data class PlaylistWithSongs(
        @Embedded val playlist: Playlist,
        @Relation(
             parentColumn = "playlistId",
             entityColumn = "songId",
             associateBy = @Junction(PlaylistSongCrossRef::class)
        )
        val songs: List<Song>
    )
    

    我的问题是:

    Error

    data class FileEntryWithTags(
            @Embedded val fileEntry: FileEntry,
            @Relation(
                    parentColumn = FileEntry.COLUMN_UUID,
                    entityColumn = Tag.COLUMN_ID,
                    associateBy = @Junction(FileEntryTagCrossRef::class)
            )
            val tags: List<Tag>
    )
    
    0 回复  |  直到 4 年前
        1
  •  22
  •   Slaw    4 年前

    看起来Android文档中有个错误。这个 Annotations - Kotlin Programming Language Kotlin参考资料中的页面告诉我们:

    如果注释用作另一个注释的参数,则其名称的前缀不会为 @ 人物:

    annotation class ReplaceWith(val expression: String)
    
    annotation class Deprecated(
            val message: String,
            val replaceWith: ReplaceWith = ReplaceWith(""))
    
    @Deprecated("This function is deprecated, use === instead", ReplaceWith("this === other"))
    

    所以你的代码应该是:

    data class FileEntryWithTags(
            @Embedded val fileEntry: FileEntry,
            @Relation(
                    parentColumn = FileEntry.COLUMN_UUID,
                    entityColumn = Tag.COLUMN_ID,
                    associateBy =  Junction(FileEntryTagCrossRef::class)
            )
            val tags: List<Tag>
    )