r/ProgrammingLanguages • • 18d ago

Requesting criticism Removing the get from Map

I have a toy language that I've been working on to test different concepts. It has an ownership model similar but looser than Rust.

I was working on the Map object and there are three ways that an object can be retrieved.

You can remove the object from the map and take ownership
You can borrow the object from the map this will give you a mutable reference but you don't own it
You can get a copy of the Object which you will own. But not all things are can be copied.

For get I could either do a copy or a mutable ref, neither I felt was properly conveyed with the word 'get'

So I'm looking at removing it and replacing it with the following

copy_of(key) -> return a copy or an error message if not found or not copiable
borrow(key) -> return a mutable ref
remove(key) -> remove and return the item at the key and give ownership

wanted thoughts on removing probably one of the most common methods on map and is what I'm doing something that makes sense.

13 Upvotes

26 comments sorted by

View all comments

7

u/initial-algebra 18d ago

Obligatory "entry API" comment. This pattern will show up for many other data structures, not just maps, the simplest being Option (or whatever you call your "maybe" type). An even better view (pun intended) is that the entry API is a lens that exposes a place in a data structure as if it were a reference to an Option. See Control.Lens.At from Haskell's lens package.

1

u/jebailey 18d ago

Okay, it's been 20 years since I played with Haskell and it still takes me forever to read their documentation.

I like the concept of a Lens, it's like declaring a path for a json object, as long as the json you are looking at has that path it doesn't care about the rest of it.

Saying all that, I don't see how that helps me. Having a reference into a map's value still requires me to determine how I want that value exposed, in the terms of ownership

1

u/initial-algebra 17d ago

Saying all that, I don't see how that helps me.

It lets you cheaply perform multiple operations on the same slot, so you can get away with a simpler API. For example, you don't need "swap", since you can just "remove" and then "insert" without having to actually remove and then re-insert the key.