英文:
How to define a implicit (given) Conversion for Generic Types?
问题
我想使用 Scala 3 的隐式转换。
我有以下的结构,我想要迁移:
implicit def toTesterObjectScenario[In <: Product](
decisionDmn: DecisionDmn[In, _]
): DmnTesterObject[In] =
DmnTesterObject(decisionDmn, defaultDmnPath(decisionDmn.decisionDefinitionKey))
我尝试过:
given Conversion[DecisionDmn[_ <: Product, _], DmnTesterObject[_ <: Product]] with
def apply(decisionDmn: DecisionDmn[_ <: Product, _]): DmnTesterObject[_ <: Product] =
DmnTesterObject(decisionDmn, defaultDmnPath(decisionDmn.decisionDefinitionKey))
这个编译通过,但在运行时不起作用:
Exception in thread "main" java.lang.NoSuchMethodError: 'camundala.dmn.DmnTesterConfigCreator$DmnTesterObject camundala.dmn.DmnTesterConfigCreator.toTesterObjectScenario$(camundala.dmn.DmnTesterConfigCreator, camundala.bpmn.DecisionDmn)'
at valiant.bpmn.dmn.BpmnDmnTester$.toTesterObjectScenario(BpmnDmnTester.scala:11)
这个甚至都无法编译通过:
given Conversion[DecisionDmn[_ <: Product, _], DmnTesterObject[_ <: Product]] with
def apply[In <: Product](decisionDmn: DecisionDmn[In, _]): DmnTesterObject[In] =
DmnTesterObject(decisionDmn, defaultDmnPath(decisionDmn.decisionDefinitionKey))
这是否不可能或者该如何实现?
英文:
I want to use the Scala 3 Implicit Conversion.
I have the following construct that I want to migrate:
implicit def toTesterObjectScenario[In <: Product](
decisionDmn: DecisionDmn[In, _]
): DmnTesterObject[In] =
DmnTesterObject(decisionDmn, defaultDmnPath(decisionDmn.decisionDefinitionKey))
I tried:
given Conversion[DecisionDmn[_ <: Product, _], DmnTesterObject[_ <: Product]] with
def apply(decisionDmn: DecisionDmn[_ <: Product, _]): DmnTesterObject[_ <: Product] =
DmnTesterObject(decisionDmn, defaultDmnPath(decisionDmn.decisionDefinitionKey))
This compiles but does not work when running:
Exception in thread "main" java.lang.NoSuchMethodError: 'camundala.dmn.DmnTesterConfigCreator$DmnTesterObject camundala.dmn.DmnTesterConfigCreator.toTesterObjectScenario$(camundala.dmn.DmnTesterConfigCreator, camundala.bpmn.DecisionDmn)'
at valiant.bpmn.dmn.BpmnDmnTester$.toTesterObjectScenario(BpmnDmnTester.scala:11)
This does not even compile:
given Conversion[DecisionDmn[_ <: Product, _], DmnTesterObject[_ <: Product]] with
def apply[In <: Product](decisionDmn: DecisionDmn[In, _]): DmnTesterObject[In] =
DmnTesterObject(decisionDmn, defaultDmnPath(decisionDmn.decisionDefinitionKey))
Is this not possible or how it is done?
答案1
得分: 3
你需要一个特定类型的实例,类型为 Conversion[DecisionDmn[In, _], DmnTesterObject[In]
,其中 In <: Product
。
以下是如何为给定的实例添加类型参数的方式:
given [In <: Product]: Conversion[DecisionDmn[In, _], DmnTesterObject[In]] with
def apply(decisionDmn: DecisionDmn[In, _]): DmnTesterObject[In] = ???
英文:
You need a given instance of type Conversion[DecisionDmn[In, _], DmnTesterObject[In]
where In <: Product
.
This is how you can add a type parameter to a given instance:
given [In <: Product]: Conversion[DecisionDmn[In, _], DmnTesterObject[In]] with
def apply(decisionDmn: DecisionDmn[In, _]): DmnTesterObject[In] = ???
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论