Questions tagged [implicits]

Anything related to Scala implicit parameters or conversions

Implicits parameters and conversions are a feature of the Scala language. An implicit parameter is one that can be automatically inferred based on its type and the values in scope, without you needing to pass in the value of the argument explicitly. An implicit conversion function converts one type to another automatically on-demand, without needing to call the function explicitly.

170 questions
0
votes
1 answer

Scala: implicit conversion not made?

[error] DeviceAffiliationCluster.scala:56: value ask is not a member of akka.actor.ActorRef [error] def ask(msg: Any): Future[Any] = deviceRegion.ask(msg) [error] ^ [warn]…
tpdi
  • 34,554
  • 11
  • 80
  • 120
0
votes
2 answers

implicit function that compares two closed classes in Scala

I am working with a Location object from another closed API and it already has a toString() method that returns a String. I just want an implicit function that can compare two Location instances by comparing their toString() values. So I will be…
0
votes
1 answer

Conditionally generating implicits in scala

I am working on a system of chained implicit functions, which is similar to the simplified example below. The test c1.payload == c2.payload represents a test I need to do that is not in "type-space"; I had expected that I would drop into a macro for…
eje
  • 945
  • 11
  • 22
0
votes
1 answer

Scala covariance losing type constraints

I wonder how can I preserve my type constraints while trying to workaround over 'Covariant type parameter in a wrong position problem' situation. Here is the code: trait Converter[SourceType, +JobType <: ConverterJobType[SourceType]] { def…
Deil
  • 492
  • 4
  • 14
0
votes
1 answer

Why type equality check with implicitly fails?

I'd like to do some calculation on type level during runtime. So I define wrapper classes and implicit definitions for them. But I could not understand why type information is lost during computation sealed trait Solve[In] { type Out } implicit…
ayvango
  • 5,867
  • 3
  • 34
  • 73
0
votes
1 answer

How to set encoder for Spark dataset when importing csv or txt file

I'm having an issue with this part of the Spark Mllib code from the docs (https://spark.apache.org/docs/latest/ml-collaborative-filtering.html), using either csv or txt files: val ratings = …
0
votes
1 answer

Scalaz implementation of Semigroup using advanced Scala features

I was digging through the implementation of Monoids in Scalaz. I came across the |+| operator that is supposed to come out of the box if you define the append operation on Monoid. The definition of this operator is in SemigroupSyntax. That class…
zaxme
  • 1,065
  • 11
  • 29
0
votes
2 answers

Pure Type Parameters and Mixed Type parameter with member

After watching a youtube video with the title Scala Type Members vs Type Parameters. I wrote the following. Purely Type parameter version works fine trait Joiner[Elem,R] { def join(xs: Seq[Elem]): R } object Program { def doJoin[T,R]…
lawal
  • 952
  • 10
  • 19
0
votes
1 answer

Setting implicit instance variable for Scala's object

I have a Scala object with a bunch of utility-methods, each method makes use of an implicit method parameter s object MyObject { def a(implicit s:String) = ??? def b(implicit s:String) = ??? def c(implicit s:String) = ??? } I don't like that…
Raphael Roth
  • 26,751
  • 15
  • 88
  • 145
0
votes
1 answer

Implicitly passing in values from containing classes into the classes fields?

Consider a class School. If we were to add a Pupil field to the school class is it possible to implicity pass a reference to the School object into the Pupil object. So rather than doing class School() { val pupil:Pupil = new Pupil(this) } We…
newlogic
  • 807
  • 8
  • 25
0
votes
2 answers

Scala class with a private constructor and implicit parameter

I would like to add an implicit parameter to a class with a private constructor. Here as a simplified example: class A[T] private(a:Int){ def this()=this(0) } If I would like to apply Pimp my library pattern to T with Ordered[T], I would need to…
Krle
  • 70
  • 7
0
votes
1 answer

Why Scala's compiler is not able to use an implicit conversion of an inherited type?

I am currently digging in the implicit topic, and got myself stuck with this case : class A class B extends A object A { implicit def b2String(b: B): String = "B" } object B { implicit def a2String(a: A): String = "A" } val s: String = new B //…
Francis Toth
  • 1,595
  • 1
  • 11
  • 23
0
votes
1 answer

How does the Scala compiler synthesize implicit evidence with `<:<`?

Given this (admittedly contrived) code fragment in Scala: object Main extends App { class X { def foo = 1 } def f[A](value: A)(implicit ev: A <:< X) = { value.foo } println(f(new X())) } What does the Scala compiler do to make this pass?…
Michiel Borkent
  • 34,228
  • 15
  • 86
  • 149
0
votes
2 answers

Using implicits to inject a method at multiple levels of a trait hierarchy

I'm trying to take an existing hierarchy of traits (which I cannot control) and inject a method at the top level (and then override that method at each point in the hierarchy that needs custom treatment). These traits are used implicitly. Below is…
0
votes
2 answers

Parser combinators prevent mapping of strings

import scala.util.parsing.combinator._ object SimpleArith extends JavaTokenParsers { "abc".map(identity) produces type mismatch; found : String("abc") required: ?{def map: ?} Note that implicit conversions are not applicable because…