# BuiltInFunctions

© 2013 Anna M. Bigatti
GNU Free Documentation License, Version 1.2

- Examples
- User documentation
- Bugs, shortcomings and other ideas
- Main changes

CoCoALib Documentation Index

## Examples

- `BuiltinFunctions.C` ;-)

## User documentation

Very rough set of notes about adding new functions to CoCoA-5

- `runtimeEnv->evalArgAs<XXXXValue>(ARG(0));` returns a pointer `intrusive_ptr<XXXXValue>` which needs to be accessed as `x->theXXXX`

- `runtimeEnv->evalArgAsListOf<TValue>(ARG(1));` -- all elements must be of type TValue and returns `vector<T>`

- `runtimeEnv->evalArgAsRingElemList(ARG(2), R->theRing);` -- accepts also INT and RAT.
- `runtimeEnv->evalArgAsRingElemList(ARG(0));` -- guesses the ring

- `runtimeEnv->evalArgAsT1orT2<T1Value,T2Value>(ARG(0), n)`
- `runtimeEnv->evalArgAsT1orT2orT3<T1Value,T2Value,T3Value>(ARG(0), n)`
- `...`
- `runtimeEnv->evalArgAsT1orT2orT3orT4orT5orT6<T1Value,T2Value,T3Value>(ARG(0), n)` returns a pointer `intrusive_ptr<RightValue>` and puts in `n` the index of the type found. Throws a meaningful error is the type found is not in the list.

- `PtrCastXXXX(v)` where `v` is a `intrusive_ptr<RightValue>` (generic right value): casts the pointer to specific type and call the reference `theXXXX`. So it returns an `XXXX`. (Defined in `BuiltinFunctions.H`)

        DECLARE_STD_BUILTIN_FUNCTION(IsOne, 1) {
          int which;
          intrusive_ptr<RightValue> v = runtimeEnv->evalArgAsT1orT2orT3<BigIntValue, BigRatValue, RingElemValue>(ARG(0), which);
          switch (which) {
          case 1: return Value::from(IsOne(PtrCastBigInt(v)));
          case 2: return Value::from(IsOne(PtrCastBigRat(v)));
          default:return Value::from(IsOne(PtrCastRingElem(v)));
          }
        }
        END_STD_BUILTIN_FUNCTION

  == Maintainer documentation ==

## Bugs, shortcomings and other ideas

## Main changes