8. LFUN

  Namespace lfun::

Description

Callback functions used to overload various builtin functions.

The functions can be grouped into a few sets:

  • Object initialization and destruction.

    __INIT() , create() , destroy()

  • Unary operator overloading.

    `~() , `!() , _values() , cast() , _sizeof() , _indices() , __hash()

  • Binary asymmetric operator overloading.

    `+() , ``+() , `-() , ``-() , `&() , ``&() , `|() , ``|() , `^() , ``^() , `<<() , ``<<() , `>>() , ``>>() , `*() , ``*() , `/() , ``/() , `%() , ``%()

  • Binary symmetric operator overloading.

    The optimizer will make assumptions about the relations between these functions.

    `==() , _equal() , `<() , `>()

  • Other binary operator overloading.

    `[]() , `[]=() , `->() , `->=() , `+=() , `()()

  • Overloading of other builtin functions.

    _is_type() , _sprintf() , _m_delete() , _get_iterator() , _search()

Note

Although these functions are called from outside the object they exist in, they will still be used even if they are declared static. It is in fact recommended to declare them static, since that will hinder them being used for other purposes.

See also

::


Method _sqrt

mixed lfun::_sqrt()

Description

Called by sqrt when the square root of an object is requested.

Note

_sqrt is not a real lfun, so it must not be defined as static.

See also

predef::sqrt()


Method _random

mixed lfun::_random()

Description

Called by random . Typical uses is when the object implements a ADT, then a call to this lfun should return a random member of the ADT or range implied by the ADT.

See also

predef::random()


Method __INIT

void lfun::__INIT()

Description

Inherit and variable initialization.

This function is generated automatically by the compiler. It's called just before lfun::create() when an object is instantiated.

It first calls any __INIT functions in inherited classes (regardless of modifiers on the inherits). It then executes all the variable initialization expressions in this class, in the order they occur.

Note

This function can not be overloaded or blocked from executing.

See also

lfun::create()


Method create

void lfun::(zero ... args)

Description

Object creation callback.

This function is called right after lfun::__INIT() .

args are the arguments passed when the program was called.

Note

In Pike 7.2 and later this function can be created implicitly by the compiler using the new syntax:

class Foo(int foo) { int bar; }

In the above case an implicit lfun::create() is created, and it's equvivalent to:

class Foo { int foo; int bar; static void create(int foo) { local::foo = foo; } }

See also

lfun::__INIT() , lfun::destroy()


Method destroy

void lfun::destroy()

Description

Object destruction callback.

This function is called by predef::destruct() right before it zeroes all the object variables and destroys the object.

Note

Note that it's also called on implicit destruct, i.e. when there are no more references to the object, or when the garbage collector decides to destruct it.

Note

Regarding destruction order during garbage collection:

If an object is destructed by the garbage collector, it's part of a reference cycle with other things but with no external references. If there are other objects with destroy functions in the same cycle, it becomes a problem which to call first.

E.g. if this object has a variable with another object which (directly or indirectly) points back to this one, you might find that the other object already has been destructed and the variable thus contains zero.

The garbage collector tries to minimize such problems by defining an order as far as possible:

  • If an object A contains an lfun::destroy and an object B does not, then A is destructed before B.

  • If A references B single way, then A is destructed before B.

  • If A and B are in a cycle, and there is a reference somewhere from B to A that is weaker than any reference from A to B, then A is destructed before B.

  • Weak references (e.g. set with predef::set_weak_flag() ) are considered weaker than normal references, and both are considered weaker than strong references.

  • Strong references are those from objects to the objects of their lexically surrounding classes. There can never be a cycle consisting only of strong references. (This means the gc never destructs a parent object before all children have been destructed.)

An example with well defined destruct order due to strong references:

class Super { class Sub { static void destroy() { if (!Super::this) error ("My parent has been destructed!\n"); } } Sub sub = Sub(); static void destroy() { if (!sub) werror ("sub already destructed.\n"); } }

The garbage collector ensures that these objects are destructed in an order so that werror in Super is called and not error in Sub.

Note

When the garbage collector calls lfun::destroy , all accessible non-objects and objects without destroy functions are still intact. They are not freed if the destroy function adds external references to them. However, all objects with lfun::destroy in the cycle are already scheduled for destruction and are thus be destroyed even if external references are added to them.

Note

The garbage collector had completely random destruct order in versions prior to 7.2.

See also

lfun::create() , predef::destruct()


Method `+

