coerce

  1. coerce
    1. Description
    2. Author
    3. Requirements
    4. Download
    5. Documentation
      1. type-of
      2. coerce
      3. coerce-all
      4. extend-coerce
      5. composite-coerce-extension
      6. remove-coerce-extension
      7. make-case-coerce
      8. case-coerce
    6. Changelog
    7. License

Description

Convert an object to another representation

Author

Ported to Chicken: Kon Lovett

Requirements

Download

coerce.egg

Documentation

A Chicken specific version of the SLIB "coerce.scm" module, with extensions, which implements object type conversion.

type-of

[procedure] (type-of OBJECT)

Returns a symbolic name for the type of OBJECT.

coerce

[procedure] (coerce OBJECT RESULT-TYPE [DEFAULT])

Converts OBJECT to a RESULT-TYPE value.

RESULT-TYPE is a symbol.

DEFAULT is (object-type-symbol object result-type-symbol -> object) and is invoked when coercion fails.

Built-in types are char, number, string, symbol, list, and vector.

coerce-all

[procedure] (coerce-all LIST-OF-OBJECT LIST-OF-RESULT-TYPE [DEFAULT])

Returns a list of every element of LIST-OF-OBJECT coerced to the corresponding element of LIST-OF-RESULT-TYPE.

DEFAULT and RESULT-TYPE are as for coerce.

extend-coerce

[procedure] (extend-coerce IDENTIFIER PREDICATE CONVERTOR)

Extends the domain of coerce with the type definition {IDENTIFIER PREDICATE CONVERTOR}.

IDENTIFIER is a symbol.

PREDICATE is a procedure, (object -> boolean), testing whether the argument is a type.

CONVERTOR is a procedure, (object symbol on-error -> object), converting from the type to a result type.

The CONVERTOR must invoke (on-error) when conversion not possible. Usually case-coerce would be used to create the CONVERTOR, which automatically handles calling of the on-error argument.

composite-coerce-extension

[procedure] (composite-coerce-extension IDENTIFIER PREDICATE CONVERTOR)

Extends the domain of an existing coerce extension.

IDENTIFIER, PREDICATE, and CONVERTOR are as for extend-coerce.

The new CONVERTOR is try'ed before the existing procedure, then the existing procedure is try'ed "on-error".

Should the new PREDICATE and existing predicate be eq? the predicates are not "composited" using or.

remove-coerce-extension

[procedure] (remove-coerce-extension IDENTIFIER)

Delete any existing coerce extension for IDENTIFIER.

make-case-coerce

[procedure] (make-case-coerce CONVERTOR [ALIST])

Returns a coercion procedure for use with extend-coerce

CONVERTOR is a coercion procedure, (object symbol on-error -> object).

ALIST is a list of (symbol . (object -> object).

case-coerce

[syntax] (case-coerce CASE ...)

Returns a coercion procedure for use with extend-coerce.

CASE is of the form ((SYMBOL ...) EXPRESSION ...), or (else EXPRESSION ...)

The variable OBJECT is bound to the object to coerce within the case-body, CASE ....

The variable on-error is bound to a thunk within the case-body, CASE ..., to invoke on error.

Changelog

License

 Copyright (c) 2006, Kon Lovett
 All rights reserved.
 Redistribution and use in source and binary forms, with or without
 modification, are permitted provided that the following conditions are
 met:
 Redistributions of source code must retain the above copyright
 notice, this list of conditions and the following disclaimer.
 Redistributions in binary form must reproduce the above copyright
 notice, this list of conditions and the following disclaimer in the
 documentation and/or other materials provided with the distribution.
 Neither the name of the author nor the names of its contributors may
 be used to endorse or promote products derived from this software
 without specific prior written permission.
 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 OF THE POSSIBILITY OF SUCH DAMAGE.