Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Number:

GEP-11

Title:

SAM coercion

Version:1

2

Type:

Feature

Status:

Draft

Leader:

Jochen Theodorou

Created:

2013-05-30

Last modification:

2013-0506-3118

Table of Contents
maxLevel2
excludeMetadata

Abstract

SAM stands for Single Abstract Method.  SAM coercion is here a Groovy style transformation of a groovy.lang.Closure instance into an object suitable for our SAM type. A SAM type is an abstract class or interface with a single abstract method. The coercion can happen as part of an assignment or as the result of a method call. Since this transformation might be outside of the types provided by Closure itself, it can be more than a simple Java style cast. Closure becomes a kind of sub type to all SAM types. Groovy has other such transformations without explicit cast or asType usage, which are number object transformations as well as the conversion of GString to String.

...

Even before Java8 we had discussions about supporting different interfaces with Closure like Runnable and Callable. These two being easy cases, any framework can define a myriad of interfaces and abstract classes. This then requires to "groovify" the library by writing a helper layer capable of transforming Closure objects into something the library then understand. While it is unlikely of this approach to make Groovy Builder surplus, it can still help with a more simple integration.

SAM examples:

  • Simple Interface:

 

Code Block
themeConfluence
languagegroovy
interface Foo {
  def foo()
}
  • Interface with defender method:
  • Interface inheriting from another interface:
  • Interface inheriting from another interface, but not defining a method on its own:
  • simple abstract class
  • abstract class with a abstract and a non abstract class:
  • abstract class extending other class:
  • abstract class implementing interface:
  • abstract class extending other class but not defining an abstract method on its own:

Non-SAM examples:

  • empty interface:
  • interface with two methods:
  • abstract class with two abstract methods:
  • empty abstract class:

Influence on method selection

...