Re: Restricting list extensions
"Spencer Allain via users" <users-GSC0n/0aZo1d/SJB6HiN2Ni2O/[email protected]>
| Newsgroups | gmane.comp.lang.groovy.user |
|---|---|
| Message-ID | <[email protected]> |
What you've described should work for the cases where it can be determined at compile time that the object is indeed a MyImmutableList being utilized, but just like in Java there are cases where your list might have been assigned to a Collection or List variable such that the compiler cannot tell that it is really a MyImmutableList until runtime.
Also, overriding all of the to-be-denied methods to throw UnsupportedOperation would ensure that anything that cannot be caught at compile-time would still be caught at runtime.
-Spencer
On Wednesday, August 2, 2023 at 07:13:16 AM EDT, Saravanan Palanichamy <[email protected]> wrote:
Hello Groovy users/devs
I have a derived class from List (lets call this MyImmutableList) and I want to only allow the use of certain extension functions. For example, collect is ok, but left shift is not
myImmutableList << 100 // I want to prevent this because its an immutable list)myImmutableList.collect { it + 1 } // This is ok
How do I achieve this? I have my own AST transformations and I can detect the use of the MyImmutableList class. My first guess is
- Enable Static compile
- Post static compile, detect all extension methods and the object inferred_type it is called on. If type is MyImmutableList, check if extension method is allow listed. If not cause compile error
I am not sure if this is fool proof. Are there easier ways to do this? Is it possible to get the static compile mechanism to tie unknown methods to a default method that I can check for and just error on?
regardsSaravanan