理解“type"关键字在 Scala 中的作用 [英] Understanding what 'type' keyword does in Scala

查看:62
本文介绍了理解“type"关键字在 Scala 中的作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Scala 的新手,我真的找不到很多关于 type 关键字的信息.我试图理解以下表达的含义:

I am new to Scala and I could not really find a lot about the type keyword. I am trying to understand what the following expression may mean:

type FunctorType = (LocalDate, HolidayCalendar, Int, Boolean) => LocalDate

FunctorType 是某种别名,但它代表什么?

FunctorType is some kind of an alias, but what does it signify?

推荐答案

是的,类型别名 FunctorType 只是

(LocalDate, HolidayCalendar, Int, Boolean) =>本地日期

类型别名通常用于保持代码的其余部分简单:您现在可以编写

Type aliases are often used to keep the rest of the code simple: you can now write

def doSomeThing(f: FunctorType)

会被编译器解释为

def doSomeThing(f: (LocalDate, HolidayCalendar, Int, Boolean) => LocalDate)

这有助于避免定义许多自定义类型,例如,这些自定义类型只是在其他类型上定义的元组或函数.

This helps to avoid defining many custom types that are just tuples or functions defined on other types, for example.

type 还有其他几个有趣的用例,例如 中所述本章Scala 编程.

There are also several other interesting use cases for type, as described for example in this chapter of Programming in Scala.

这篇关于理解“type"关键字在 Scala 中的作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