Skip to content

Refactoring: use type aliases (in utils/Misc) to improve interfaces readability#2227

Merged
gasche merged 11 commits intoocaml:trunkfrom
gasche:misc-type-aliases
Jan 31, 2019
Merged

Refactoring: use type aliases (in utils/Misc) to improve interfaces readability#2227
gasche merged 11 commits intoocaml:trunkfrom
gasche:misc-type-aliases

Conversation

@gasche
Copy link
Copy Markdown
Member

@gasche gasche commented Jan 27, 2019

This PR is the first part of a larger refactoring I've been working on for typing/env. It should be a complete no-op, as it does not touch any runtime code, only types.

(Easy to review!)

@trefis
Copy link
Copy Markdown
Contributor

trefis commented Jan 27, 2019

It should be a complete no-op, as it does not touch any runtime code, only types.

Well, you're adding some open Misc in places. I'll agree that it probably doesn't change anything. But it's not obvious.

Btw, I don't like this PR: I'm against "transparent" type aliases, they are arguably nicer to read, but they don't really bring you any extra safety and the added information is only available at the declaration, not when used.
I'd rather see labels added to functions, exception/constructors defined using inline records, than having them declared using type aliases.
Another option would of course be to use abstract types (which would be aliases under the hood, I'm fine with that), but that's a lot more heavy weight.

That's of course only my personal opinion and I'd be curious to hear what you and/or other people think.

@gasche
Copy link
Copy Markdown
Member Author

gasche commented Jan 27, 2019

I think that transparent type aliases definitely improve the situation, because they make declarations readable -- in many cases they were not before. I could not go on with my refactorings without them. If you do believe they are a strict improvement, then I think you could approve the PR.

What you have in mind is indeed better, but it makes changes much more invasive as all use-sites have to be changed. It would be nice work to do, but these changes are not the focus of my work, just side-works, so I don't want to spend a lot more effort on them for now. Anyone is free to come after this change and use type modname = Modname of string [@@unboxed] -- it is much easier to do after this PR. But I don't think the two steps of changes should be required to happen at the same time.

@Armael
Copy link
Copy Markdown
Member

Armael commented Jan 28, 2019

It's not a panacea, but there's indeed some added value in this PR.
The aliases do not enforce anything statically, but they at least document informally what these string types stand for. I think that information is always good to have; it would be disappointing to not integrate it and continue having everyone re-doing the job of inferring it from the code.

@alainfrisch
Copy link
Copy Markdown
Contributor

I'm against "transparent" type aliases, they are arguably nicer to read, but they don't really bring you any extra safety

The advantage compared to the current situation is clear ("nicer to read") and I don't see any disadvantage. In particular, this PR does not make it more difficult to evolve to something that would bring more safety (quite the opposite actually). The only risk is that a wrong annotation could indeed be misleading, but I trust @gasche was careful enough. So count me in favor.

@trefis
Copy link
Copy Markdown
Contributor

trefis commented Jan 28, 2019

Ack. I'm not planning to veto that PR anyway.
I was just saying that:

The aliases do not enforce anything statically, but they at least document informally what these string types stand for.

doesn't convince me. A docstring would have done just as well.

@Drup
Copy link
Copy Markdown
Contributor

Drup commented Jan 28, 2019

I tend to agree with @trefis in this case, but I still think that this PR goes in the right direction. I'm not fond of putting things in Misc though.

I'm pretty sure making crcs abstract would have very little cost. IIRC, it's only really used in Consistbl, and could live there directly. That would be a nice change to do later.

Copy link
Copy Markdown
Member

@Armael Armael left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@trefis one advantage of type aliases compared with docstrings is that it makes it easy to change the value of the alias, for example to transition from string to a better representation or an abstract type.

I think there is consensus that this PR goes in the right direction overall and makes further refactoring easier (such as the one @gasche submitted in a more recent PR).

I'm approving. @gasche : you need to make check-typo happy it seems.

@gasche gasche force-pushed the misc-type-aliases branch from 0a4ac61 to 6b0e9d1 Compare January 30, 2019 16:45
@gasche gasche force-pushed the misc-type-aliases branch from 6b0e9d1 to 1d75048 Compare January 30, 2019 21:48
@gasche
Copy link
Copy Markdown
Member Author

gasche commented Jan 31, 2019

I fixed check-typo but Changes, for some reason, still complains. Merging nonetheless to encourage people to look at #2228.

@gasche gasche merged commit c7cc34a into ocaml:trunk Jan 31, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants