Skip to content

[Android] Add Kotlin collection factory functions for Maps and Arrays - #54937

Open
l2hyunwoo wants to merge 1 commit into
react:mainfrom
l2hyunwoo:feat/mutable-collections-factory-function
Open

l2hyunwoo wants to merge 1 commit into
react:mainfrom
l2hyunwoo:feat/mutable-collections-factory-function

Conversation

@l2hyunwoo

Copy link
Copy Markdown
Contributor

Summary:

This PR is based on the proposal: react-native-community/discussions-and-proposals#966

Add Kotlin collection factory functions for maps (WritableMap, ReadableMap) and arrays (WritableArray, ReadableArray)

Introduce idiomatic Kotlin factory functions for creating React Native bridge collection types:

  • writableMapOf(): Creates WritableMap with vararg key-value pairs
  • writableArrayOf(): Creates WritableArray with vararg elements
  • readableMapOf(): Returns ReadableMap
  • readableArrayOf(): Returns ReadableArray

These functions mirror Kotlin's standard library conventions (mapOf/mutableMapOf) and provide a more concise alternative to the builder DSL pattern when constructing small, static collections.

Supported types: null, Boolean, Int, Long, Float, Double, String, ReadableMap, ReadableArray.

Long and Float are converted to Double for JavaScript compatibility.

Changelog:

[ANDROID] [ADDED] - Add Kotlin collection factory functions: writableMapOf, writableArrayOf, readableMapOf, readableArrayOf

Test Plan:

Unit tests added in CollectionFactoryTest.kt covering:

  • Empty collections
  • Primitive values (String, Int, Boolean, Double)
  • Null handling
  • Long/Float to Double conversion
  • Nested maps and arrays
  • Error handling for unsupported types
스크린샷 2025-12-19 오전 10 40 31

Add Kotlin collection factory functions for maps(WritableMap, ReadableMap) and arrays(WritableArray, ReadableArray)

Introduce idiomatic Kotlin factory functions for creating React Native bridge collection types:

- `writableMapOf()`: Creates WritableMap with vararg key-value pairs
- `writableArrayOf()`: Creates WritableArray with vararg elements
- `readableMapOf()`: Returns ReadableMap
- `readableArrayOf()`: Returns ReadableArray

These functions mirror Kotlin's standard library conventions (mapOf/mutableMapOf) and provide a more concise alternative to the builder DSL pattern when constructing small, static collections.

Supported types: null, Boolean, Int, Long, Float, Double, String, ReadableMap, ReadableArray. Long and Float are converted to Double for JavaScript compatibility.
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Dec 19, 2025
@facebook-github-bot facebook-github-bot added the Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team. label Dec 19, 2025
@l2hyunwoo

Copy link
Copy Markdown
Contributor Author

@cortinico Hello! Can you review this PR? This PR's goal is same as previous one.

*
* @throws IllegalArgumentException if a value is of unsupported type
*/
public fun writableMapOf(vararg pairs: Pair<String, Any?>): WritableMap {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar to 78dbbaa you should put those inside WritableArrayBuilder and so on

@react-native-bot

Copy link
Copy Markdown
Collaborator

This PR is stale because it has been open for 180 days with no activity. It will be closed in 7 days unless you comment on it or remove the "Stale" label.

@react-native-bot react-native-bot added Stale There has been a lack of activity on this issue and it may be closed soon. and removed Stale There has been a lack of activity on this issue and it may be closed soon. labels Aug 10, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants