When is an empty HList not an HList?

Studying formlessness, I wonder why this does not compile:

def someHList[H <: HList]: H = HNil

since the HNil object extends the HNil trait that extends the HList?

What is the correct way to define a method in an attribute that returns some HList that is implemented only by the expanding class?

I would like to do something like the following:

trait Parent {
  def someHList[H <: HList]: H
}

object Child1 extends Parent {
  def someHList[H <: HList] = HNil
}

object Child2 extends Parent {
  def someHList[H <: HList] = 1 :: "two" :: HNil
}

Any advice is appreciated. Thank!

EDIT

To understand, as I understand it, what I defined in my original question:

1.) It is advisable not to indicate Hexplicitly in each implementation class, but let it be displayed (on the call site?).

2.) I would like to use HNil as the default implementation in the parent attribute, which can be overridden in subclasses. My example probably should have been:

trait Parent {
  def someHList[H <: HList]: H = HNil
}

object Child extends Parent {
  override def someHList[H <: HList] = 1 :: "two" :: HNill
}
+4
2

HNil HList. H.

def someHList[H <: HList]: H = HNil

type H, HList , HNil

, ,

, , ,

type H, HList member

, :

import shapeless._

trait Parent {
  type H <: HList
  def someHList: H
}

object Child1 extends Parent {
  type H = HNil
  def someHList: H = HNil
}

object Child2 extends Parent {
  type H = Int :: String :: HNil
  def someHList: H = 1 :: "two" :: HNil
}

Update

,

abstract class Parent[H <: HList](val someList: H)
object Child1 extends Parent(HNil: HNil)
object Child2 extends Parent(1 :: "two" :: HNil)

, HNil , object HNil HNil.type HNil,

+8

HList , :

trait Parent {
  def someHList: HList
}

object Child1 extends Parent {
  def someHList = HNil
}

object Child2 extends Parent {
  def someHList = 1 :: "two" :: HNil
}

:

trait Parent {
  def someHList: HList = HNil
}

object Child2 extends Parent {
  override def someHList = 1 :: "two" :: HNil
}
+4

Source: https://habr.com/ru/post/1663048/


All Articles