如何为泛型类型定义隐式 (given) 转换?

huangapple go评论63阅读模式
英文:

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 &lt;: Product](
                                   decisionDmn: DecisionDmn[In, _]
                                ): DmnTesterObject[In] =
    DmnTesterObject(decisionDmn, defaultDmnPath(decisionDmn.decisionDefinitionKey))

I tried:

  given Conversion[DecisionDmn[_ &lt;: Product, _], DmnTesterObject[_ &lt;: Product]] with
    def apply(decisionDmn: DecisionDmn[_ &lt;: Product, _]): DmnTesterObject[_ &lt;: Product] =
      DmnTesterObject(decisionDmn, defaultDmnPath(decisionDmn.decisionDefinitionKey))

This compiles but does not work when running:

Exception in thread &quot;main&quot; java.lang.NoSuchMethodError: &#39;camundala.dmn.DmnTesterConfigCreator$DmnTesterObject camundala.dmn.DmnTesterConfigCreator.toTesterObjectScenario$(camundala.dmn.DmnTesterConfigCreator, camundala.bpmn.DecisionDmn)&#39;
	at valiant.bpmn.dmn.BpmnDmnTester$.toTesterObjectScenario(BpmnDmnTester.scala:11)

This does not even compile:

  given Conversion[DecisionDmn[_ &lt;: Product, _], DmnTesterObject[_ &lt;: Product]] with
    def apply[In &lt;: 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 &lt;: Product

以下是如何为给定的实例添加类型参数的方式:

given [In &lt;: 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 &lt;: Product.

This is how you can add a type parameter to a given instance:

given [In &lt;: Product]: Conversion[DecisionDmn[In, _], DmnTesterObject[In]] with
  def apply(decisionDmn: DecisionDmn[In, _]): DmnTesterObject[In] = ???

huangapple
  • 本文由 发表于 2023年2月24日 16:19:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/75554111.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定