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
118
votes
4 answers

What is a "context bound" in Scala?

One of the new features of Scala 2.8 are context bounds. What is a context bound and where is it useful? Of course I searched first (and found for example this) but I couldn't find any really clear and detailed information.
Jesper
  • 202,709
  • 46
  • 318
  • 350
23
votes
2 answers

How to get ClassTag form TypeTag, or both at same time?

I have some code like this: class ReflectiveJsonFormat[T:TypeTag] extends JsonFormat[T] { def write(x: T) : JsValue = { val t = typeOf[T] val getters = t.declarations.filter { s => s.isMethod && s.asMethod.isGetter } val mirror =…
Rob N
  • 15,024
  • 17
  • 92
  • 165
15
votes
3 answers

How do I get an instance of the type class associated with a context bound?

Note: I'm posing this question to answer it myself, but other answers are welcome. Consider the following simple method: def add[T](x: T, y: T)(implicit num: Numeric[T]) = num.plus(x,y) I can rewrite this using a context bound as follows def add[T:…
Aaron Novstrup
  • 20,967
  • 7
  • 70
  • 108
11
votes
2 answers

Context bounds shortcut with higher kinded-types

Is it possible to use the context bounds syntax shortcut with higher kinded-types? trait One { def test[W : ClassManifest]: Unit } // first-order ok trait Two { def test[W[_]: ClassManifest]: Unit } // not possible?? trait Six { def…
0__
  • 66,707
  • 21
  • 171
  • 266
8
votes
1 answer

What was the reason to restrict on combining implicit parameters and view/context bounds?

One of the recent commits to Scala master removes restriction on combining context/view bounds with implicit parameters. That's a great improvement that reduces amount of boilerplate, but what was the reason of making that restriction before, and…
Vasil Remeniuk
  • 20,519
  • 6
  • 71
  • 81
8
votes
2 answers

":" in type parameter

In scala-arm project, I see code like this: def managed[A : Resource : Manifest](opener : => A) : ManagedResource[A] = new DefaultManagedResource(opener) Can someone explain the meaning of [A : Resource : Manifest] ?
xiefei
  • 6,563
  • 2
  • 26
  • 44
7
votes
1 answer

How to avoid ambiguous conversion chains with multiple Type Class relationships?

In my library, I have three type classes: trait Monoid[T] { val zero : T def sum(x : T, y : T) : T } trait AbelianGroup[T] extends Monoid[T] { def inverse(x : T) : T def difference(x : T, y : T) : T } //represents types that are represents…
Nimrand
  • 1,748
  • 2
  • 16
  • 29
6
votes
2 answers

Situations when Manifest not available

def bar[T: Manifest](a: Array[T]) = Array.ofDim[T](3) class Foo bar(Array(new Foo)) //Array[Foo] = Array(null, null, null) Manifests seem to exist implicitly for arbitrary types, as shown above. Since we have a context bound, this implies that…
Luigi Plinge
  • 50,650
  • 20
  • 113
  • 180
5
votes
1 answer

Could not find implicit value while using Context Bound

I'm using the following code written in Scala 2.11.8: sealed trait Acceptable[T] object Acceptable { implicit object Int extends Acceptable[Int] implicit object String extends Acceptable[String] } case class Enc[T](f: T => Any) …
mixel
  • 25,177
  • 13
  • 126
  • 165
5
votes
1 answer

How can I apply a macro annotation to a case class with a context bound?

When I try to add a macro annotation to my case class: @macid case class CC[A: T](val x: A) I get the error: private[this] not allowed for case class parameters @macid is just the identity function, defined as a whitebox StaticAnnotation: import…
SII_of_SII
  • 83
  • 4
4
votes
2 answers

Transparent proxy to original type

I have an run time object of type {System.Runtime.Remoting.Proxies.__TransparentProxy} which is created from an instance of class which is inherited from ContextBoundObject. This class raise an event to some other object, I need to convert this…
hungryMind
  • 6,931
  • 4
  • 29
  • 45
4
votes
3 answers

Another subtype after a type bound in scala

class PEControl[T <: Data : Arithmetic](accType: T), this is a class definition from riscv-gemmini. The Data type is the basic data type in chisel, Arithmetic provides some arithmetic operation on Data, and abstract class Arithmetic[T <: Data]. What…
Phantom
  • 165
  • 1
  • 8
3
votes
1 answer

Scala: How to get context bound List[T] conversion working here?

This is my first question here so hope I provide enough detail. Feel free to ask for clarification. Taking the following into consideration, which works: implicit def optionBsonReader[T, U](implicit ev: BsonReader[T, U]) = new BsonReader[Option[T],…
dlouwers
  • 83
  • 1
  • 7
3
votes
1 answer

Is it possible to have two or more context bound classes in Scala

How do I specify that I need e.g. a ClassTag AND an Ordering for T? Example def sort[T: ClassTag Ordering](future: Future[Seq[T]]): Future[Seq[T]]
Chris W.
  • 2,266
  • 20
  • 40
3
votes
1 answer

NPE in spray-json because of recursive implicits (context bound issue?)

Perhaps I discovered a bug in spray-json. I get Null Pointer Exception when I'm trying to get json of an object that has field of type of itself. Example is: case class TestItem(subitems: Option[List[TestItem]]) object MyJsonProtocol extends…
expert
  • 29,290
  • 30
  • 110
  • 214
1
2 3 4