Shy07
2015-08-10 14:13:30 +08:00
Swift 和 Python 不清楚,但是 Ruby 是强类型的,Ruby 的变量是“Duck Type”,隐式转换的前提是对象存在 to_s, to_i 这类转换方法。
eg:
a = 10086
puts a # 打印的不是 a 本身的对象,而是 a.to_s 返回的对象,隐式转换
a = "10086" # 为什么可以改变 a 的类型?Ruby 的变量是动态类型
puts a == 10086 # => false,为什么?因为 a 是 String,10086 是 Fixnum,强类型
puts 10087 == a.to_i + 1 # => true,显示转换类型,为什么支持调用 to_i ?Duck Type
puts 10087 == a + 1 # => no implicit conversion of Fixnum into String (TypeError)
如果想要看到
document.write('10086' == 10086); // => true
这类代码,我建议还是继续 JS 大法好吧