Showing posts with label operator. Show all posts
Showing posts with label operator. Show all posts

Tuesday, 4 March 2025

Kotlin Operator Overloading

So my colleague mentioned operator overloading, and how much fun it is.

So I wrote a little test.

It works pretty good, but for infix functions it has a very high "syntactic sugar" and less about "real" operator overloading.

Also, the reference page in [1] indicates that for Infix functions, the precedence has fixed rules that do not (always) conform to precedence that we would assume.

My colleague told me that a number of infix functions were created for QueryDSL, so we could write a semblance of SQL in Kotlin, but the precedence tends to screw things up.

We removed these infix functions from our source code again.

So, use sparingly and only when it makes sense. For example if it makes your code a magnitude easier to read/reason about and preferable with as small a scope as possible.

References

[1] KotlinLang - Functions
https://kotlinlang.org/docs/functions.html#function-scope
Baeldung - Operator Overloading in Kotlin
https://www.baeldung.com/kotlin/operator-overloading
Baeldung - Infix Functions in Kotlin
https://www.baeldung.com/kotlin/infix-functions

Thursday, 14 May 2015

Operator Precedence

I see a number of people get the answer to the following equation wrong:
1 + 5 * 2
Depending who you talk to, it could either be 12 (6 * 2) or the appropriate and correct answer 11 (1 + 10).

Everybody knows the precedence of things, but to make matters clear, here's the list ordered in descending precedence:
  1. parenthesis (also known as brackets)
  2. exponents/roots
  3. multiplication/division
  4. addition/subtraction

Mnemonics

Several mnemonics are in use to remember the correct precedence. Below are three examples.

“Please Excuse My Dear Aunt Sally”
  • Parentheses
  • Exponents
  • Multiplication
  • Division
  • Addition
  • Subtraction

However, this mnemonic doesn't do justice to the fact that some operations actually have the same precedence, for example subtraction and addition.

“Hallo, Meneer van Dalen wacht op antwoord!”
  • Haakjes (Parentheses)
  • Machtsverheffen (Exponents)
  • Vermenigvuldigen (Multiplication)
  • Delen (Division)
  • Worteltrekken (Root)
  • Optellen (Addition)
  • Aftrekken (Subtraction)

This Dutch mnemonic is very very old (and very wrong nowadays). It indicates that roots are very low in priority. Also, if taken literally, it means there's a difference in precedence between, for example, addition and subtraction, similar to the English mnemonic.

“Hé, Mw. v/d Aorta!”

A different Dutch mnemonic (written above), also contains information regarding the equality of certain operations.
  1. Haakjes (Parentheses)
  2. Machtsverheffen (Exponents)/Worteltrekken (Root)
  3. Vermenigvuldigen (Multiplication)/Delen (Division)
  4. Aftrekken (Subtraction)/Optellen (Addition)
Unfortunately, it contains a number of abbreviations:
Mw.
mevrouw (misses)
v/d
van de (of)

References

Wikipedia - Order of operations
http://en.wikipedia.org/wiki/Order_of_operations
Wikipedia - Bewerkingsvolgorde
http://nl.wikipedia.org/wiki/Bewerkingsvolgorde

Friday, 14 November 2014

SQL Operator Precedence

Just a small note for me to remember how this works.

select * 
from ((select 'FINANCE' as dept,  42 as employees from dual) union 
      (select 'SALES' as dept, 32 as employees from dual)) departments
where
   departments.dept = 'FINANCE'
or
   departments.dept = 'SALES'
and
   departments.employees < 30;
returns: FINANCE 42

Clearly, AND takes precedence over OR, as usual.
AND B OR C => (A AND B) OR C

OR B AND C => A OR (B AND C)

References

Stackoverflow - sql logic operator precedence
http://stackoverflow.com/questions/1241142/sql-logic-operator-precedence-and-and-or
SQL Logical Operators
http://www.praetoriate.com/t_garmany_easysql_sql_logical_operators.htm