我想知道如果我有類似的情況下,我如何可以防止循環隱式轉換?在隱式轉換中防止循環
編輯:這是用於在用作ORTO實體的ORM實體和案例類之間轉換的一些上下文。
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")