[e-lang] using @inert with collection APIs

Tyler Close tyler.close at gmail.com
Fri Sep 26 16:52:52 CDT 2008


On Wed, Sep 24, 2008 at 1:45 PM, Tyler Close <tyler.close at gmail.com> wrote:
> As a refresher, when you've got an @inert reference to an object,
>    - you can only access its Immutable state
>    - any return value from an operation that takes an @inert argument
> MUST be held in an @inert variable
>
> So, listValues() could be implemented as:
>
> /**
>  * This method guarantees it will not mutate any of the objects held
> in the provided map.
>  */
> static ConstArray<SomeMutable>
> listValues(@inert ConstMap<SomeMutable> allValues,
>               @inert ConstArray<String> someKeys) {
>    @inert ConstArray<SomeMutable> r = ConstArray.array();
>    for (int i = someKeys.length(); i-- > 0;) {
>        r = r.with(allValues.getValue(someKeys.get(i)));    // assumes
> ConstArray.with(@inert Object x)
>    }
>    return r;
> }
>
> There are 2 issues with this implementation:
>
> 1. We were unable to use the standard iteration API, since it uses a
> mutable iterator object. So the following code would generate a
> verifier error:
>
>    Iterator<SomeMutable> i = someKeys.iterator();    // cannot hold
> return value in non- at inert variable
>
> and so would:
>
>    @inert Iterator<SomeMutable> i = someKeys.iterator();
>    i.next();    // cannot call non- at inert method on @inert reference.

If we change the problem slightly, I think the iteration API can be
saved in this case. For example,

static ConstArray<SomeMutable>
listValues(@inert ConstMap<SomeMutable> allValues,
               PowerlessArray<String> someKeys) {
   @inert ConstArray<SomeMutable> r = ConstArray.array();
   for (final String key : someKeys) {
       r = r.with(allValues.getValue(key));
   }
   return r;
}

In the above code, the method signature alone still let's us know that
this operation is pure and that none of the mutables in the allValues
map will be read or modified.

Still don't know how to save the ArrayBuilder API.

--Tyler


More information about the e-lang mailing list