sample1-1
let int1:Int=10 let int2:Int=20 let plus:Int=int1+int2 print(plus)
30
sample1-1は、Int型の変数int1,int2に値をセットして+演算子(算術加算演算子)で加算して結果を表示する、という何の変哲もないコードです。まさに何の変哲もないのですが、
つまり+演算子に関して我々が全く何の実装もせずに、当たり前に加算が行われている、ということですね。
このようにInt型ではプログラマは+演算子に関して何も実装せずとも、普通に加算が行われるわけですが、これが自分で作った型の場合はどうか。
sample1-2
struct Container { var width:Int=0 var height:Int=0 } let con1=Container(width:10,height:20) let con2=Container(width:20,height:30) print(con1+con2)
main.swift:17:11: error: binary operator '+' cannot be applied to two 'Container' operands print(con1+con2)…