Skip to Main Content
Language Implementation Patterns
book

Language Implementation Patterns

by Terence Parr
December 2009
Intermediate to advanced content levelIntermediate to advanced
380 pages
9h 2m
English
Pragmatic Bookshelf
Content preview from Language Implementation Patterns
Pattern 21Automatic Type Promotion

Purpose

This pattern describes how to automatically and safely promote arithmetic operand types.

Discussion

The goal of automatic type promotion is to get all operands of a single operation to be of the same type or compatible types. That’s just how computer CPU instructions want their operands. Unfortunately, that’s fairly inconvenient from a programming point of view. We often want to use a variety of types within the same operation such as 3+’0’. Most programming languages would, for example, interpret 3+’0’ as 3+(int)’0’.

A language application can convert between types at will as long as it doesn’t lose information. For example, we can convert 4 to 4.0 but can’t convert 4.5 to integer 4 without losing information. ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Implementation Patterns

Implementation Patterns

Kent Beck

Publisher Resources

ISBN: 9781680500097Errata Page