什么是动态打字? [英] What is Dynamic Typing?

查看:151
本文介绍了什么是动态打字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我听说过这个术语与PHP等脚本语言一起使用。这是什么意思?

解决方案

动态打字是一种语言的确定特征。一个简短的解释可能是:


当语言没有将值
与特定类型相关联时,语言具有动态类型,但是它的目的是根据您尝试使用该值的方式决定
应该在运行时的类型。


例如,在PHP中,您可以编写

  $ count =5; //定义一个字符串变量

然后继续说

  $ count = $ count * 2; //这是合法的,具有明显的效果¹

这里发生了什么?一个编译器并没有抱怨你试图将一个字符串乘以一个数字,拒绝编译程序(例如会发生在C,C ++,C#和Java这样的语言)。它产生代码,将参数 $ count 2 转发到乘法运算符,就像你问和移动一样。 / p>

随着程序的编译,动态输入在运行时生效。当乘法运算符周围查看其操作数时,它会检查查看当前的内容,如果你愿意的话,每个操作数的类型。像以前一样,它是一个字符串和一个int。但是操作员知道它只能乘以两个整数(为简单起见,我们忽略浮点数),所以它必须以某种方式从字符串中产生一个整数值。所有动态类型的语言都有规定这样的转换如何工作的规则所有价值类型之间;在这种情况下,PHP 产生整数5 从字符串5。



可能遇到的动态打字的另一个方面称为鸭子打字;这仅适用于类类型(即不是原语)的值。简而言之,鸭子打字规定当你写

  $ object-> quack(); 

编译器不会尝试查看 $ object 是一种类型,它有一个名为 quack 的方法,它不需要参数。相反,它将在运行时尝试看看 $ object 是否具有这样的方法;如果有的话,该方法将被调用,无论我们现有的对象类型为
(可能是一个鸭子,可能是所有编译器关心的狗)。



脚注



¹将字符串乘以一个整数是动态打字是关于(由于乘法需要一个,从字符串产生一个整数);然而,在这里工作也有松散的打字(允许乘法编译而无法证明这两个操作数实际上是int)。


I've heard this term used with scripting languages such as PHP. What exactly does it mean?

解决方案

Dynamic typing is a definitive characteristic of a language. A short explanation might be:

A language has dynamic typing when it does not associate values strictly with a specific type, but it is designed to "decide" what the type of a value should be at runtime, based on how you are attempting to use it.

For example, in PHP you can write

$count = "5"; // defines a string variable

and then go on to say

$count = $count * 2; // this is legal and has the obvious result¹

What happened here? For one, the compiler did not complain that you are attempting to multiply a string by a number and refuse to compile the program (such as would happen in languages like C, C++, C# and Java). It produced code to forward the arguments $count and 2 to the multiplication operator just like you asked and moved on.

With the program now compiled, dynamic typing comes into effect at runtime. When the multiplication operator gets around to look at its operands, it checks to see what is the current, if you will, type of each one. As before, it's a string and an int. But the operator knows that it can only multiply two integers (let's ignore floats for simplicity), so it has to somehow produce an integer value from the string. All dynamically typed languages have rules that stipulate how such a conversion works between all pairs of value types; in this case, PHP produces the integer 5 from the string "5".

Another aspect of dynamic typing you might come across is called duck typing; this only applies to values of class types (i.e. not primitives). In short, duck typing stipulates that when you write

$object->quack();

the compiler will not attempt to see if $object is of a type that has a method named quack that takes no arguments. Rather, it will attempt at runtime to see if $object actually has such a method; if it does, the method will be called regardless of what type of object we have at hand (might be a duck, might be a dog for all the compiler cares).

Footnotes:

¹ Multiplying a string by an integer is what dynamic typing is all about (producing an integer from a string because the multiplication demands one); however, there is also loose typing at work here (allowing the multiplication to compile without being able to prove that both operands are actually ints).

这篇关于什么是动态打字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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