mixed lfun::`+(zero arg, zero ... rest)

Description

Left side addition/concatenation callback.

This is used by predef::`+ . It's called with any arguments that follow this object in the argument list of the call to predef::`+ . The returned value should be a new instance that represents the addition/concatenation between this object and the arguments in the order they are given.

Note

It's assumed that this function is side-effect free.

See also

lfun::``+() , lfun::`+=() , predef::`+()


Method `+=

this_program lfun::`+=(zero arg, zero ... rest)

Description

Destructive addition/concatenation callback.

This is used by predef::`+ . It's called with any arguments that follow this object in the argument list of the call to predef::`+ . It should update this object to represent the addition/concatenation between it and the arguments in the order they are given. It should always return this object.

Note

This function should only be implemented if lfun::`+() also is. It should only work as a more optimized alternative to that one, for the case when it's safe to change the object destructively and use it directly as the result.

Note

This function is not an lfun for the += operator. It's only whether or not it's safe to do a destructive change that decides if this function or lfun::`+() is called; both the + operator and the += operator can call either one.

See also

lfun::`+() , predef::`+()


Method ``+

mixed lfun::``+(zero arg, zero ... rest)

Description

Right side addition/concatenation callback.

This is used by predef::`+ . It's called with any arguments that precedes this object in the argument list of the call to predef::`+ . The returned value should be a new instance that represents the addition/concatenation between the arguments in the order they are given and this object.

Note

It's assumed that this function is side-effect free.

See also

lfun::`+() , predef::`+()


Method `-

mixed lfun::`-(void|zero arg)

Description

Negation and left side subtraction/set difference callback.

This is used by predef::`- . When called without an argument the result should be a new instance that represents the negation of this object, otherwise the result should be a new instance that represents the difference between this object and arg .

Note

It's assumed that this function is side-effect free.

See also

lfun::``-() , predef::`-()


Method ``-

mixed lfun::``-(zero arg)

Description

Right side subtraction/set difference callback.

This is used by predef::`- . The result should be a new instance that represents the difference between arg and this object.

Note

It's assumed that this function is side-effect free.

See also

lfun::`-() , predef::`-()


Method `&

mixed lfun::`&(zero ... args)

Description

Left side bitwise and/intersection callback.

Note

It's assumed that this function is side-effect free.

See also

lfun::``&() , predef::`&()


Method ``&

mixed lfun::``&(zero ... args)

Description

Right side bitwise and/intersection callback.

Note

It's assumed that this function is side-effect free.

See also

lfun::`&() , predef::`&()


Method `|

mixed lfun::`|(zero ... args)

Description

Left side bitwise or/union callback.

Note

It's assumed that this function is side-effect free.

See also

lfun::``|() , predef::`|()


Method ``|

mixed lfun::``|(zero ... args)

Description

Right side bitwise or/union callback.

Note

It's assumed that this function is side-effect free.

See also

lfun::`|() , predef::`|()


Method `^

mixed lfun::`^(zero ... args)

Description

Left side exclusive or callback.

Note

It's assumed that this function is side-effect free.

See also

lfun::``^() , predef::`^()


Method ``^

mixed lfun::``^(zero ... args)

Description

Right side exclusive or callback.

Note

It's assumed that this function is side-effect free.

See also

lfun::`^() , predef::`^()


Method `<<

mixed lfun::`<<(zero arg)

Description

Left side left shift callback.

Note

It's assumed that this function is side-effect free.

See also

lfun::``<<() , predef::`<<()


Method ``<<

mixed lfun::``<<(zero arg)

Description

Right side left shift callback.

Note

It's assumed that this function is side-effect free.

See also

lfun::`<<() , predef::`<<()


Method `>>

mixed lfun::`>>(zero arg)

Description

Left side right shift callback.

Note

It's assumed that this function is side-effect free.

See also

lfun::``>>() , predef::`>>()


Method ``>>

mixed lfun::``>>(zero arg)

Description

Right side right shift callback.

Note

It's assumed that this function is side-effect free.

See also

lfun::`>>() , predef::`>>()


Method `*

mixed lfun::`*(zero ... args)

Description

Left side multiplication/repetition/implosion callback.

Note

