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

Can a scala self-type be satisfied via delegation?

Suppose I have (and this is rather contrived) trait DbConnection { val dbName: String val dbHost: String } class Query { self: DbConnection => def doQuery(sql: String) { // connect using dbName, dbHost // perform query …
Josh
  • 2,077
  • 1
  • 17
  • 21
1
vote
2 answers

Scala class wrapping a partially applied constructor - how to use it to create API methods?

I'm trying to create a simple api for dealing with intervals of hours. (I'm aware of joda time, and I'm not trying to reinvent it. This is rather an exercise). What I would like to achieve is this: (1) assert(from("20:30").to("20:50") ==…
teo
  • 1,393
  • 1
  • 15
  • 25
1
vote
3 answers

Integrating scala with java

I'm new to Scala. I need help in resolving this issue. I have built a project which is written in Scala and I'm trying to integrate it with a Java project. I have taken the dependency on the build scala jar in my Java project in eclipse. However…
Bourne
  • 1,905
  • 13
  • 35
  • 53
1
vote
1 answer

Scala implicit context resolution

I have found there rules for implicict resolution i SLS: if T is a compound type T1 with ... with Tn, the union of the parts of T1, ..., Tn, as well as T itself if T is a parameterized type S[T1, ..., Tn], the union of the parts of S and T1, ...,…
zlaja
  • 1,351
  • 3
  • 13
  • 23
1
vote
2 answers

Groovy equivalent for Scala implicit parameters - extended

This question extends my previous one Groovy equivalent for Scala implicit parameters Not sure if this is the right way to develop from a previous topic, but anyway.. I am looking for a way to express in groovy something like this: // scala object A…
Ghiro
  • 500
  • 1
  • 4
  • 11
1
vote
1 answer

Safely chaining implicit conversions

You can do this to get implicit conversions to chain: package language object chainedImplicits { implicit def chainImplicits[A, B, C](a: A)(implicit conv1: A => B, conv2: B => C): C = conv2(conv1(a)) } but this is obviously not safe. I can't see…
Ptharien's Flame
  • 3,246
  • 20
  • 24
0
votes
0 answers

Could not find implicit value for parameter defined in companion object

In the minimal type class example below, scala 2.12.6 / sbt 1.2.1 complains could not find implicit value for parameter tc: tryout.Tryout.TypeClassTrait[Int]. If I uncomment the println line, it compiles. I was expecting this to work without the…
Georg
  • 966
  • 8
  • 25
0
votes
3 answers

Scala: overridable implicits in for-comprehension

I am trying to define implicits by API and want to allow client to override them. Here is a discussion: [How to override an implicit value, that is imported? I have tried it with simplest solution. It works as expected. Now I want to define…
Alexandr
  • 9,213
  • 12
  • 62
  • 102
0
votes
2 answers

Why am I getting a " diverging implicit expansion" error when trying to sort instances of an ordered class?

There are a lot of questions on the subject but after one hour of reading, I still cannot understand what I am doing wrong. Here is a minimal example of the code I have (Scala 2.11): object Anomalies { sealed abstract class AnomalyType(val…
Oli
  • 9,766
  • 5
  • 25
  • 46
0
votes
1 answer

Implicit conversions in generic function

I need to convert from Any to basic numeric types like Int or Double. I implemented these conversions by using Scala implicits. My code is similar to this one: def convertAny[T](any: Any)(implicit run: Any => Option[T]) = run.apply(any) implicit…
0
votes
2 answers

Advanced Type Constraints in Scala - Logical Operators and Implicit Parameters

I have the following code which works well. package com.andrew object ExperimentWithTypeConstraints { def union[T](t: T)(implicit c: (T =:= String)) = t match { case s: String => println(s"Some nice string: $s") } def main(args:…
0
votes
0 answers

Exception java.lang.NoSuchMethodError: org.apache.spark.sql.SparkSession$implicits$.newSequenceEncoder

The exact message is: Exception in thread "main" java.lang.NoSuchMethodError: org.apache.spark.sql.SparkSession$implicits$.newSequenceEncoder(Lscala/reflect/api/TypeTags$TypeTag;)Lorg/apache/spark/sql/Encoder; at…
0
votes
0 answers

Implicit method that is matched by trait that's extended by a specific (case?) class

I have case classes BFile and AFile that both extend trait File. And case classes BDir and ADir that both extend trait Dir. The classes are instantiated as their trait type. I am trying to write implicit move methods (located on an object outside…
Mike
  • 961
  • 6
  • 19
  • 43
0
votes
1 answer

What is a "" type gotten through a TypeTag?

I have a method: import scala.reflect.runtime.universe.{TypeTag,typeOf} def print[T:TypeTag] = println(typeOf[T].typeSymbol.name.toString) Most of the time, print[MyClass] prints MyClass when invoked, but sometimes, it prints ? I am…
holbech
  • 573
  • 3
  • 8
0
votes
1 answer

How do I get the implicit instance of the VectorSpace I need?

I'm trying to use spire to make a linear interpolation function I wrote more general def interpolate(pointA: (Double,Double), pointB: (Double,Double), point: Double): Double = { ((pointB._1 - pointA._1) / (pointB._2 - pointA._2) * (point -…
Ryan Stull
  • 1,056
  • 14
  • 35