IN class operator



IN class operator
Smart pascal source code
type TMyRange = class FMin, FMax: Integer; constructor Create(min, max: Integer); function Contains(i: Integer): Boolean; function ContainsArray(i: array of Integer): Boolean; class operator IN Integer uses Contains; class operator IN array of Integer uses ContainsArray; end; constructor TMyRange.Create(min, max: Integer); begin FMin := min; FMax := max; end; function TMyRange.Contains(i: Integer): Boolean; begin Result := (i >= FMin) and (i <= FMax); end; function TMyRange.ContainsArray(i: array of Integer): Boolean; var k: Integer; begin for k in i do if k not in Self then Exit(False); Result := True; end; var range1to5 := TMyRange.Create(1, 5); var range3to7 := TMyRange.Create(3, 7); var i: Integer; WriteLn('Range 1-5:'); for i := 1 to 9 do if i in range1to5 then WriteLn(i); WriteLn(''); WriteLn('Range 3-7:'); for i := 1 to 9 do if i in range3to7 then WriteLn(i); WriteLn(''); var vals: array of Integer; vals := [1, 3]; WriteLn(vals in range1to5); WriteLn(vals in range3to7); {<<< RESULT - CONSOLE LOG >>> ----------------------------- Range 1-5: 12345 Range 3-7: 34567 True False ----------------------------- {<<<<<<<<< THE END >>>>>>>>>}