score:8
This is a special case of structural typing but called a 'type lambda', if you search for type lambda scala
google will give you some results.
In short, it's used in a similar fashion as partially applied functions.
def x(a:Int, b:Int):Int = a * b
val x10 = x(10, _:Int)
x10(2) // 2
An example with types.
type IntEither[B] = Either[Int, B]
val y:IntEither[String] // Either[Int, String]
In some cases methods or classes expect a type with a single parameter
class Test[F[_]]
You can not give the Test
class an Either
because Test
expects a type with 1 parameter and Either
has 2. In order to be able to pass in an Either
we can partially apply it
type X[B] = Either[Int, B]
new Test[X]
An alternative way of writing it is like this:
type X = {
type T[x] = Either[Int, x]
}
new Test[X#T]
Instead of creating a type alias, we can also define the X
type anonymously
new Test[({type T[x] = Either[Int, x]})#T]
These all give you an instance of type Test[Either[Int, x]]
.
Edit
Your example could look like this:
type PartiallyTypedFunction[R] = {
type T[x] = R => x
}
implicit def FunctionFunctor[R] =
new Functor[PartiallyTypedFunction[R]#T] {
def map[A, B](fa: R => A)(f: A => B): R => B = (x => f(fa(x)))
}
Source: stackoverflow.com
Related Query
- Is there any comprehensive tutorial on how to use this kind of structural typing?
- Is there any demo to show how to use the placeholder like this in Scala?
- How do you call this kind of concurrency related class? And is there a standard implementation?
- How to use implicit conversion instead of structural typing when only one collection method is needed
- How to use Scala's this typing, abstract types, etc. to implement a Self type?
- Scala: "map" vs "foreach" - is there any reason to use "foreach" in practice?
- How can I convert this foldLeft : Double expression to use Option[Double] instead?
- Is there any book or online tutorial for Scala DSL?
- How to prevent this kind of bug - pattern matching and Nil
- Is there any trick to use macros in the same file they are defined?
- Structural typing in Scala: use abstract type in refinement
- How do I use a structural type with generic parameters?
- Is there any way to exclude implicit time convertions in org.spec2.time and use your own?
- How save a TypeTag and then use it later to reattach the type to an Any (Scala 2.10)
- How to understand this kind of function declaration: `=> .. => .. => ..`?
- Is there a name for this kind of lifting a function?
- How to use apache commons (or any other library not part of the jdk) from Scala
- How to use function with Any input
- How to use Numeric[T] to represent zero of any numeric type
- is there any way to use import someValue._ to implement overriding methods in scala?
- Is there any way to use chisel to generate blackbox from verilog text content? (define val based on text content in scala)
- Is there an example of how to use akka-http with servlet container?
- In Scala 2.8, how to write (append) a line to a file? Should I use Java clesses or there are native Scala functions for this?
- How to failed compilation if there are any dead code or unused imports, variables etc
- How to do structural typing in scala that ALSO only accepts subclasses?
- How define Structural Type that the method return this
- How can I create this kind of interface for an observer system?
- Is there any reason to use classes vs object at all in Scala?
- Structural Programming in Scala with Shapeless: How to use the SYB implementation correctly?
- Is there any difference between this syntax of curried functions?
More Query from same tag
- How to implement a class with a static self-increase member in Scala?
- Spark Dataframe : Set column values if an conditional row is encountered
- How to implement reliable Periodic Actor?
- value registerAsTable is not a member of org.apache.spark.sql.DataFrame
- Is there a ScalaTest Plugin for Eclipse Luna and Scala IDE Lithium?
- Read ORC files directly from Spark shell
- In Scala how do I filter by reified types at runtime?
- Scala: What does mean to pass a Set to the map function of a set
- Where are all Setting[T] stored in sbt 0.11?
- Why is Some(1).getOrElse(Some(1)) not of type Option[Int]?
- code for finding duplicates in an array, Scala
- Is there a term for groupBy that returns multiple groups?
- How can I use primitives in Scala?
- Akka actor ask pattern does not work as expected
- Getting both Maven and SBT use local Artifactory-server
- Scala Standalone JAR with a conf Folder
- How to stop IntelliJ Scala plugin indenting blocks
- Running a single test class from sbt , in a multi module project
- Unsigned integer in scala
- how to compare 3 options hierarchy value and return boolean the scala way?
- Cannot instansiate BinaryClassificationMetrics class in Spark
- Scala Cats Accumulating Errors and Successes with Ior
- Associating two arrays in an RDD by index
- Spark - Write DataFrame with custom file name
- Error importing types from Spark SQL
- Replace a value in a row with a value in another row in Spark dataframe
- learn Scala with TDD
- What is Flow#join in Akka Streams
- Error with floating point literal in Scala REPL
- How to use a java custom comparator for ordering a Spark RDD