Sets

From The Oxygene Language Wiki

Jump to:navigation, search

This is a Language topic
Feel free to add your notes to this topic below.



A set is a collection of ordinal values of the same type. If you are coming to Delphi Prism via C#, the concept of sets may be new to you. Sets are a convenient way of storing and accessing values that belong together.

For example, instead of defining day, month and year as 1, 2 and 3, it is more convenient to declare them as a set.

Although you can fill sets with integers, you will typically use them to hold Enums:

Type
  MySet1 = set of 1000; // set with elements 0..999
  MySet2 = set of (Abc, Def, Ghi);  // implicit enums

You can also declare a set using an explicit enum:

type
  MyEnum = (Abc, Def, Ghi);
  MySet3 = Set of MyEnum; // creates a copy of all possible values in the enum;

A set is a value type and typical syntax is as follows:

var x: MySet3 := [MySet.Def, MySet.Abc]; // note MySet, not MyEnum


Additional Syntax

set of byte // same as set of 256
set of 256 // same as set of 0..255
set of Word // same as set of 65536
set of 65536 // same as set of 0..65535


Set Operators

The following operators take two sets as operands and return a third set:

Operator
Operation
Example

+

union

[a,b,e]+[c,e,f] = [a,b,c,e,f]

-

difference

[a,b,c,d]-[a,c] = [b,d]

*

intersection

[a,b,e]*[c,e,f] = [e]


These operators compare two sets:

Operator
Operation
Example

=

equality

[a,b,c] = [a,b,c] // true

[a,b,c] = [a,b,c,d] // false

[a,b,e] = [a,b,d] // false

[a,b,c,d] = [a,b,c] // false

<>

inequality

[a,b,c] <> [a,b,c] // false

[a,b,c] <> [a,b,c,d] // true

[a,b,e] <> [a,b,d] // true

[a,b,c,d] <> [a,b,c] // true

<=

subset

[a,b,c] <= [a,b,c] // true

[a,b,c] <= [a,b,c,d] // true

[a,b,e] <= [a,b,d] // false

[a,b,c,d] <= [a,b,c] // false

>=

superset

[a,b,c] >= [a,b,c] // true

[a,b,c] >= [a,b,c,d] // false

[a,b,e] >= [a,b,d] // false

[a,b,c,d] >= [a,b,c] // true


Finally, the in operator lets you test set membership:

Operator
Operation
Example

in

membership

if a in [a,b,c] then .. // True

if x not in foo then... // shortcut for if not (x in foo) then


Set Rules

External use of Sets

Sets may be used from non-Delphi Prism code, including all operators except 'in'.

In needs to be called via the op_in method or via the regular methods on the set class in C#.


See Also


Oxygene-48.png

Area: Oxygene Language
Compiler version: Oxygene 5

Language GlossaryKeywordsTypesFAQHow To

Navigation
Areas
More
Toolbox