OnChainSubstitute
scalus.compiler.OnChainSubstitute
final class OnChainSubstitute(substitute: AnyRef, selfAs: Class[_] = ...) extends StaticAnnotation
Marks a trait/class as having an on-chain substitute object.
When the Scalus plugin compiles a method call on a type annotated with @OnChainSubstitute, it redirects the call to the substitute @Compile object. The substitute object must define methods matching the original trait's methods, with an additional self first parameter.
On-chain, self receives the actual qualifier value (cast via typeProxy if selfAs is specified). Off-chain, the trait works as normal OOP — the annotation is ignored.
Value parameters
- selfAs
-
The on-chain type for
self. The plugin inserts a zero-costtypeProxycast from the trait type toselfAs. Defaults toNothing(no cast,selfkeeps the original type). Example:@OnChainSubstitute(OnChainCellOps, classOf[ScriptContext]) trait CellContext { def requireSignedBy(pkh: PubKeyHash): Unit } @Compile object OnChainCellOps { def requireSignedBy(self: ScriptContext, pkh: PubKeyHash): Unit = require(self.txInfo.isSignedBy(pkh), "missing signature") } - substitute
-
A
@Compile objectproviding on-chain implementations for each trait method.
Attributes
- Graph
-
- Supertypes
-
trait StaticAnnotationclass Annotationclass Objecttrait Matchableclass Any
In this article