Scala: macro to create an instance from a class body

后端 未结 2 1345
误落风尘
误落风尘 2021-01-27 16:20

I am building a DSL in Scala, and for that, I need to store \"instances\" of a class (Parent in this case), except these \"instances\" must be re-created several ti

2条回答
  •  北恋
    北恋 (楼主)
    2021-01-27 16:50

    If I understood correctly, you just need to change object to def in the ObjectA block:

    class User {
      var userVar = 13
      def makeParent = new Parent {
        func(1)
        func(userVar)
      }
    
      construct(makeParent)
    }
    

    and it'll do what you want.

提交回复
热议问题