logo
Tags down

shadow

Can I use scala type parameters with a val?


By : David Rees
Date : July 30 2020, 11:00 AM
it fixes the issue I am going through some exercises to get accustomed to Scala's type system. I have a helper function for running unit tests where I specify the input and expected output of a function. In this case the function I'm testing returns the first n elements of a list: , Consider parameterising your model like so
code :
case class TestCase[T](input: (List[T], Int), output: List[T])
val inputs: List[TestCase[String]] = ???
def input[T](x: List[TestCase[T]]) = ???
val input = [T] => (x: List[TestCase[T]]) => ???
def assertEquals[A, B](input: List[A], output: List[B])(implicit ev: A =:= B) = ???

assertEquals(List(1), List("")) // error: Error: Cannot prove that Int =:= String


Share : facebook icon twitter icon

Can Scala allow free Type Parameters in arguments (are Scala Type Parameters first class citizens?)?


By : user1316075
Date : March 29 2020, 07:55 AM
This might help you I have some Scala code that does something nifty with two different versions of a type-parameterized function. I have simplified this down a lot from my application but in the end my code full of calls of the form w(f[Int],f[Double]) where w() is my magic method. I would love to have a more magic method like z(f) = w(f[Int],f[Double]) - but I can't get any syntax like z(f[Z]:Z->Z) to work as it looks (to me) like function arguments can not have their own type parameters. Here is the problem as a Scala code snippet. , You can do it like this:
code :
trait Forall {
  def f[Z] : Z=>Z
}

def z(u : Forall) = w(u.f[Int], u.f[Double])
def z(u : {def f[Z] : Z=>Z}) = w(u.f[Int], u.f[Double])
scala> object f1 {def f[Z] : Z=>Z = x => x}
defined module f1

scala> def z(u : {def f[Z] : Z=>Z}) = (u.f[Int](0), u.f[Double](0.0))
z: (AnyRef{def f[Z]: (Z) => Z})(Int, Double)

scala> z(f1)
res0: (Int, Double) = (0,0.0)
scala> z(new Forall{def f[Z] : Z=>Z = x => x})

Why doesn't Scala fully infer type parameters when type parameters are nested?


By : user3363005
Date : March 29 2020, 07:55 AM
To fix the issue you can do Kipton got close with his higher-kinded solution. Unfortunately he tripped over what appears to be a bug in Scala < 2.9.1.RC1. The following works as expected with 2.9.1.RC1 and trunk,
code :
Welcome to Scala version 2.9.1.RC1 (Java HotSpot(TM) Server VM, Java 1.7.0).
Type in expressions to have them evaluated.
Type :help for more information.

scala> abstract class A
defined class A

scala> abstract class B[T <: A]
defined class B

scala> class ConcreteA extends A
defined class ConcreteA

scala> class ConcreteB[T <: A] extends B[T]
defined class ConcreteB

scala> class Example[T <: A, U[X <: A] <: B[X]](resolver: U[T])
defined class Example

scala> new Example(new ConcreteB[ConcreteA])
res0: Example[ConcreteA,ConcreteB] = Example@ec48e7

How can I improve Scala's type inference with type parameters that don't show up in the first parameter list?


By : user3383732
Date : March 29 2020, 07:55 AM
may help you . One solution is to create a temporary instance of a class that defines an apply method simulating the second parameter list, which has itself the B0 type parameter:
code :
abstract class Wrapper[A](wrapped: A) {

  // ... as before...

  def fold[B](whenTrue: => B) = new FoldRequest[B](whenTrue)

  class FoldRequest[B](whenTrue: => B) {
    def apply[B0 >: B](whenFalse: => B0) =
      if (someCondition) whenTrue else whenFalse
  }
}

How two abstract the number of parameters of a function with type parameters in Scala?


By : Goljob
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , You could use tupled method on function: new Wrapper(multiplyFunction.tupled).
If you want to make this transparent to the wrapper class's user you could use duck typing:
code :
object Wrapper {
  def apply[I, O](e: { def tupled: I => O }) = new Wrapper(e.tupled)
  def apply[I, O](e: I => O) = new Wrapper(e)
}

scala> Wrapper( (a: Int) => a )
res0: Wrapper[Int,Int] = Wrapper@29d03e78

scala> Wrapper( (a: Int, b: Int) => a * b )
res1: Wrapper[(Int, Int),Int] = Wrapper@581cdfc2

Scala - how come using a super-type with two generic parameters cause the scala type checker to treat the child-type dif


By : Nerdie
Date : March 29 2020, 07:55 AM
around this issue tl;dr Type erasure and pattern matching
Typing the Map with SuperTrait concealed information about the type, and caused the pattern matching to assume a broad type for your extractor.
code :
case class Identity[A : Manifest]() {
  def apply(a: A) = a match { case a: A => a } // seemingly safe no-op
}

val myIdentity: Any = Identity[Int]()

myIdentity match {
  case f@Identity() => f("string") // uh-oh, passed String instead of Int
}
scala.MatchError: string (of class java.lang.String)
  at Identity.apply(...)
case class Identity[A : Manifest]() {
  def apply(a: A) = a match { case a: A => a }
}

val myIdentity: Identity[_] = Identity[Int]()

myIdentity match {
  case f@Identity() => f("string")
}
found   : String("string")
required: _$1 where type _$1
     case f@Identity() => f("string")
DesiredKeyConstraints.KeyConstraint[Any](_, constraint)
DesiredKeyConstraints.KeyConstraint[T](_, constraint) forSome {type T}
shadow
Privacy Policy - Terms - Contact Us © voile276.org