score:4
Accepted answer
one possible approach is to infer the types of the two arguments separately then use =:=
to prove they're the same:
def test2[s, t](a: string, b: s, c: t)(implicit ev: s =:= t): t = ???
val x = test2("", new a, new a) // compiles
val y = test2("", new a, new b) // doesn't compile; can't prove a =:= b
this might be a bit strict for you though in the presence of subtyping:
class c extends b
val z = test2("", new b, new c) // doesn't compile; can't prove b =:= c
val w = test2[b, b]("", new b, new c) // does compile
score:3
as you mention, the reason why that compiles is that t
is the first common type between stateinlistview(ar.name)
and ar.state
.
you can verify this by executing these lines:
class a()
class b()
def test[t](a : string, b : t, c : t) : t = ???
val x : any = test("ciao", new a(), new b()) // compiles ok
val y : a = test[a]("ciao", new a(), new b()) // does not compile: b does not work
val z : b = test[b]("ciao", new a(), new b()) // does not compile: a does not work
besides specifying t
manually (e.g. test[a](...)
), i can't really think of a way to avoid this behaviour....
Source: stackoverflow.com
Related Query
- How to make compiler check that 2 method arguments have the same type?
- How can I check that method parameters have the same type as the methods this?
- How to write a zipWith method that returns the same type of collection as those passed to it?
- How to make it so that dependent types in two different traits are recognized as the same type
- Scala - how to make the SortedSet with custom ordering hold multiple different objects that have the same value by which we sort?
- How do I make a trait that I can mix into mapped fields to that I don not have to override the same properties for many fields in a database?
- How to make method return the same generic as the input?
- Scala Puzzle: enforcing that two function arguments are of the same type AND both are a subtype of a given class
- scala: how to create a generic type which is subtype of all the number classes in scala so that it can include compare method
- How can I provide a compile-time guarantee that my method will return the same object it gets in Scala?
- How to resolve ambiguous implicit conversion method that takes same input type in Scala?
- Why can method parameter F have the same name as type constructor F?
- How can a method accept a (T <: Seq)[A] and return T[B] -- the same type of sequence with a different type parameter?
- Is there any method that does the same thing as map() but generates a different type of container?
- How define Structural Type that the method return this
- How to override a generic method with implicit arguments if the generic type is already fixed?
- How to make the scala compiler find case classes used with wrong arguments
- how to delete the elements that have the same prefix String (2 to 5 Chars) in a List?
- How do I have the scala compiler infer one type from another?
- In Shapeless, given two records, how do I require that both records have the same keys and join them?
- Is there a way to expand the scope of an existential type quantifier in Scala to convince the type checker that two variables have the same type?
- How to make ENTER do go to the selected type or method when using Ensime global search with Emacs --no-window?
- Why in this scala snippet the compiler loses the type [P] of the payload? How can I make this snippet compile?
- How to pass co-variant varargs through apply() to a function that takes co-variant varargs of the same type
- What is the actual return type of the filter method on a range? How do I make it a Vector?
- How do I use the forall loop to check if all rows have the same number of columns in a Vector in Scala?
- How to use shapeless HList to have a list of the same type but with a different generic?
- How do I make sure a function receives the same parameter type as the current object?
- How can I make a scala method parameter type that is a collection of multiple types that can be converted to a given type?
- How to have multiple traits of same base trait implement the same method
More Query from same tag
- Getting case class field value with a val
- AbstractMethodError when mixing in trait nested in object - only when compiled and imported
- Array of struct parsing in Spark dataframe
- Play Framework + JDBC + Futures
- Type-level filtering using shapeless
- If the first statement in a for comprehension fails , recover is not able to catch the exception
- traversing a graph in spark-graphx via edge properties
- Spark - Can't create schema for array of structs
- Dynamic PathMatcher from String
- Scalac restrictions on value classes for implicits on traits with type parameters
- Convert a column which contains array of string (of unequal size) to exactly two columns with multiple rows in scala spark
- Kafka MirrorMaker's consumer not fetching all messages from topics
- Pass type identifier to static macro annotation
- spark 1.4.0 java.lang.NoSuchMethodError: com.google.common.base.Stopwatch.elapsedMillis()J
- Tuple2 mistakenly inferred as Product in recursive Scala function
- Using Cats Show on enum
- Scala: two sliding more efficiently
- Scala string filter based on split index
- Write a map with key as int to json in scala using json4s
- How to generate a random sequence of binary strings of fixed size ( say 36 bits ) in scala
- How to designate a thread pool for actors
- Java EE replacement for Scala?
- How to determine the best setting for spark running on a single node?
- how to construct Scala API with error sentinel token (like Option.None) that can be chained through flatmap?
- What to do when my AKKA Actor dies
- How to convert a recursive function into a tail-recursive version?
- Akka actorFor vs passing an ActorRef
- Scala Lift - Delete file in mongo gridfs
- How do I specify a type bound for Float and Double on a generic type in Scala?
- Converting thunk to sequence upon iteration