Chapter 7. Bracket Overloading

In many programming languages, bracket-notation is commonly associated with array subscripting. For instance, A[i] is a left-value in C that refers to array-cell i in the array referred to by A. There is support in ATS for overloading brackets with multiple function names so that bracket-notation can be employed to call these functions, resulting in code that reads like some form of subscripting. It is expected that this style of calling functions can, sometimes, make the code written in ATS more easily accessible.

Let us now see a simple example of bracket-notation in overloading. In the followng code, a linear abstract type lock is introduced together with two functions:

// absvtype lock(a:vt@ype) // extern fun{a:vt0p} lock_acquire(!lock(a)): a extern fun{a:vt0p} lock_release(!lock(a), a): void //

As one can imagine, lock_acquire is called to obtain the value stored in a given lock while lock_release is called to return a value to a given lock.

Suppose that we now introduce the following overloading declarations:

// overload [] with lock_acquire overload [] with lock_release //

With these declarations, the following code typechecks:

// val mylock = $extval(lock(int), "mylock") // val x0 = mylock[] // = lock_acquire (mylock) val () = mylock[] := x0 // = lock_release (mylock, x0) //

Note that the bracket-notation in any assigement is only allowed to refer to a function that returns the void-value. In the above example, the function lock_release returns the void-value.

In ATS, bracket-notation is already overloaded with functions performing list-subscripting, array-subscripting and matrix-subscripting, and it can also be used to access and update a given reference.

Please find on-line the entirety of the code presented in this chapter.