It's assumed that this function is side-effect free.

See also

lfun::``*() , predef::`*()


Method ``*

mixed lfun::``*(zero ... args)

Description

Right side multiplication/repetition/implosion callback.

Note

It's assumed that this function is side-effect free.

See also

lfun::`*() , predef::`*()


Method `/

mixed lfun::`/(zero ... args)

Description

Left side division/split callback.

Note

It's assumed that this function is side-effect free.

See also

lfun::``/() , predef::`/()


Method ``/

mixed lfun::``/(zero ... args)

Description

Right side division/split callback.

Note

It's assumed that this function is side-effect free.

See also

lfun::`/() , predef::`/()


Method `%

mixed lfun::`%(zero ... args)

Description

Left side modulo callback.

Note

It's assumed that this function is side-effect free.

See also

lfun::``%() , predef::`%()


Method ``%

mixed lfun::``%(zero ... args)

Description

Right side modulo callback.

Note

It's assumed that this function is side-effect free.

See also

lfun::`%() , predef::`%()


Method `!

int lfun::`!()

Description

Logical not callback.

Returns

Returns non-zero if the object should be evaluated as false, and 0 (zero) otherwise.

Note

It's assumed that this function is side-effect free.

See also

predef::`!()


Method `~

mixed lfun::`~()

Description

Complement/inversion callback.

Note

It's assumed that this function is side-effect free.

See also

predef::`~()


Method `==

int(0..1) lfun::`==(mixed arg)

Description

Equality test callback.

Note

If this is implemented it might be necessary to implement lfun::__hash too. Otherwise mappings might hold several objects as indices which are duplicates according to this function. Various other functions that use hashing also might not work correctly, e.g. predef::Array.uniq .

Note

It's assumed that this function is side-effect free.

See also

predef::`==() , lfun::__hash


Method `<

int(0..1) lfun::`<(mixed arg)

Description

Less than test callback.

Note

It's assumed that this function is side-effect free.

See also

predef::`<()


Method `>

int(0..1) lfun::`>(mixed arg)

Description

Greater than test callback.

Note

It's assumed that this function is side-effect free.

See also

predef::`>()


Method __hash

int lfun::__hash()

Description

Hashing callback.

This function gets called by various mapping operations when the object is used as index in a mapping. It should return an integer that corresponds to the object in such a way that all values which lfun::`== considers equal to the object gets the same hash value.

Note

The function predef::hash does not return hash values that are compatible with this one.

Note

It's assumed that this function is side-effect free.

See also

lfun::`==


Method cast

mixed lfun::cast(string requested_type)

Description

Value cast callback.

Parameter requested_type

Type to cast to.

Returns

Expected to return the object value-casted (converted) to the type described by requested_type .

Note

The argument is currently a string with the name of the type, but might in the future be a value of the type type.

Note

Currently casting between object types is a noop.

Note

If the returned value is not deemed to be of the requested type a runtime error may be thrown.

Note

It's assumed that this function is side-effect free.


Method `[]

mixed lfun::`[](zero arg1, zero|void arg2)

Description

Index/subrange callback.

Note

It's assumed that this function is side-effect free.

See also

predef::`[]()


Method `[]=

mixed lfun::`[]=(zero arg1, zero arg2)

Description

Index assignment callback.

See also

predef::`[]=() , lfun::`->=()


Method `->

mixed lfun::`->(string arg)

Description

Arrow index callback.

Note

It's assumed that this function is side-effect free.

See also

predef::`->()


Method `->=

mixed lfun::`->=(string arg1, zero arg2)

Description

Arrow index assignment callback.

See also

predef::`->=() , lfun::`[]=()


Method _sizeof

int lfun::_sizeof()

Description

Size query callback.

Called by predef::sizeof() to determine the number of elements in an object. If this function is not present, the number of public symbols in the object will be returned.

Returns

Expected to return the number of valid indices in the object.

Note

It's assumed that this function is side-effect free.

See also

predef::sizeof()


Method _indices

array lfun::_indices()

Description

List indices callback.

Returns

Expected to return an array with the valid indices in the object.

Note

It's assumed that this function is side-effect free.

See also

predef::indices() , lfun::_values()


Method _values

array lfun::_values()

Description

List values callback.

Returns

Expected to return an array with the values corresponding to the indices returned by lfun::_indices() .

