typeclass

How to avoid calling asInstanceOf in Scala

Deadly 提交于 2021-02-08 14:08:52
问题 Here is a simplified version of my code. How can I avoid to call asInstanceOf (because it is a smell for a poorly design solution) ? sealed trait Location final case class Single(bucket: String) extends Location final case class Multi(buckets: Seq[String]) extends Location @SuppressWarnings(Array("org.wartremover.warts.AsInstanceOf")) class Log[L <: Location](location: L, path: String) { // I prefer composition over inheritance // I don't want to pass location to this method because it's a

Dealing with implicit typeclass conflict

牧云@^-^@ 提交于 2021-02-08 08:58:20
问题 I'm trying to deal with an ambiguous implicits problem, and (relatedly) figure out what best practise should be for parameterizing typeclasses. I have a situation where I am using a typeclass to implement a polymorphic method. I initially tried the approach below: abstract class IsValidTypeForContainer[A] object IsValidTypeForContainer { implicit val IntIsValid = new IsValidTypeForContainer[Int] {} implicit val DoubleIsValid = new IsValidTypeForContainer[Double] {} } abstract class

In scala 2.13, why is it sometimes impossible to summon type class explicitly?

牧云@^-^@ 提交于 2021-02-08 04:42:13
问题 Here is a simple example in shapeless 2.3.3: val book = ("author" ->> "Benjamin Pierce") :: ("title" ->> "Types and Programming Languages") :: ("id" ->> 262162091) :: ("price" ->> 44.11) :: HNil val v1 = book.values assert(v1.head == "Benjamin Pierce") // works fine // summoning Values[_] type class explicitly, the HList & TypeTag are optional case class HasValues[T <: HList: TypeTag](v: T) { def vs(implicit v: Values[T]): Values[T] = v } val _vs = HasValues(book).vs val v2 = book.values(_vs)

Initialize a type variable with dynamic / concrete type

∥☆過路亽.° 提交于 2021-02-07 20:55:14
问题 I am learning Scala and I was trying to create a type class to solve the "Every animal eats food, but the type of food depends on the animal" problem. I have an Eats type class with context bounds: trait Eats[A <: Animal, B <: Edible] object Eats { def apply[A, B]: Eats[A, B] = new Eats[A, B] {} } with both Animal and Edible being abstract classes. The (reduced) Animal interface looks something like this abstract class Animal { type This // concrete type def eat[A <: Edible](food: A)(implicit

Initialize a type variable with dynamic / concrete type

故事扮演 提交于 2021-02-07 20:54:04
问题 I am learning Scala and I was trying to create a type class to solve the "Every animal eats food, but the type of food depends on the animal" problem. I have an Eats type class with context bounds: trait Eats[A <: Animal, B <: Edible] object Eats { def apply[A, B]: Eats[A, B] = new Eats[A, B] {} } with both Animal and Edible being abstract classes. The (reduced) Animal interface looks something like this abstract class Animal { type This // concrete type def eat[A <: Edible](food: A)(implicit

Why is it fair to think of just locally small cartesian closed categories in Haskell for the Curry class?

元气小坏坏 提交于 2021-02-07 19:14:41
问题 Control.Category.Constrained is a very interesting project that presents the class for cartesian closed categories - Curry. Yet, I do not see why we think of all cartesian closed categories which allow curry and uncurry ( Hom(X * Y, Z) ≅ Hom(X, Z^Y) in terms of category theory). Wikipedia says that such property holds only for locally small cartesian closed categories. Under this post many people suggest that Hask itself is not locally small (on the other hand, everyone says that Hask is not

Why is it fair to think of just locally small cartesian closed categories in Haskell for the Curry class?

泪湿孤枕 提交于 2021-02-07 19:13:55
问题 Control.Category.Constrained is a very interesting project that presents the class for cartesian closed categories - Curry. Yet, I do not see why we think of all cartesian closed categories which allow curry and uncurry ( Hom(X * Y, Z) ≅ Hom(X, Z^Y) in terms of category theory). Wikipedia says that such property holds only for locally small cartesian closed categories. Under this post many people suggest that Hask itself is not locally small (on the other hand, everyone says that Hask is not

How to put constraints on type variable of kind `Constraint`?

人走茶凉 提交于 2021-02-07 05:02:20
问题 I'm playing around with the ConstraintKinds extension of GHC. I have the following data type, which is just a box for things fulfilling some one parameter constraint c : data Some (c :: * -> Constraint) where Some :: forall a. c a => a -> Some c For example, I could construct a box with some kind of number (arguably not very useful). x :: Some Num x = Some (1 :: Int) Now, as long as c includes the constraint Show , I could provide an instance of Show (Some c) . instance ??? => Show (Some c)

Implement product type in Scala with generic update function working on its parts

╄→гoц情女王★ 提交于 2021-02-07 03:36:53
问题 In Scala, I need to create a product type & that represents a compound value, e.g.: val and: String & Int & User & ... = ??? I.e. and should have a String part and an Int part and a User parts inside. This is similar to Scala with keyword: val and: String with Int with User with ... = ??? Having such product type I need a way to, having a function A => A , apply it to some product value and get that product back with A part altered. It implies that each type in product must be unique - that's

Implement product type in Scala with generic update function working on its parts

烂漫一生 提交于 2021-02-07 03:36:26
问题 In Scala, I need to create a product type & that represents a compound value, e.g.: val and: String & Int & User & ... = ??? I.e. and should have a String part and an Int part and a User parts inside. This is similar to Scala with keyword: val and: String with Int with User with ... = ??? Having such product type I need a way to, having a function A => A , apply it to some product value and get that product back with A part altered. It implies that each type in product must be unique - that's