簡體   English   中英

如何在scala中通過類型參數的類型成員約束參數?

[英]How to constrain argument by type member of type parameter in scala?

函數reduceTo獲取一個類型參數,指示應返回的時間序列的類型,但函數splitInto需要一個Period值。 我想確保它們是連貫的,所以你不能這樣做:

reduceTo[MonthlySeries](period=Weekly)  // not ok
reduceTo[MonthlySeries](period=Monthly) // but this is ok
reduceTo[MonthlySeries]  // even better, derive the period from the type param

類型參數應該足夠了,但我似乎無法從參數化類型中獲取我需要的Period信息。 這是簡化的代碼:

sealed trait Period
case class Daily extends Period
case class Monthly extends Period


sealed abstract class TimeSeries {
    type T
}
case class DailySeries extends TimeSeries {
    type T = Daily.type
}
case class MonthlySeries extends TimeSeries {
    type T = Monthly.type
}

implicit class DailySeriesOps(lhs: DailySeries) {

    def reduceTo[G: TimeSeries](period: Period):G {
        // instead of sending the period in, how do I get
        // the period value from the type G?
        // Or at least constrain the period value?
        val s = splitInto(period, lhs)
        val reduced : G = ...
        // this is ugly
        assert(reduced.period == period, s"Reducing to $period but timeseries is ${reduced.period}")
    }
}

最初我認為路徑依賴類型會有所幫助

def reduceTo[G: TimeSeries](period: G.T)

但是根據我的理解,這將需要每日將在DailySeries中定義的類,因為它在其他環境中使用並不理想。

另一個不起作用的想法是不發送period參數但是從G獲取值,但由於它的實例尚不存在,它不起作用

def reduceTo... = {
  val period = G.T
  ...
}

[更新]

也試過了

def reduceTo[G : TimeSeries](period: TimeSeries#T: G = {
    val ts = reduce[F](period.asInstanceOf[GroupedPeriod], lhs)

但是,這在reduceTo調用上的類型不匹配時無法編譯:

type mismatch;
[error]  found   : com.example.metrics.Periods.Monthly.type
[error]  required: com.example.metrics.calculations.TimeSeries#T

有任何想法嗎?

根據您的評論,如果GroupedSeries具有type T字段,您可以使用period: G#T ,或者如果不是,則需要添加其他通用參數並使用如下內容:

  def reduceTo[G <: GroupedSeries[_], T <: TimeSeries](period: T#T): G = {
    //Your code
  }

有了這個我的編譯器說:

DailySeriesOps().reduceTo[DailySeries,DailySeries](Monthly) // I don't compile that
DailySeriesOps().reduceTo[DailySeries,DailySeries](Daily) // that i will
DailySeriesOps().reduceTo[MonthlySeries,MonthlySeries](Monthly) // that i will too
DailySeriesOps().reduceTo[MonthlySeries,MonthlySeries](Daily) // and not this one

但你的問題不容易再現,所以我不知道這是否可以回答你的問題,我希望它有所幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM