How to use a type alias to define a type constructor

烈酒焚心 提交于 2019-12-13 13:13:24

问题


Let's say I have some code that uses List

def processList(input: List[Int]): List[Int]

I want to replace list with other collection types, like Vector.

Is there a way to define a type constructor so that I can write something like

type SomeCollection[_] = List[_]

def processList(input: SomeCollection[Int]): SomeCollection[Int]

Now I have written processList in terms of SomeCollection. To change SomeCollection to Vector, I just change the type alias, and everywhere in the codebase where I use SomeCollection, I now use Vector. Like so:

type SomeCollection[_] = Vector[_]

def processList(input: SomeCollection[Int]): SomeCollection[Int]

This way, I only need to change the codebase in one place instead of everywhere.

I do not want to write

type SomeIntCollection = List[Int]

because I have connected the collection to Int type.

Is there a way?


回答1:


You're pretty close, this can be done along the lines of

type SomeCollection[A] = List[A]

def processList(input: SomeCollection[Int]): SomeCollection[Int] = input.map(_+1)

However, there's better ways to describe the abstraction. In the cats library, there are a variety of typeclasses designed to abstract over the type of operation you want to perform. The above with cats would look something like

import cats._
import cats.implicits._

def process[F[_]: Functor](input: F[Int]): F[Int] = input.map(_+1)

It doesn't lock you into a specific underlying collection, so you're free to use whatever makes the most sense at the call site.



来源:https://stackoverflow.com/questions/58293648/how-to-use-a-type-alias-to-define-a-type-constructor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!