Note

It's assumed that this function is side-effect free.

See also

predef::values() , lfun::_indices()


Method `()

mixed lfun::`()(zero ... args)

Description

Apply callback.

See also

predef::`()


Method _is_type

int(0..1) lfun::_is_type(string basic_type)

Description

Type comparison callback.

Called by the cast operator to determine if an object simulates a basic type.

Parameter basic_type

One of:

"array"
"float"
"function"
"int"
"mapping"
"multiset"
"object"
"program"
"string"
"type"
"void"
"zero"

The following five shouldn't occurr, but are here for completeness:

"lvalue"
"mapping_data"
"object storage"
"pike_frame"
"unknown"

Returns

Expected to return 1 if the object is to be regarded as a simulation of the type specified by basic_type .

Note

The argument is currently a string with the name of the type, but might in the future be a value of the type type.

Note

It's assumed that this function is side-effect free.


Method _sprintf

string lfun::_sprintf(int conversion_type, mapping(string:int)|void params)

Description

Sprintf callback.

This method is called by predef::sprintf() to print objects. If it is not present, printing of the object will not be supported for any conversion-type except for the %O-conversion-type, which will output "object".

Parameter conversion_type

One of:

'b'

Signed binary integer.

'd'

Signed decimal integer.

'u'

Unsigned decimal integer.

'o'

Signed octal integer.

'x'

Lowercase signed hexadecimal integer.

'X'

Uppercase signed hexadecimal integer.

'c'

Character. If a fieldsize has been specified this will output the low-order bytes of the integer in network byte order.

'f'

Float.

'g'

Heuristically chosen representation of float.

'G'

Like %g, but uses uppercase E for exponent.

'e'

Exponential notation float.

'E'

Like %e, but uses uppercase E for exponent.

's'

String.

'O'

Any value (debug style).

't'

Type of the argument.


Parameter params

Conversion parameters. The following parameters may be supplied:

"precision" : int

Precision.

"width" : int

Field width.

"flag_left" : int(1..1)

Indicates that the output should be left-aligned.

"indent" : int

Indentation level in %O-mode.


Returns

Is expected to return a string describing the object formatted according to conversion_type .

Note

_sprintf() is currently not called for the following conversion-types:

'F'

Binary IEEE representation of float (%4F gives single precision, %8F gives double precision.)


Note

This function might be called at odd times, e.g. before lfun::create has been called or when an error has occurred. The reason is typically that it gets called when a backtrace is being formatted to report an error. It should therefore be very robust and not make any assumptions about its own internal state, at least not when conversion_type is 'O'.

Note

It's assumed that this function is side-effect free.

See also

predef::sprintf()


Method _equal

int lfun::_equal(mixed arg)

Description

Recursive equality callback.

Note

It's assumed that this function is side-effect free.

See also

predef::equal() , lfun::`==()


Method _m_delete

mixed lfun::_m_delete(mixed arg)

Description

Delete index callback.

See also

predef::m_delete()


Method _get_iterator

predef::Iterator lfun::_get_iterator()

Description

Iterator creation callback.

The returned predef::Iterator instance works as a cursor that references a specific item contained (in some arbitrary sense) in this one.

Note

It's assumed that this function is side-effect free.

See also

predef::Iterator , predef::get_iterator , predef::foreach()


Method _search

mixed lfun::_search(mixed needle, mixed|void start)

Description

Search callback.

See also

predef::search()

  Namespace ::

Description

Symbols implicitly inherited from the virtual base class.

These symbols exist mainly to simplify implementation of the corresponding lfuns.

See also

lfun::


Method `->

mixed ::`->(string index)

Description

Builtin arrow operator.

This function indexes the current object with the string index . This is useful when the arrow operator has been overloaded.

See also

::`->=()


Method `->=

void ::`->=(string index, mixed value)

Description

Builtin arrow set operator.

This function indexes the current object with the string index , and sets it to value . This is useful when the arrow set operator has been overloaded.

See also

::`->()


Method _indices

mixed ::_indices()

Description

Builtin function to list the identifiers of an object. This is useful when lfun::_indices has been overloaded.

See also

::_values, ::`->


Method _values

mixed ::_values()

Description

Builtin function to list the values of the identifiers of an object. This is useful when lfun::_values has been overloaded.

See also

::_indices, ::`->