dex-lang icon indicating copy to clipboard operation
dex-lang copied to clipboard

`with_` better names

Open axch opened this issue 2 years ago • 0 comments

The <- binding syntax introduced by PR #1147 makes with_ names less nice. While

with_state init \x.
  ...

read reasonably ok, the new style

x <- with_state init
...

reads pretty poorly.

The fundamental reason for this is that when the continuation is indented and the function is first, it's fairly clear that the name refers to what it does to the continuation as a whole; whereas when the continuation is not indented and the binder is first, there is a pretty strong message that the function's name is about the kind of object it produces, and its effect on the continuation isn't very important --- fades into the background, takes care of itself.

Therefore, I think it's worth thinking about new names for those functions. Particularly the ones whose names currently start with with_ all have the flavor of introducing some resource, and maybe some type-level restrictions on how that resource is to be used, and then cleaning the resource up when the block ends. This means they will probably be used predominantly with the <- syntax. Here are all the with_ functions in the current Dex corpus:

  • with_state
  • with_reader
  • with_dynamic_buffer
  • with_alloc
  • with_file
  • with_temp_file
  • with_temp_files
  • with_c_string
  • with_table_ptr

The name of with_state I currently dislike pretty substantially, because it names the (ahem) effect it has on the continuation rather than the resource it provides. It's also used a great deal, so its name matters to the feel of the language. (Aside: It also annoys me that the binder is almost everywhere named ref instead of being named after its contents, but I can fix that along the way.) How about these other names for with_state:

  • var
  • variable
  • reference
  • new_reference
  • new_variable
  • new
  • box

Thoughts? The only down-side I can see is obscuring the relationship between with_state and its cousins yield_state and run_state. But run_state is almost completely unused in the corpus (except to implement with_state and yield_state), so we can call it anything we want, and we could reconsider the name yield_state as well.

I dislike with_reader for the same reason as with_state but it doesn't matter because it's unused.

The others are all of the same flavor: they provide some resource in the IO effect, and then clean it up when the continuation ends, returning the value of the continuation unchanged. Ergo, they will likely be used exclusively with <- syntax, so should read well in it. We could reasonably

  • Just remove the with_ prefix, leaving x <- dynamic_buffer ..., x <- c_string "foo", etc.
  • Change with_ to new_ to emphasize that something is being created or provided
  • Perhaps auto_, following C++ auto-pointers, which are auto-freed when they leave scope?

Thoughts?

axch avatar Nov 07 '22 21:11 axch