All Questions
Tagged with polymorphism scala
189
questions
0
votes
1
answer
45
views
How to correctly implement a polymorphic functional Tree data structure in Scala?
I am trying to figure out the best way to implement a polymorphic tree:
trait Tree[A]
case object Void extends Tree[A] // does not compile
case class Node[A](left: Tree[A], key: A, right: Tree[A]) ...
0
votes
1
answer
274
views
Scala implicit class based on type class
implicit class IntIncrement(val underlying: Int) extends AnyVal {
def increment(): Int = underlying + 1
}
This is valid and allows me to do something like 1.increment()
I want to be able to ...
0
votes
1
answer
402
views
Can I extend an existing class (e.g. String) with a custom trait in Scala?
Consider the simple trait TypeName
trait TypeName {
def typeName(): String
}
For a custom class, it is clear how I might specify my implementation for the typeName method.
class Point(val x: Int, ...
0
votes
1
answer
104
views
How to change base class field in functional fashion in scala
Say I have this hierarchy
trait Base {
val tag: String
}
case class Derived1(tag: String = "Derived 1") extends Base
case class Derived2(tag: String = "Derived 2") extends Base
/...
0
votes
1
answer
87
views
How to get a classOf[Array[T]] in Scala
I have a method (scala 2.12) that does look like the following.
The goal is to pass to the method readValue from objectMapper (jackson) a string and a class that the string needs to be casted, which ...
0
votes
2
answers
56
views
Scala argument upper type bounds and overriding
I am trying to understand and incorporate upper bound types with overriding in my system, but have not been able to achieve it without some ugly code. I have the following 2 traits:
trait MyId {
def ...
2
votes
1
answer
281
views
Proper way to use Numeric type bounds for classes in scala
General question: What is the proper way to define a generic class typed with a type which is Numeric, ideally without using any implicit in Scala 2?
Specific example: consider the following toy class ...
0
votes
0
answers
97
views
Return concrete type for a trait from it's companion object
Previously I returned concrete types from a trait, but never from a companion object of a trait and couldn't find an example of my exact scenario online. Please feel free to point me if this is a ...
2
votes
1
answer
161
views
How to simulate inheritance in Clojure?
I recently started looking into Clojure, so admittedly I do not have much
experience nor have I read every book about it. Still, I am having a hard
time figuring out how to expand the behavior of ...
1
vote
3
answers
577
views
Leveraging a generic return type in Scala
So I would like to use a generic return type and be able to use the info of that type within the function. Not sure this is possible but here is what I would like:
def getStuff[A](a: MyObj, b: ...
0
votes
4
answers
280
views
Can a make a Scala trait with a polymorphic, variable-arity abstract method?
Say I have a Scala trait that does some computation and then calls a polymorphic method on extending classes that might have a different method signature in each class:
trait GenericThing {
val ...
1
vote
1
answer
77
views
Scala Any => native format
I have a map of type Map[_, Any], and I want to extract the values in their native format (without resorting to .asInstanceOf[_]).
Something like this...
val m: Map[String, Any] = Map("i" -&...
1
vote
0
answers
120
views
Calculate derivative of function at point in Scala Breeze
I am trying to calculate the numeric approximation of the derivative of a given function at a point. It should be very straightforward, i.e., $f'(a) = \lim\limits_{\Delta\to 0} \frac{f(a+\Delta) - f(a-...
2
votes
3
answers
136
views
type stable parametric polymorphism
I don't understand why the following scala code doesn't compile:
sealed trait A
case class B() extends A {
def funcB: B = this
}
case class C() extends A {
def funcC: C = this
}
def f[T <: A](s:...
9
votes
0
answers
286
views
Use case of kind polymorphism with AnyKind
Given
trait Int // proper type
trait List[A] // 1st-order-kinded type constructor
trait Functor[F[_]] // higher-order-kinded type constructor taking type constructor
...