Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make Delphi TList immutable

is it possible to make a Delphi TList immutable?
I searched in the delphi doc for a class similar to the unmodifiableList in java, but didn't found anything.

regards!

like image 548
norty Avatar asked Dec 20 '22 16:12

norty


2 Answers

You could use the IReadOnlyList<T> from Spring4D.

If you have an IList<T> you just call AsReadOnlyList (AsReadOnly since 2.0) and it returns you the same instance as IReadOnlyList<T> which does not provide methods to manipulate the list (no Add, Delete or setter for the Items property).

However there is a difference to the unmodifiableList from Java:

In Java you really get a List<T> which will throw UnsupportedOperationException when you try to modify it while in Spring4D which is mostly modeled after .NET you get something that you cannot call any modifying operations on.

like image 91
Stefan Glienke Avatar answered Dec 24 '22 03:12

Stefan Glienke


The Delphi RTL contains no classes that implement immutable or read-only lists. You will have to implement such a class yourself, or find a library that offers such functionality.

like image 40
David Heffernan Avatar answered Dec 24 '22 02:12

David Heffernan