Is it possible to "curry" higher-kinded types in Scala?
Asked Answered
G

5

24

Let's suppose I have a trait with two type parameters, e.g.

trait Qux[A, B]

and another trait with a higher-kinded type parameter, e.g.

trait Turkle[C[_]]

I'd like to be able to substitute a fixed value for one of the type parameters for Qux, so that it can be used to parametrize Turkle.

Here's an example (of code that doesn't make sense in Scala!):

trait Baz[A] extends Turkle[Qux[A, _]]

Anyone have any ideas how to achieve this effect?

Graycegrayheaded answered 6/6, 2011 at 4:38 Comment(2)
This compiles, although I have absolutely no idea if this is what you want: type QuxApplied[A] = Qux[A, _]; trait Baz[A] extends Turkle[QuxApplied]Embow
@Embow - My guess is that it is supposed to be the same A, which is not the case in your example.Magnusson
S
24

Jason Zaugg came up with the most succinct way to do this:

trait Baz[A] extends Turkle[({type x[a]=Qux[A, a]})#x]

IntelliJ's Scala plugin will optionally collapse this to:

trait Baz[A] extends Turkle[x[a]=Qux[A, a]]
Scarify answered 6/6, 2011 at 6:2 Comment(1)
Thanks. It's nice to have an "inline" solution.Graycegrayheaded
B
4

You mean something like this?

trait QuxWithString[A] extends Qux[A, String]
new Turkle[QuxWithString]{}

This is the analog to partial application for types.

Briscoe answered 6/6, 2011 at 5:19 Comment(0)
M
2
trait Turkle[C[_]]
trait Qux[A,B]
trait Wraps[A] {
  type Jkz[X] = Qux[A,X]
  trait Baz extends Turkle[Jkz]
}
Magnusson answered 6/6, 2011 at 5:9 Comment(1)
This is actually pretty good. In my use cases it's not so unusual to have nested traits anyway, so there's little feeling of writing boilerplate.Graycegrayheaded
R
1

The compiler plugin kind projector allows this as well:

// Explicit lambda, greek letters
trait Baz[A] extends Turkle[λ[α=>Qux[A,α]]]

// Explicit lambda, normal letters
trait Baz[A] extends Turkle[Lambda[a=>Qux[A,a]]]

// No explicit lambda, ? placeholder    
trait Baz[A] extends Turkle[Qux[A,?]]
Ryun answered 6/8, 2015 at 7:38 Comment(0)
P
1

Scala 3 is proposing SIP: Underscore Syntax for Type Lambdas #5379 such that the following might be possible

trait Baz[A] extends Turkle[Qux[A, _]]

Scala 3 natively provides type lambdas using [X] =>> F[X] syntax so try

trait Baz[A] extends Turkle[[B] =>> Qux[A, B]]
Pantomime answered 5/4, 2020 at 14:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.