igraph / rigraph

igraph R package
https://r.igraph.org
532 stars 200 forks source link

why do autogen functions create a res object even when it's directly returned #1411

Closed maelle closed 1 week ago

maelle commented 2 weeks ago

rather than just having the .Call at the end of the function

szhorvat commented 2 weeks ago

In some cases the result needs to be annotated with additional information. One common example is graph generators. For example, static_fitness_game_impl has:

  if (igraph_opt("add.params")) {
    res$name <- 'Static fitness model'
    res$loops <- loops
    res$multiple <- multiple
  }

Another example is adding vertex names to vertex quantities, which is very common. For example eigenvector_centrality_impl has:

  res <- .Call(R_igraph_eigenvector_centrality, graph, directed, scale, weights, options)
  if (igraph_opt("add.vertex.names") && is_named(graph)) {
    names(res$vector) <- vertex_attr(graph, "name", V(graph))
  }

You might find other examples by browsing the generated code.

maelle commented 1 week ago

Thanks, it makes sense!