Module: Carbon::Core::Integer::Cast Private

Included in:
Carbon::Core::Integer
Defined in:
lib/carbon/core/integer/cast.rb

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Defines cast functions for integers. This applies to all integers. This guarentees that the following functions exist on all integers:

  • .to-int64(self): Carbon::Int64
  • .to-uint64(self): Carbon::UInt64
  • .to-int32(self): Carbon::Int32
  • .to-uint32(self): Carbon::UInt32
  • .to-int16(self): Carbon::Int16
  • .to-uint16(self): Carbon::UInt16
  • .to-int8(self): Carbon::Int8
  • .to-uint8(self): Carbon::UInt8

These functions upcast and downcast as nessicary, but use the sign of the original number to determine how the casting should be handled. For example, an Int32 is upcast to a UInt64 using the sign-extension instruction; however, a UInt32 is upcast to a Int32 using the zero-extension instruction. This is to keep the same number as the value, but just converted to a longer number. This is in line with the behavior of C.

Instance Method Summary collapse

Instance Method Details

#define_cast_function(from, to) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Defines a cast function for a single pair of integers.

Parameters:



34
35
36
37
38
39
40
# File 'lib/carbon/core/integer/cast.rb', line 34

def define_cast_function(from, to)
  function_name = from.name.call(to.cast, [from.name])
  Core.define(function: function_name) do |function|
    function[:return] = to.name
    perform_cast(from, to, function[:definition])
  end
end