>>> x = 3
>>> y = '4'
>>> print(x+y)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'int' and 'str'
JavaScript代码:
var x = 3
var y = '4'
alert(x + y) //Produces "34"
class testme(object):
''' A test object '''
def __init__(self):
self.y = 0
def f(aTestMe1, aTestMe2):
return aTestMe1.y + aTestMe2.y
c = testme #get a variable to the class
c.x = 10 #add an attribute x inital value 10
c.y = 4 #change the default attribute value of y to 4
t = testme() # declare t to be an instance object of testme
r = testme() # declare r to be an instance object of testme
t.y = 6 # set t.y to a number
r.y = 7 # set r.y to a number
print(f(r,t)) # call function designed to operate on testme objects
r.y = "I am r.y" # redefine r.y to be a string
print(f(r,t)) #POW!!!! not good....
例如,Haskell允许您定义一种类型,即数字、字符串、该类型的列表或从字符串到该类型的映射,这是表示可以从JSON解码的任何内容的完美方式。在Java中没有办法定义这样的类型。但至少Java有参数(泛型)类型,所以你可以写一个函数,接受一个List of T,并且知道元素是类型T;其他语言,如早期的Java,强迫你使用对象列表和向下转换。但至少Java允许你用它们自己的方法创建新类型;C语言只允许创建结构。而BCPL连这个都没有。以此类推,直到集合,唯一的类型是不同的位长。
1 + 1.0;;
----^^^
error FS0001: The type 'float' does not match the type 'int'
if [] then 1 else 2;;
---^^
error FS0001: This expression was expected to have type bool but here has type 'a list