Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PR: Safe Edge Addition API with Result Type
Summary
This PR introduces a breaking change to the edge addition API to address partial integrity (aka Ghost Nodes) — nodes that exist in edge dictionaries but not in the graph's nodes map. The
add_edge()function now returnsResult(Graph, String)instead ofGraph, forcing explicit handling of missing node scenarios.Breaking Changes
Edge Addition API
add_edge()now returnsResult(Graph, String)Error("Node X does not exist")if either endpoint node is missingMigration Guide:
Renamed Functions
add_edge_ensured()→add_edge_ensure()add_edge_ensured_with()→add_edge_with()Added
Bulk Edge Addition Functions
New convenience functions for adding multiple edges with a single
Resultunwrap:add_edges(graph, edges: List(#(NodeId, NodeId, e)))— Add weighted edgesadd_simple_edges(graph, edges: List(#(NodeId, NodeId)))— Add edges with weight 1add_unweighted_edges(graph, edges: List(#(NodeId, NodeId)))— Add edges with weightNilThese functions fail fast on the first missing node, reducing boilerplate compared to chaining individual
add_edgecalls:Rationale
The Partial Integrity Problem
Previously,
add_edgewould silently add entries to edge dictionaries even when nodes didn't exist ingraph.nodes. This caused:all_nodes()(would miss ghost nodes)Design Decisions
Why not panic? Panicking on missing nodes is not the Gleam way — explicit error handling is preferred.
Why not make
add_edge_ensurethe default? Too verbose for the common case where nodes are already added. The explicitResulttype makes the contract clear.Why bulk functions? They maintain the safety of
Resultwhile reducing verbosity for the common pattern of adding multiple edges at once.Documentation
GLEAM_FSHARP_COMPARISON.mddocumenting feature parity and migration guidance between Gleam and F# implementationsadd_edgesor properResulthandlingMigration Checklist
|> add_edge(patterns — replace with|> add_edges([...])where possiblelet graph = ... |> add_edge(— addassert Ok(graph) =or useadd_edge_ensureadd_edge_ensured→add_edge_ensureall_nodes()— they now have consistent behavior