Hi all,

recently I have been working on supporting dynamic tags like “_itemCount”, 
“_curItem” and “_lastItem” in array elements.

In java I used ThreadLocals for this as it felt like a Java-Native way and it 
wouldn’t blow up the signatures of our Parser, Serializer and GetLengthInXYZ 
methods.

Now when looking for something similar in Go, it seems in such cases Contexts 
are used. These are sort of like a HashMap wrapped with little util methods. In 
this HashMap you pass along values that are “in context” … if you change the 
context, a copy of the old context is created and then the updated/added values 
are updated/added to the HashMap.

So, in an array I add the additional information to the existing one by:

arrayCtx := spiContext.CreateArrayContext(ctx, int(_numItems), int(_curItem))


and then simply pass in the „arrayContext”. And for accessing it I simply for 
this:

spiContext.GetLastItemFromContext(ctx)



Admittedly I’m not a big fan of crating copies of the full context every time, 
I would prefer to be able to push to sort of a context stack, but I guess 
that’s the way things work in Go.



When I wanted to add the functionality of Java and Go to PLC4C I was a bit 
stuck, as neither concept really existed.



However thinking a bit more about it, adding the concept of Contexts to C was 
actually quite simple.



Now that got me thinking:

Shouldn’t we implement the same pattern for Java? It should be easy to 
implement.



And while I was thinking, I even thought: Shouldn’t we extend this idea and 
also get the ReadBuffer, WriteBuffer, the Encoding, the ByteOrder and anything 
we’re currently passing along with “WithXYZOption” var-args, into these 
Contexts and simply pass along one Context object?

I think this would clean up the code quite a bit.



What do you think?



Chris





Reply via email to