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

Type-specific term types #95

Open
hansihe opened this issue May 21, 2017 · 1 comment
Open

Type-specific term types #95

hansihe opened this issue May 21, 2017 · 1 comment

Comments

@hansihe
Copy link
Member

hansihe commented May 21, 2017

This is mostly a suggestion. I am unsure if this is a good idea, and am looking for input on the matter.

The situation today

With NifTerm today, doing stuff like constructing a map is a little awkward. You end up having to do stuff like:

map_new(env)
    .map_put(key, value).ok().unwrap()
    .map_put(key, value).ok().unwrap()

Doing it this way carries several disadvantages with it:

  • More boilerplate. There is loads of code that shouldn't need to be there in the above code example.
  • Less errors are detected at compile-time. When we create an empty map, we know the type of that term is map a map. Even though we know that, there is nothing stopping us today from calling something like a list function on such a term today.
  • Potentially more runtime checks.

How this could be remedied

I have two solution for how this could be solved:

  1. Introduce a separate and more specific NifTerm type for each term. This is somewhat the same as what we have for atoms today. (although that is done for different reasons)
  2. Have another type parameter on NifTerm, NifTerm<Variant>. This Variant type parameter would normally be Any. This could have all the method implementations we have today. When calling a method with a guaranteed return type (like map_put, which will always return a map term), we can return a term parametrized with a Map type. NifTerm<Map> could then have specific implementations which can not fail with type errors.

Any and all feedback is welcome. Is this a good idea? Any improvements? @scrogson @jorendorff

@scrogson
Copy link
Member

scrogson commented Jul 11, 2017

From and API ergonomics perspective I'd like to see this:

use rustler::term::Map;

let mut map = Map::new(env);
map.put(key, "string".to_string());

assert_eq!(map.get(key), Some("string"));

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

No branches or pull requests

2 participants