Jump to content
Sign in to follow this  
Dave Novo

ISet<T> in spring4D

Recommended Posts

I am a bit new to spring 4D, and am attempting to use ISet<T> as a replacement of the standard set of <type> in Delphi.

 

How do you easily check if an value is in a ISet<T>

 

The use case I have is with an enum. Where TEnumSet = set of enum;

 

In Delphi I would do 

 

if myEnumToCheckFor in MySet then .....

 

I had expected to see a method ISet<T>.Contains<T>:Boolean, or something along those lines, but I don't see anything. It seems like ISet<T> has lots of helpful methods for dealing with other sets, but I cannot find anything to check if an individual item is in the set. Of course, I can create a new set that contains 1 item and use that, but it seems overkill to do

 

presuming Myset:=ISet<some enum>

if MySet.InsersetsWith(TCollections.CreateSet([myenumTocheckFor])) do....

 

am I missing something?

 

note: sorry, I meant to put this in 3rd party, but somehow it ended up here. I cannot seem to find a way to move it to the 3rd party subforum.

Edited by Dave Novo
note about wrong forum

Share this post


Link to post

Inheritance Tree:

IEnumerable<T>

  IReadOnlyCollection<T>

   ICollection<T>

     ISet<T> 

 

  IEnumerable<T> has many functions including Contains:

    function Contains(const value: T): Boolean; overload;
    function Contains(const value: T; const comparer: IEqualityComparer<T>): Boolean; overload;
    function Contains(const value: T; const comparer: TEqualityComparison<T>): Boolean; overload;

 

Share this post


Link to post

Oh thanks. I did not think to go all the way up to enumerable, as I am used to that interface just being for enumeration. I will investigate that further....

Share this post


Link to post

FWIW if your type is an enum already it makes no sense to use ISet<T> because you can use the enum set. ISet<T> is for types that are no enums, such as string for example.

Share this post


Link to post

The reason I was thinking of using ISet is just that Delphi is very verbose:

 

if myEnum in myset then

 

is not so bad but

 

if  [enum1,enum2] * myset <> [] then

 

is obscure and if you dont use this syntax frequently you are constantly reminding yourself what the intention was, particularly for more complex set operations. I feel that

 

if mySet.DoesNotContainAny([enum1,enum2]) then

 

 is much easier to understand the intention. Not as performant of course. 

Edited by Dave Novo

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×