In this post, we are going to examine the some general rules/law that guide through the scala functions and operators.
Let's check out the souce code, which should have abundant code that can help to explain the code within..
// operators_and_methods.scala // in this file, we will examine the operator and method // in essens, there is no dedicated operator (there might be, I don't know) and // every operator is actually a method call val sum = 1 + 2 val sumMore = (1).+(2) val s = "Hello, world!" s indexOf 'o' // the operator is as the call of the following method call s.indexOf('o') // if you call a method that takes multiple arguments using operator notation // you have place those argument in parenthesis s indexOf ('o', 5) // which is equivalent s.indexOf('o', 5) // below we will examine the unary operator // and teh unary operator can be a prefix or postfix operator. // when you have !variable or -variable, it is actually a method call -2.0 (2.0).unary_- // and only +, -, !, ~ (other identifiers) can be used for the unary prefix operator // while postfix oerator are just method call wihtout paremters. val s = "Hello, world" s.toLowerCase // and you can call in operator notation s toLowerCase // there are some bitwise operator // hwere it is unique in Scala -1 >> 31 -1 >>> 31 // == in scala is actually carefully crafted 1 == 2 1 != 2 List(1, 2, 3) == List( 1, 2, 3) List(1, 2, 3) == null null == List(1, 2, 3) // if you desire refcerence equality test, you can use the 'eq' or 'ne' operator List(1, 2, 3) eq List(1, 2, 3) List(1, 2, 3) ne List(1, 2, 3) // oeprator precedence and associativity // table of precedence // (all other special characters) // * / % // + - // : // = ! // < > // & // ^ // | // (all leters) // ( all assignment operators) 2 << 2 + 2 // resut is 32, think of why // one except to the rule above // if an operator ends in '=' // such as *= // and the operator is not one of the <=, >=, !=, == // then the operator is the same as the simple assignment (=) var x = 10 val y = 3 x *= y + 1 x *= (y + 1) // the associativity of the operator determines the way operators are grouped // the associativity of an operator in Scala is determined by the last character. // any operator that ends in ':' are invoked on the right operand, passing in the left operand // but still the operand are evaluate from left to right .. though the associativity might be //var a = 5 //var b = 6 //a ::: b // is actually equal to //{ val x = a; b.:::(x)} // rich wrapper /// there is a concept that is called // implicit conversions, // and it is implicit conversion that endow the primitives // with additional methods // e.g. of some rich wrapper functions 0 max 5 0 min 5 -2.7 abs -2.7 isInfinity 4 to 6 "bob" capitalize "robert " drop 2