On 27.08.23 16:36, Saravanan Palanichamy wrote:
Jochen,
Thank you for your quick response.
I did consider this, but I want to be able to use a mutable where-ever
immutable is allowed. For example, if a function can take all immutable
parameters, I want to be able to call it with mutable types as well. So
my choices are either bypass typing when I try to use mutable where
immutables are needed (in addition to dealing with the generics issue I
called out) or just deal with the generics issue I am looking to solve.
Either way, I'll have to do something (or am I thinking too much :D)
I specifically want this to work, because my DSL is trying to implement
ReadOnly constructs so Mutable/Immutable are not really types seen by
the programmer, its just used during the type checking mechanism to make
sure something marked @ReadOnly is not modified
for me they are kind of exclusive, but ok.
So the programmer will type
void fn1(@ReadOnly Entity myInputParameter) {
List<Entity> things = []
things << myInputParameter
// This should cause a compile error
myInputParameter.myProperty = "1234
}
that should be possible without much problem. A bit more tricky is
def x = myInputParameter
x.myProperty = "1234"
but still possible I would say. Where it gets really problematic is then
things[0].myProperty = "1234"
and I want to see
void fn1(Entity.Immutable myInputParameter) {
// I want this to work but it wont because of groovy type checking
List<Entity.Mutable> things = []
things << myInputParameter
// This will automatically fail (like I want it to) because
setMyProperty is not a method in immutable and is only available in mutable
myInputParameter.myProperty = "1234"
}
but what, except that you replaced the annotations with two types is now
really different?
But why not extend the annotation idea?
void fn1(@ReadOnly Entity myInputParameter) {
List<@ReadOnly Entity> things = []
// only allow if T(myInputParameter) is
// @ReadOnly Entity
things << myInputParameter
// fails because of @ReadOnly
myInputParameter.myProperty = "1234
// fails because of propagated @ReadOnly
things[0].myProperty = "1234"
// could be allowed or forbidden based
// on @Readonly
Entity x = things[0]
things[0].myProperty = "1234"
}
Frankly I currently don't see how to solve this based on the type system
if you do not want to do it like I suggested.
In general adding restrictions or making something found that is not
found otherwise is easy with the type checking extensions. But to tell
the compiler that something is found but wrong or that something should
be propagated in a different way than usual... that is difficult with
the current infrastructure. One way to solve this would then imho be to
find a version that passes compilation and let that the typechecker
check that instead. And then add your checks for immutability.
bye Jochen