Skip to content

Instantly share code, notes, and snippets.

@MaxwellBo
Last active January 16, 2019 12:56
Show Gist options
  • Save MaxwellBo/86f0495ec19d8e7cef7bf528061d5e42 to your computer and use it in GitHub Desktop.
Save MaxwellBo/86f0495ec19d8e7cef7bf528061d5e42 to your computer and use it in GitHub Desktop.

It may come as a surprise to many but Scala actually supports a flexible form Uniform Function Call Syntax (sometimes also referred to as Uniform Calling Syntax or Universal Function Call Syntax), henceforth referred to as UFCS. This feature can be seen in D, Rust and Nim. An example in Nim:

proc add(a: int, b: int): int =
  a + b

add(1, 2)
# OR 
1.add(2)

As we can see, UFCS allows any function to be called using the syntax for method calls, by using the receiver as the first parameter, and the given arguments as the remaining parameters.

The Scala equivalent of this code is:

def add(x: Int, y: Int) = x + y

add(1, 2) 
// OR 
1.add(2)

Scala also supports Biversal Function Call Syntax, which adds an additional receiver:

def add(x: Int, y: Int, z: Int) = x + y + z

add(1, 2, 3) 
// OR
1.add(2)dda.3

The reversed function name is a little weird to get used to, but in some ways its similar to the if ... fi syntax of Bash. Scala also supports Triversal Function Call Syntax:

def add(w: Int, x: Int, y: Int, z: Int) = w + x + y + z

add(1, 2, 3, 4) 
// OR 
1.add(2)dda.3
	  
	  d
	  d
	  a
	  .
	  4

Quadversal Function Call Syntax

def add (a: Int, b: Int, c: Int, d: Int) = a + b + c + d

add(1, 2, 3, 4, 5)
// OR
      1
      .
      a
      d
      d
      
2.add(5)daa.3
      
      d
      d
      a
      .
      4

Octoversal Function Call Syntax

(7)dda.1.add(6)
 ´    .   `
 d d   a   d d
 d  d  d  d  d
 a   a d a   a
 .    ..   .
 2.add(0)daa.3
 .    ..   .
 a   a d a   a
 d  d  d  d  d
 d d   a   d d
 ,   .    ,
(8)dda.4.add(5)

and even Enneadecagonaversal Function Call Syntax

            (7)dda.1.add(6)
            `     .     `
           d     a     d d
          d     d     d  d
         a     d     a   a
        .     ,     .    .
       2.add(0)daa.3     3
      .     `     ..    `.
     a     d     a a   a a
    d     d     d  d  d  d
   d     a     d   d d   d
  ,     .     ,    ,   
(7)dda.1.add(6)   (0)   (5)
      .        `⌣    `
 d     a     d   d d   d
 d     d     d  d  d  d
 a     d     a a   a a
 .          .,    ..
 2.add(0)daa.3     4
 .          .    .
 a     d     a   a
 d     d     d  d
 d     a     d d
      .    ,
(8)dda.4.add(5)

Unfortunatey, Scala does not support Icosikaitraversal Function Call Syntax and above, as Scala only supports functions that take 22 parameters or less. Fortunately, this is getting addressed in Dotty.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment