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
1
vote
1 answer

Scala nested implicit type parameters

Can you please explain the type T in this method definition? This is from gatling. I know that colon is for context bound values. here I see them nested. What is # for? implicit def stringToExpression[T: TypeCaster:…
mert inan
  • 1,537
  • 2
  • 15
  • 26
1
vote
2 answers

Scala - Overriding trait method using a sub context bound method

For sealed trait User {...} sealed trait Trader extends User {...} trait AuthObject trait AuthUserObject { def authorize[U <: User](u: U): Boolean } trait AuthTraderObject extends AuthUserObject { def authorize[T <: Trader](t: T):…
Orar
  • 940
  • 1
  • 10
  • 13
1
vote
0 answers

ScalaC exception on attempt to use upper bound and context bound at the same time

I get scalac exception on attempt to use upper bound and context bound at the same time. Is it even allowed ? I'm on Scala 2.11.8 Consider this import spray.json._ abstract class CrossRefMessage case class CrossRefResponse[T <:…
expert
  • 29,290
  • 30
  • 110
  • 214
1
vote
1 answer

Scala isInstanceOf[T] function fail to use bounded ClassTag/TypeTag

The follow code: abstract class Foo[T: ClassTag] { def v(a: Any): Any = a match { case _ if a.isInstanceOf[T] => Some(a) case _ => None } } yield the following in compilation: Warning: abstract type T is unchecked since it…
tribbloid
  • 4,026
  • 14
  • 64
  • 103
1
vote
1 answer

Self referential View/Context bound

I am actually not clear whether the following were a view or context bound. Also not clear what the self referential aspect (re-referencing Model) is doing ;) : abstract class Model[M <: Model[M]] extends Transformer { An explanation on the…
WestCoastProjects
  • 58,982
  • 91
  • 316
  • 560
1
vote
1 answer

Scala: explicitly specifying context bound in a second constructor

Why this is allowed: class Foo[O: Option](s: String) new Foo[Any]("foo")(None) while this is not: class Foo[O: Option](s: String) { def this() = this("foo")(None) } compiler message: Foo[O] does not take parameters Is there any way to…
eprst
  • 733
  • 6
  • 14
0
votes
1 answer

No implicit Ordering defined for ord

I want to define a collection class and require its element being Ordered Considering the code below: class MyCollection[K: Ordered[K]] { def func(seq: Seq[K]): Unit = { seq.sorted } } The compiler will report error No implicit Ordering…
Ziqi Liu
  • 2,931
  • 5
  • 31
  • 64
0
votes
2 answers

What Does a Variable Defined before a Scala Function Mean?

Learning Scala from the Scala for Data Science book and the companion Github repo, here I am particularly talking about this function, copied below for reference. def fromList[T: ClassTag](index: Int, converter: String => T): DenseVector[T] = …
Della
  • 1,264
  • 2
  • 15
  • 32
0
votes
0 answers

Context bound on trait

I would like to do something akin to trait Copyable[T] { extension (o: T) def copy: T } trait A : Copyable { // Context bound is not legal here... def foo(): A = copy } or trait A { this: (A: Copyable) => // Context bound is not legal…
Stephane Bersier
  • 710
  • 7
  • 20
0
votes
1 answer

Why view bound and context bound fails to detect implicits present in the context

I am new to scala and I am trying to implement a method method "append" which appends two numbers or string. The ideato practice on this code came from this post where I was referring an example of append method and Appendable trait used for…
Gunjan Shah
  • 5,088
  • 16
  • 53
  • 72
0
votes
0 answers

How to use `ClassTag` and type comparison operator, `<:` at the same time?

I have the following class defined, class FixedLengthQueue[T<:Hashable](maxLength: Int) { private val _outdated = ArrayBuffer.empty[T] def outdated: Array[T] = { val result = _outdated.toArray[T] _outdated.clear() result …
Max Wong
  • 694
  • 10
  • 18
0
votes
2 answers

Context Bound on a Generic Class Using Implicits

I am learning Scala in order to use it for a project. One thing I want to get a deeper understanding of is the type system, as it is something I have never used before in my other projects. Suppose I have set up the following code: // priority…
finite_diffidence
  • 893
  • 2
  • 11
  • 20
0
votes
1 answer

Type Error for Context Bounding with Priority Implicits

I have the following issue, and I am confused as to what is going: I have a priority implicit defined I use this priority implicit to impose a context bound I declare a case class with a default field, which has values that are covered by the…
finite_diffidence
  • 893
  • 2
  • 11
  • 20
0
votes
1 answer

Difference between [A: C] and [A[_]: C] context bounds

I'm a newbie, according to my lectures : class Test [T: Comparing] means that it requires an implicit value of type Comparing[T] that can be used in the methods of that class. With this Higher kinded type notation Question : What does this…
0
votes
1 answer

What is the meaning of a generic type like "C: ClassTag : Manifest"?

I found some snippets as follow: import org.json4s.DefaultFormats import org.json4s.jackson.JsonMethods._ import scala.io.Source import scala.reflect.ClassTag class ConfigLoader[C: ClassTag : Manifest](filePath: String) { def loadFromFile(): C…