Skip to content

Erlang Binary functions#374

Closed
atavistock wants to merge 13 commits into
bartblast:devfrom
atavistock:atavistock/create_erlang_binary
Closed

Erlang Binary functions#374
atavistock wants to merge 13 commits into
bartblast:devfrom
atavistock:atavistock/create_erlang_binary

Conversation

@atavistock
Copy link
Copy Markdown
Contributor

#355

The scope of the issue was for :binary.split/2 and :binary.split/3

  • This required creating :binary.compile_pattern/1 and implementing the existing matching algorithms, which was the bulk of the actual work to get the initial scope finished.
  • Because those matching algorithms were finished, it made adding :binary.match/2, :binary.match/3, :binary.matches/2, :binary.matches/3, :binary.replace/3, and :binary.replace/4 became relatively easy to implement as they also rely on those algoithms for the heavy lifting, and they are consequently included in this pull request.

@bartblast
Copy link
Copy Markdown
Owner

Hey @atavistock!

Thanks for jumping on this so quickly - really appreciate how thorough the :binary work is. I do need to ask you to break this PR up so review stays manageable.

GitHub issues: I created one per function. Please comment anything on each so I can assign you officially:

If you think the shared matching algorithms need their own issue, feel free to create one - though ideally due to how Hologram's compiler works, it's better to inline the code with the functions themselves (see below).

Why split: I'm swamped with PRs. Separate PRs let me review faster, unblock functions independently, and make it clear to others which functions are already claimed (per the initiative flow).

Code organization reminder: The compiler only sees code that's reachable via Deps definitions in the call graph (see the Contributing Guide). So instead of extracting validation or shared logic into private helpers:

  • Prefer having the smaller-arity function delegate to the larger one with defaults.
  • If you truly need shared utilities, either add them to the runtime (so every app ships with it) or wrap them in the Start/End extraction markers and register them in the call graph definitions so the compiler pulls them in. Until then, keep validations inline - we'll DRY things up later.

Quick note: The CI formatting check found some issues. You can fix them by running $ mix f from the root directory.

Thanks again - your contributions are awesome, we just have to keep the process tight so review doesn't bottleneck. Once the issues are claimed and PRs split, I can move fast on reviews :)

@bartblast
Copy link
Copy Markdown
Owner

bartblast commented Nov 19, 2025

I've just added "Quality Checks Before Submitting" section here: https://hologram.page/reference/contributing, though to get the updated checks you'll need to sync with the dev branch.

@atavistock
Copy link
Copy Markdown
Contributor Author

I'm happy to split these apart into separate incremental PRs. I can certainly understand how it will be much easier to digest whats happening and the changes being made.

I would like to understand this a little better:

If you think the shared matching algorithms need their own issue, feel free to create one - though ideally due to how Hologram's compiler works, it's better to inline the code with the functions themselves (see below).

Erlang uses two fairly complex algorithms done in a C .nif for the pattern handling which are decided based on the input. Each of those algorithms does searching, matching, and replacing differently, but they also share a lot of the same boiler plate. Implementing both of them in javascript takes 600 or 900 lines of javascript depending on if we're using an abstract class. Inlining the code into the mock Erlang calls would make for a massive amount of duplication in each of the binary operations. I don't know the implications for Hologram, but I think that amount of duplication would be a significant problem for maintenance and complexity.

If we eschew the simpler algorithm completely, it could make inlining search, match, and replace much more clear, but they'd still share a lot of duplicated code for the alogirthm implementation itself.

Curious how you feel we should go here?

@bartblast
Copy link
Copy Markdown
Owner

Hi Aaron, I get it - the shared code is complex and avoiding duplication makes total sense.

I think the best approach is to implement them as "virtual" Erlang functions with underscored names (e.g., :binary._my_fun/1). This way they:

  • Stay in binary.mjs with Start/End markers
  • Can be declared in Deps and tracked in the call graph
  • Can have isolated tests
  • Get extracted by the compiler only when needed
  • Won't be confused with actual Erlang API functions (underscore prefix convention)

You could even model class instance-like structures this way if needed.

What do you think about this approach? Also, can the matchers be split into 2-3 separate issues (beyond the actual :binary function PRs) to make review more manageable, or do they need to go together in one PR?

@atavistock
Copy link
Copy Markdown
Contributor Author

atavistock commented Nov 26, 2025 via email

@bartblast
Copy link
Copy Markdown
Owner

Thanks, I proposed a solution here: #418 (comment)

@bartblast
Copy link
Copy Markdown
Owner

@bartblast bartblast closed this Feb 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants