Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[examples] Consider using pointer or non pointer for the Tree type when using p/demo/avl #3683

Open
jeronimoalbi opened this issue Feb 4, 2025 · 1 comment

Comments

@jeronimoalbi
Copy link
Member

Description

The p/demo/avl package has a constructor function NewTree() that returns a pointer *Tree. In may cases this function is not used because the Tree type can also be defined as a non pointer and called without the need of using the constructor function, for example:

package main

import "gno.land/p/demo/avl"

func main() {
  // Non pointer type
  var treeA avl.Tree
  println(treeA.Size())

  // Pointer type
  var treeB *avl.Tree = avl.NewTree()
  println(treeB.Size())
}

// Output:
// 0
// 0

This is currently possible because the underlying Node type that Tree uses is implemented in a way that handles all relevant nil cases with defaults.

Using a non pointer for the Tree type is convenient for example to have tree variables/properties initialized by default without the need to call NewTree().

It might be worth considering if the avl package, which is widely used, should favor and maybe settle for a pointer or non pointer approach by changing the package implementation to that purpose.

The downside of settling in one of the approaches is that it could be a breaking change.

@jeronimoalbi
Copy link
Member Author

cc @leohhhn @moul @x1unix

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Triage
Development

No branches or pull requests

1 participant