是否有任何动态类型但不允许弱类型的语言?

Mau*_*rus 5 type-systems

例如,在伪代码中添加(以前未声明的)int和字符串:

x = 1;
y = "2";
x + y = z;
Run Code Online (Sandbox Code Playgroud)

我见过强类型语言,不允许添加这两种类型,但这些类型也是静态类型的,因此不可能出现上述情况.另一方面,我已经看到了允许上述类型的弱类型语言,并且是静态类型的.

是否有任何动态类型但也强类型的语言,以便上面的代码段无效?

Amb*_*ber 12

当然:Python.

>>> a = 3
>>> b = "2"
>>> a+b
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'int' and 'str'
>>> b = 2
>>> a+b
5
Run Code Online (Sandbox Code Playgroud)


Ken*_*oom 5

Ruby是动态类型,但强类型.

irb(main):001:0> 2 + "3"
TypeError: String can't be coerced into Fixnum
    from (irb):1:in `+'
    from (irb):1
irb(main):002:0> "3" + 2
TypeError: can't convert Fixnum into String
    from (irb):2:in `+'
    from (irb):2
irb(main):003:0> "3" + 2.to_s
=> "32"
irb(main):004:0> 2 + "3".to_i
=> 5
Run Code Online (Sandbox Code Playgroud)