Hostname: page-component-cd9895bd7-jkksz Total loading time: 0 Render date: 2024-12-13T07:04:39.850Z Has data issue: false hasContentIssue false

FUNCTIONAL PEARL Type-safe cast

Published online by Cambridge University Press:  27 October 2004

STEPHANIE WEIRICH
Affiliation:
University of Pennsylvania, Philadelphia, PA 19104, USA (email: sweirich@cis.upenn.edu)
Rights & Permissions [Opens in a new window]

Abstract

Core share and HTML view are not available for this content. However, as you have access to this content, a full PDF is available via the ‘Save PDF’ action button.

Comparing two types for equality is an essential ingredient for an implementation of dynamic types. Once equality has been established, it is safe to cast a value from one type to another. In a language with run-time type analysis, implementing such a procedure is fairly straightforward. Unfortunately, this naive implementation destructs and rebuilds the argument while iterating over its type structure. However, by using higher-order polymorphism, a casting function can treat its argument parametrically. We demonstrate this solution in two frameworks for ad-hoc polymorphism: intensional type analysis and Haskell type classes.

Type
Functional pearls
Copyright
© 2004 Cambridge University Press

Footnotes

A previous version of this paper appeared in the Fifth ACM International Conference on Functional Programming (ICFP 00).
Submit a response

Discussions

No Discussions have been published for this article.