```buildTypeParameter { val paramBuilder = thi...
# compiler
m
Copy code
buildTypeParameter {
    val paramBuilder = this
    paramBuilder.source = old.fir.source
    paramBuilder.moduleData = old.fir.moduleData
    paramBuilder.origin = FirDeclarationOrigin.Plugin(KEY)
    paramBuilder.name = old.name
    paramBuilder.symbol = key
    paramBuilder.containingDeclarationSymbol = function
    paramBuilder.variance = old.fir.variance
    paramBuilder.isReified = old.fir.isReified
    paramBuilder.bounds.addAll(old.fir.bounds)
}
I’ve never developed a plugin before, so I’m unfamiliar with the API. Data classes have the
copy
method to let me copy an instance with just the fields I want overridden, so I feel like Kotlin would also have a shorter way for this too but cannot find it myself.
d
FIR elements aren't data classes, so they don't have a builtin copy. Moreover, the real elements implementations are hidden and only the abstract classes for them are exposed. For copying elements there is a copy-builder API in addition to just builder. The convention name for them is
buildElementNameCopy(originalElement) {...}
. For type parameters it would be
Copy code
buildTypeParameterCopy(old) {
    origin = ...
    symbol = key
}
Link to source.
m
Copy code
@OptIn(SymbolInternals::class) buildTypeParameter {
    val paramBuilder = this
    paramBuilder.source = old.fir.source
    paramBuilder.moduleData = old.fir.moduleData
    paramBuilder.origin = FirDeclarationOrigin.Plugin(KEY)
    paramBuilder.name = old.name
    paramBuilder.symbol = key
    paramBuilder.containingDeclarationSymbol = function
    paramBuilder.variance = old.fir.variance
    paramBuilder.isReified = old.fir.isReified
    paramBuilder.bounds.addAll(old.fir.bounds)
}
@dmitriy.novozhilov So I should be able to replace this with
Copy code
@OptIn(SymbolInternals::class) buildTypeParameterCopy(old.fir) {
    val paramBuilder = this
    paramBuilder.origin = FirDeclarationOrigin.Plugin(KEY)
    paramBuilder.symbol = key
    paramBuilder.containingDeclarationSymbol = function
}
This, right? Thanks!
d
Well, you don't need
val paramBuilder = this
. But generally yes.
❤️ 1
m
@dmitriy.novozhilov I just like to always do that for clarity. Thanks for having a look and for the explanation 🌻