> For the complete documentation index, see [llms.txt](https://corman.gitbook.io/evo-api-draft/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://corman.gitbook.io/evo-api-draft/java/behaviors.md).

# 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!');
    }
});
```
