amazon-ion / ion-element-kotlin

IonElement is an immutable in-memory representation of the Ion data format. IonElement's API is idiomatic to Kotlin.
Apache License 2.0
8 stars 8 forks source link

Improve usage of constructor functions from Java #50

Closed dlurton closed 3 years ago

dlurton commented 3 years ago

Instantiating a single scalar element in Java looks like this, which is way too syntactically heavy:

IntElement elem = Ion.ionInt(42, Collections.emptyList(), Collections.emptyMap())

The reason is because Java does not recognize the default parameter values of Kotlin functions and so they must be specified even if they are empty. To fix this we should use @JvmOverloads on all element constructor functions.

All constructor functions in this file should look something like this:

/** Creates a [BoolElement] that represents an Ion `bool`. */
@JvmOverloads
fun ionBool(
    b: Boolean,
    annotations: List<String>? = emptyList(),
    metas: MetaContainer? = emptyMetaColntainer()
): BoolElement = BoolElementImpl(b, annotations, metas)

Usage from Java should be:

// no metas, no annotations
IntElement elem = Ion.ionInt(42);

// just annotations
IntElement elem = Ion.ionInt(42, Collections.singletonList("meaning_of_life"));

// just metas
Map<String, Object> metas = ...
IntElement elem = Ion.ionInt(42, metas);

Which is an significant improvement.