【发布时间】:2015-01-16 21:16:53
【问题描述】:
我想知道我是否有与以下类似的场景,如何防止循环隐式转换?
编辑:一些上下文,用于在用作 ORM 实体的某些类和用作 DTO 的案例类之间进行转换。
class Author(var name: String) {
def books : List[Book] = List(new Book("title", this))// get books
}
class Book(var title: String, var author: Author)
case class DTOBook(title: String, author: Option[DTOAuthor])
case class DTOAuthor(name: String, books: List[DTOBook])
implicit def author2Author(author: Author) : DTOAuthor = {
DTOAuthor(author.name, author.books.map(x => x : DTOBook) : List[DTOBook])
}
implicit def book2Book(book: Book) : DTOBook = {
DTOBook(book.title, Option(book.author : DTOAuthor))
}
val author: DTOAuthor = new Author("John Brown")
【问题讨论】: