Chapter 7 CPL
overloaded operator advantage and disadvantage
Advantage: When sensibly used, such operators can be an aid to readability (avoid method calls, expressions appear natural). Potential problems: Users can define nonsense operations. Readability may suffer, even when the operators make sense.
expressions in scheme and lisp
All arithmetic and logic operations are by explicitly called subprogram a + b * c is coded as (+ a (* b c))
mixed mode assignment in other languages
Assignment statements can also be mixed-mode. In Fortran, C, Perl, and C++, any numeric type value can be assigned to any numeric type variable. In Java and C#, only widening assignment coercions are done. In Ada, there is no assignment coercion. What do you think of Python and Ruby? P.S. both of them support dynamic type binding.
operator overloading info
C++, C#, and F# allow user-defined overloaded operators. Some operators can't be overloaded in C++. Class member operator (.) and scope operator (::). Java doesn't support operator overloading.
errors in expressions
Causes -Inherent limitations of arithmetic. e.g., division by zero. -Limitations of computer arithmetic. e.g. overflow or underflow. -Often detected at run-time and called exceptions.
conditional expressions in c
Conditional Expressions: C-based languages (e.g., C, C++) An example: average = (count == 0)? 0 : sum / count Evaluates as if written as follows: if (count == 0) average = 0 else average = sum /count
conditional targets
Conditional targets (Perl) ($flag ? $total : $subtotal) = 0 Which is equivalent to if ($flag){ $total = 0 } else { $subtotal = 0 }
implicit type conversion
-A mixed-mode expression is one that has operands of different types. -In most languages, all numeric types are coerced in expressions, using widening conversions. EX: -In Ada, there are virtually no coercions in expressions except in exponentiation **. -In ML and F#, there are no coercions in expressions.
referential transparency
-A program has the property of referential transparency if any two expressions in the program that have the same value can be substituted for one another anywhere in the program. -Because they do not have variables, programs in pure functional languages are referentially transparent.
compound assignment operators
-A shorthand method of specifying a commonly needed form of assignment. -Example: a = a + b can be written as a += b
explicit type conversion
-Called casting in C-based languages Examples C: (int)angle Java: Integer.parseInt(var), Integer.valueOf(var) F#: float(sum) Note that F#'s syntax is similar to that of function calls.
boolean expressions
-Consist of Boolean variables, Boolean constants, relational expressions, and Boolean operators. Boolean operators: AND, OR, NOT, XOR.
assignment in functional languages
-Identifiers in functional languages are only names of values. ML Names are bound to values with val. val fruit = apples + oranges; If another val for fruit follows, it is a new and different name.
unary minus
-In Java and C#, unary minus also causes the implicit conversion of short and byte operands to int type. -Unary minus operator can appear anywhere inside the expression, as long as it is parenthesized. EX: a + (- b) * c
multiple expressions
-Perl, Ruby, and Lua allow multiple-target multiple-source assignments ($first, $second, $third) = (20, 30, 40); -Also, the following is legal and performs an interchange: ($first, $second) = ($second, $first);
assignment statements
-The general syntax <target_var> <assign_operator> <expression> -The assignment operator = Fortran, BASIC, the C-based languages. := Ada, Pascal, ALGOL 60. -= can be bad when it is overloaded for the relational operator for equality (that's why the C-based languages use == as the relational operator)
unary assignment operators
-Unary assignment operators in C-based languages combine increment and decrement operations with assignment. Examples: sum = ++count sum = count++ count++ -count++
relational expressions
-Use relational operators (<,>,<>,==) and operands of various types. -Evaluate to some Boolean representation. -Operator symbols are varied among languages (!=, /=, ~=, .NE., <>). -The relational operators always have lower precedence than the arithmetic operators. a + 1 > 2 * b the arithmetic expressions are evaluated first.
short circuit evaluation
-When the result of an expression is determined without evaluating all of the operands and/or operators. Example: (13 * a) * (b / 13 - 1)
narrowing vs widening conversion
-is one that converts an object to a type that cannot include all of the values of the original type e.g., float to int. -is one in which an object is converted to a type that can include at least approximations to all of the values of the original type. e.g., int to float -Both of these can be explicit or implicit
common operator overloading
+ for int and float
solutions for side effects
1) Write the language definition to disallow functional side effects No two-way parameters in functions. No non-local references in functions. Advantage: it works! Disadvantage: inflexibility of one-way parameters and lack of non-local references. 2) Write the language definition to demand that operand evaluation order be fixed Disadvantage: limits some compiler optimizations. Java requires that operands appear to be evaluated in left-to-right order.
associativity rules
Typical associativity rules: -Left to right, except **, which is right to left. -Sometimes unary operators associate right to left (e.g., in FORTRAN). -APL is different; all operators have equal precedence and all operators associate right to left. -Precedence and associativity rules can be overridden with parentheses.
operator overloading
Use of an operator for more than one purpose is called
problem with functional side effects
When a function referenced in an expression alters another operand of the expression; e.g., for a parameter change: a = 10; /* assume that fun changes its parameter a=20 and returns 8*/ b = a + fun(&a);
functional side effects
when a function changes one of its parameter (a two-way parameter) or a global variable.
overloaded operator trouble
Loss of compiler error detection (omission of an operand should be a detectable error). Some loss of readability.
operand evaluation order
Operand evaluation order: 1)Variables: fetch the value from memory. 2)Constants: sometimes a fetch from memory; sometimes the constant is in the machine language instruction. 3)Parenthesized expressions: evaluate all operands and operators first. 4)The most interesting case is when an operand is a function call and it has a side effect.
arithmetic expression design issues
Operator precedence rules? Operator associativity rules? Order of operand evaluation? Operand evaluation side effects? Operator overloading? Type mixing in expressions?
problem with short circuit evaluation
Problem with non-short-circuit evaluation index = 0; while((index < length) && (LIST[index] != value)) index++; -When index=length, LIST[index] will cause an indexing problem (assuming LIST is length - 1 long)
expressions in ruby
Ruby -All arithmetic, relational, and assignment operators, as well as array indexing and bit-wise logic operators, are implemented as methods. a+b is a call to the + method of the object referenced by a. - One result of this is that these operators can all be overridden by application programs.
