Advice on refactoring this code snippet



fun ServiceProxy.rewriter(configure: RewriteInterceptor.() -> Unit) {
  val interceptor = RewriteInterceptor()

fun ServiceProxy.request(configure: RequestInterceptor.() -> Unit) {
  val interceptor = RequestInterceptor()


(RewriteInterceptor and RequestInterceptor implement the same interface.)


I don't know what do you want to achieve with refactoring, but I would probably create "intercept" helper method like this:

fun <T:Interceptor> ServiceProxy.intercept(interceptor: T, configure: T.()->Unit) {   interceptor.configure()   getInterceptors().add(interceptor) }

and use it like this to build DSL

fun ServiceProxy.rewrite(configure: RewriteInterceptor.()->Unit) = intercept(RewriteInterceptor(), configure)


Thanks, exactly what I was looking for.