AdeptLanguage / Adept

The Adept Programming Language
GNU General Public License v3.0
120 stars 8 forks source link

Add the Delphi range data type #288

Closed ghost closed 7 months ago

ghost commented 7 months ago

Range-based a type is a subset of values of an integer, character , or enumerated type and is described as a..b, where a - lower, b - upper limit of the interval type, a< b:

var
  intI: 0..10;
  intC: 'a'..'z';
  intE: Mon..Thr; 

The type that a range type is based on is called the base basic type for that range type. Values of the range type occupy the same amount of memory as values of the corresponding base type.

IsaacShelton commented 7 months ago

Something like this could work:

record <$T> Range (lower, upper $T) {
    func includes(value $T) bool {
        return this.lower <= value && value <= this.upper
    }
}
import basics

func main {
    uppercase_letters <ubyte> Range = Range('A'ub, 'Z'ub)

    printf("uppercase_letters.includes('Q'ub) = %b\n", uppercase_letters.includes('Q'ub))
    printf("uppercase_letters.includes('q'ub) = %b\n", uppercase_letters.includes('q'ub))
}
ghost commented 7 months ago

Implement everything in code (that could be packed into a library) so you don't have to extend the syntax of the language. You are so clever! Btw, I would name the method as contains instead of includes to keep it universal.