List of ai2 components to be extension

Has anyone tried to pass a list of components from ai2 to the extension? I tried to do it with arrangements but the components in the list are surrounded by another class and you can't cast them to the class of the given component. Is there a way?

I'm not sure what you mean by this?

I added some arrangements to the list and passed them to the extension. In java, I get, for example, 1 item from the list and cast it to, for example, HorizontalArrangament, then using the getView() method, I tried to extract a ViewGroup. The compiler compiled the extension correctly. But after adding the extension to ai2 and trying to run it, I got an error in companion that I can't cast a class (I don't remember exactly gnu...simple..) to a horizontal arrangament. I had a similar situation when I wanted a number to pass an egg object to an extension. I had to use valueof() to get the value from the int object.

Tomorrow when I'm at my computer, I'll post some sample code and show the error I'm getting.

Can you please post your code here?

If I had a guess, I would say that you're trying to do something like this:

public void DoFoo(List<Component> items) {
  for (Component item : items) {
    // do foo
  }
}

However, when a list is passed into a component it is always a YailList (YailList extends List so this is safe) but because of type erasure the generic information cannot be enforced so there's no safety that the items in the list are all Component. This is particularly important because YailList is a linked list and its first element is the symbol *list* (which ensures that the 1-based indexing works correctly). Instead, you can do something like:

public void DoFoo(YailList items) {
  for (Object it : items.getCdr()) {  // skips the *list* item at index 0
    if (it instanceof Component) {
      // do foo
    } else {
      // possibly report an invalid input to your extension
    }
  }
}

For example, here is how the Map Features setter is implemented:

1 Like
@SimpleFunction(description = "")
    public void SetList(YailList arrangements) {
        for (Object arrangement : arrangements) {
            ViewGroup viewGroup = (ViewGroup) ((HVArrangement) arrangement).getView();

        }

    }

errreoe

But this work:

@SimpleFunction(description = "")

    public void SetList(HVArrangement arrangement) {

        ViewGroup viewGroup = (ViewGroup) arrangement.getView();

    }

This is my point about the fact that the first element in the list is always the symbol *list*. You will need to skip the first element using one of the two techniques I presented.

1 Like

What is this method? Ok, so only the first position is the problem? Will try.

Ok thanks, it work:

@SimpleFunction(description = "")

    public void SetList(YailList arrangements) {

        for (Object arrangement : arrangements) {

            if (arrangement instanceof HVArrangement) {

                ViewGroup viewGroup = (ViewGroup) ((HVArrangement) arrangement).getView();

            }

        }

    }

I've often passed Lists for extension, but never considered that the first element is something I don't expect. I've never had this problem with strings or numeric values before. Only with components.

Linked lists are typically modeled using a pair where the first element in the pair is the element at that position in the list and the second item in the pair is a pointer to the next pair in the list. Historically in Lisp and other functional languages the two fields are called car and cdr. So by getting the cdr of the start of the list we skip over the *list* element in the 0th position (the car of the first pair).

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.