Skip to content

Mixins : Decomposable

johnmcclean-aol edited this page Feb 24, 2016 · 2 revisions

Cyclops has merged with simple-react. Please update your bookmarks (stars :) ) to https://github.com/aol/cyclops-react

All new develpoment on cyclops occurs in cyclops-react. Older modules are still available in maven central.

screen shot 2016-02-22 at 8 44 42 pm

The Decomposable Interface

Implementing com.aol.cyclops.lambda.api.Decomposable adds a default unapply method that decomposes the host Object into an Iterable over it's fields. This can be very useful for pattern matching recursively against fields.

Coercing to Decomposable

Any Object can be coerced to a Decomposable via the following methods

  • AsDecomposable.asDecomposable
  • As.asDecomposable

When unapply() is called on coerced Decomposable

  • If the Object is an iterable it will be returned
  • Otherwise, the values of the Object fields will be returned in an Immutable Array List

Example

With Lombok @Value

  assertThat(As.asDecomposable(new MyCase("key",10))
			.unapply(),equalTo(Arrays.asList("key",10)));
 
  @Value static class MyCase { String key; int value;}
Clone this wiki locally