# Behaviors

Consider the following: Java has an interface called `Iterable`, which many types of Java collections implement. Now, if we found a way to make it follow the [iterable protocol](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols), we could easily iterate over any Java class in TypeScript.

This is where the Behavior API comes in. You define a **predicate**, and an **apply** as a behavior. Once that Java class is initialized somewhere, if it meets the predicate, it will be applied.

Keep in mind this is the TypeScript API. There will almost certainly also be a Java API.

```typescript
Java.behavior({
    predicate(object) {
        return object instanceof Iterable;
    }
    
    apply(object) {
        object[Symbol.iterator] = *function() {
            let iterator = object.iterator();
            while (iterator.hasNext()) {
                yield iterator.next();
            }
            
            return this;
        }
    }
});
```

Three types of behaviors will be natively implemented:

* Native `Iterable` support.

```typescript
for (const el of javaIterable) {
    console.log(el); 
}
```

* One-method interfaces will have a constructor to make them from lambdas.

```typescript
let runnable = new Runnable(() => console.log("Hi!"));
```

* Interfaces will have a constructor to turn objects into them. Each object will have its own method.

```typescript
let runnable = new Runnable({
    run() {
        console.log('Hi!');
    }
});
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://corman.gitbook.io/evo-api-draft/java/behaviors.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
