Questions tagged [context-bound]

Context bounds were introduced in Scala 2.8.0, and are typically used with the so-called type class pattern, a pattern of code that emulates the functionality provided by Haskell type classes, though in a more verbose manner.

Context bounds were introduced in Scala 2.8.0, and are typically used with the so-called type class pattern, a pattern of code that emulates the functionality provided by Haskell type classes, though in a more verbose manner.

References

Related Tags

55 questions
2
votes
1 answer

Chain of events / Proxy to original object

I have a class which is inherited from context bound object. Class has attribute on some properties. When some property is changed, PostProcess(IMessage msg, IMessage msgReturn) raise an event and from the event again a new property with same…
hungryMind
  • 6,931
  • 4
  • 29
  • 45
2
votes
2 answers

How to define a context bound with a higher kinded Type (Type Constructor)

I have tried the following def test[Option[T]: Ordering](value1: Option[T], value2: Option[T]) = { val e = implicitly(Ordering[Option[T]].compare(value1, value2)) } but does not work ? Any idea what's the issue ? EDIT This of course works def…
MaatDeamon
  • 9,532
  • 9
  • 60
  • 127
2
votes
1 answer

could not find implicit value for evidence parameter of type org.scalacheck.Arbitrary

I was trying to use a method called random with the following signature: def random[T: WeakTypeTag: Arbitrary]: T On a case class named Checking but I get this : could not find implicit value for evidence parameter of type …
Ismail H
  • 4,226
  • 2
  • 38
  • 61
2
votes
1 answer

Shorthand for defining scala context bound in trait

In scala abstract class, if you want to define a context bound, you can simply use, e.g. [T: ClassTag] in parameter, however this is not possible in trait: trait Foo[T: ClassTag] Error:(11, 35) traits cannot have type parameters with context bounds…
tribbloid
  • 4,026
  • 14
  • 64
  • 103
2
votes
2 answers

Implicit resolution for different orders of case class and companion

Usually, I write first a case class and then the companion object in the same file, right below. But when trying to import an implicit declared in the companion, I'm forced to switch the order of declaration (and I don't want, obviously). What is…
ale64bit
  • 6,232
  • 3
  • 24
  • 44
2
votes
2 answers

implicit resolution for a function argument

I tried to implement mergesort in Scala. I got to the following: def mergeSort[A: Ordering](as: List[A]): List[A] = as match { case Nil => as case head :: Nil => as case _ => { val (l, r) = split(as) merge(mergeSort(l),…
Martijn
  • 11,964
  • 12
  • 50
  • 96
2
votes
2 answers

Impose more than one generic type constraint on a Scala type parameter

I want to do the following stuff using Scala's context-bound pattern: class Polynomial[T: Ring] { def apply[X: Ring with Includes[T]](x: X): X = ... ... } This is a polynomial class which requires the coefficients are…
Tongfei Chen
  • 613
  • 4
  • 14
2
votes
1 answer

What's the advantage of a Scala "context bound" over a normal parameter?

I'm reading about the context bounds and implicit parameters that are supposed to work like type classes. The examples I see often use Ordering[T]. Something like: foo[T : Ordering](a: T, b: T) which is sugar for foo[T](a: T, b: T)(implicit ord:…
Rob N
  • 15,024
  • 17
  • 92
  • 165
2
votes
2 answers

Scala context bound unexpectedly not working

I tried to define a function that would check whether a generic Seq is sorted. I came up with this: import Ordering.Implicits._ def isOrdered[A: Ordering](seq: Seq[A]): Boolean = seq.sliding(2).map({ case List(a, b) => b > a…
user510159
  • 1,379
  • 14
  • 26
2
votes
1 answer

Accessing type constructor parameter of context bounds with higher kinded types

Is it possible to access the type constructor parameter of a higher-kinded type in a context bound? I am looking to define a trait that takes a higher-kinded type, and has a method that returns an instance of the constructor parameter. case class…
Woodz
  • 1,029
  • 10
  • 24
1
vote
3 answers

Context bound in Scala

I am learning Context bound in Scala. In the below code, I am invoking multiplication operator on the integer parameter. But it errors out. 'a' is considered as type parameter; but it is actually not as per my understanding. Can someone please…
user3103957
  • 636
  • 4
  • 16
1
vote
1 answer

Shapeless: Mapping an natural transformation over a KList

Is there a way to map a natural transformation (e.g. a Option ~> Either[String, *]) over a KList (e.g. a HList with a UnaryTCConstraint)? That would seem to be the natural thing to do with a KList. Specifically I was trying to do the…
mrArkwright
  • 254
  • 2
  • 10
1
vote
1 answer

What is the difference between the two definitions

What's the difference between the two definitions? def f[F[_]: Async](...) = ??? def f[F[_]](...)(implicit F: Async[F]) = ??? later I can use Async[F].async {} in first case and F.async {} in second But i can't figure out the difference. Thx.
cr4zsci
  • 11
  • 3
1
vote
1 answer

Type erasure in a nested list with a given context bound

I am going through the book Scala with Cats. I am trying to understand the subtleties of the scala type system. I came up with the following example: object Example extends App { sealed trait Serializer[T] { def serialize(seq: List[T]):…
finite_diffidence
  • 893
  • 2
  • 11
  • 20
1
vote
3 answers

Understanding Mixed Context Bounds of Seq[AnyVal] and Seq[String]

Suppose I have some function that should take a sequence of Ints or a sequence of Strings. My attempt: object Example extends App { import scala.util.Random val rand: Random.type = scala.util.Random // raw data val x = Seq(1, 2, 3, 4,…
finite_diffidence
  • 893
  • 2
  • 11
  • 20