scala 如何推断变量的类型? [英] How does scala infers the type of a variable?

查看:32
本文介绍了scala 如何推断变量的类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我总是很想知道 Scala 如何总是正确推断类型.Scala 是如何实现的?

I am always fascinated to see how scala always infers the type correctly . How scala does it?

scala> val num = 5
num: Int = 5

scala> val num = "5"
num: String = 5

我知道在这里问这个问题可能很愚蠢,但我不知道答案.请赐教.谢谢!

I know it might be a very stupid question to ask here but i don't know the answer. Please enlighten me. Thanks!

推荐答案

有几种方法可以推断变量的类型.主要使用基于逻辑理论的那些称为推理规则.

There are several methods for inferencing the type of a variable. Mainly using those called inference rules based in logic theory.

有很多论文解释了背后的理论.在这里,我举了一个很好的例子(使用 Scala ;))

There are plenty of papers explaining the theory behind. Here I put a good example (with Scala ;) )

www2.in.tum.de/hp/file?fid=879

这篇关于scala 如何推断变量的类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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