You are looking at historical revision 16003 of this page. It may differ significantly from its current revision.

coerce

Type Identity & Coercion

Documentation

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

The major difference here is the ability to extend the type domain.

type-of

Determine the type of an object.

Usage

(require-extension type-of)
(require-extension type-extend)

type-of

[procedure] (type-of OBJECT)

Returns a type symbol for the type of OBJECT.

Built-in types are unspecified (void), boolean, record-instance, port, char, number, string, keyword, symbol, pair, list, and vector. The catch-all is scheme-object.

type-of-extended?

[procedure] (type-of-extended? TYPE-SYMBOL)

Is the domain extended to cover the type identifier TYPE-SYMBOL.

type-of-extend!

[procedure] (type-of-extend! TYPE-SYMBOL PREDICATE)

Extends the domain of type-of with the type definition (TYPE-SYMBOL PREDICATE).

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

type-of-composite-extension!

[procedure] (type-of-composite-extension! TYPE-SYMBOL PREDICATE)

Extends the domain of an existing type-of extension.

TYPE-SYMBOL and PREDICATE are as for extend-type-of.

Should the new PREDICATE and existing predicate be eq? the operation is ignored.

type-of-extension-remove!

[procedure] (type-of-extension-remove! TYPE-SYMBOL)

Delete any existing type-of extension for TYPE-SYMBOL.

coerce

Translate an object into another type representation.

Usage

(require-extension coerce)
(require-extension type-extend)

coerce

[procedure] (coerce OBJECT TYPE-SYMBOL [DEFAULT-PROC])

Converts OBJECT to a value of TYPE-SYMBOL.

DEFAULT-PROC is a procedure with the signature (OBJECT TYPE-SYMBOL -> OBJECT) and is invoked when coercion fails. The default is to signal an error.

Built-in types are boolean, record-instance, port, char, number, string, keyword, symbol, pair, list, and vector.

coerce-all

[procedure] (coerce-all (OBJECT...) (TYPE-SYMBOL...) [DEFAULT-PROC])

Returns a list of every element of the (OBJECT...) coerced to the corresponding element of the (TYPE-SYMBOL...).

DEFAULT-PROC and TYPE-SYMBOL are as for coerce.

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 (type-symbol . (object -> object).

case-coerce

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

Returns a coercion procedure for use with extend-coerce.

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

The variable object is bound to the object to coerce within CASE ....

The variable on-error is bound to a thunk within CASE ...} to invoke on error.

(define (unspecified? obj) (eq? (void) obj))

(define unspecified-coerce
  (case-coerce
    ((char)     #\nul)
    ((number)   +nan)
    ((string)   "")
    ((symbol)   '||)
    ((list)     (list))
    ((vector)   (vector))))

(coerce-extend! 'unspecified unspecified? unspecified-coerce)

(type-of (void)) ;=> unspecified

(coerce-extend! 'string
  string?
  (case-coerce ((unspecified) (if (string=? "" object) (void) (on-error)))))

(coerce "" 'unspecified) ;=> (void)

coerce-extended?

[procedure] (coerce-extended? TYPE-SYMBOL)

Is the domain extended to cover the type identifier TYPE-SYMBOL.

coerce-extend!

[procedure] (coerce-extend! TYPE-SYMBOL PREDICATE CONVERTOR)

Extends the domain of coerce with the type definition (TYPE-SYMBOL PREDICATE CONVERTOR).

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.

coerce-composite-extension!

[procedure] (coerce-composite-extension! TYPE-SYMBOL PREDICATE CONVERTOR)

Extends the domain of an existing coerce extension.

TYPE-SYMBOL, 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.

coerce-extension-remove!

[procedure] (coerce-extension-remove! TYPE-SYMBOL)

Delete any existing coerce extension for the type identifier TYPE-SYMBOL.

Usage

(require-extension coerce)

Examples

Notes

Requirements

miscmacros lookup-table

Bugs and Limitations

Author

kon lovett

Version history

1.0.0
Initial Chicken 4 release.

License

Copyright (C) 2009 Kon Lovett. All rights reserved.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the Software), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED ASIS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.