Project

General

Profile

Actions

Feature #21148

open

Class proposal: IndefiniteNumeric < Numeric

Added by Student (Nathan Zook) 4 days ago. Updated 3 days ago.

Status:
Open
Assignee:
-
Target version:
-
[ruby-core:121118]

Description

Suppose someone deals five cards face down from a regulation poker deck, and we wish to reason about the number of aces. We know that that number is one of zero, one, two, three, or four. Therefore, if someone asks "is this number larger than negative one?", the answer is yes even without ever needing to resolve the actual value.

This proposal is inspired by the proposed Enumerable::Lazy::Length class of Kevin Newton in response to #21135

The IndefiniteNumeric class would serve as a base class for such values. In particular, it relies on a subclass to define #value, which returns a definite Numeric.

class IndefiniteNumeric < Numeric
   def coerce(other) = value.coerce(other)
   def +(other) = value + other
   def -(other) = value - other
   def *(other) = value * other
   def /(other) = value / other
   def <=>(other) = value <=> other
end

It is expected that in particular, <=> will be overridden for subclasses where #value might be slow to return.

Usually, we will know more than just that the value in question is Numeric. It seems appropriate have IndefiniteInteger and so forth. What is not clear to me is if IndefiniteInteger should be a subclass of IndefiniteNumeric or of Integer. (Such a thing does not appear to be currently possible.) If subclassing cannot be, what about defining #kind_of?(Integer) to be true?

Note: with the length of an Enumerable as inspiration, I would argue that an IndefiniteInteger might actually value to Float::INFINITY, so it should NOT define <=> against plus or minus Float::INFINITY.

Actions

Also available in: Atom PDF

Like0
Like0Like0