Skip to content

alias: mention how to actually create new types #151

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

Closed
vks opened this issue Jul 1, 2014 · 4 comments
Closed

alias: mention how to actually create new types #151

vks opened this issue Jul 1, 2014 · 4 comments

Comments

@vks
Copy link
Contributor

vks commented Jul 1, 2014

I'm not sure whether the current example is a good idea, because it exercises a bad practice that results in something you would not want to do. (That is adding quantities in wrong units.)

At least it should be mentioned how the unit example can actually be implemented correctly (I think the idiomatic way is to use one-field structs).

Also see https://blog.mozilla.org/research/2014/06/23/static-checking-of-units-in-servo/.

@japaric
Copy link
Member

japaric commented Jul 2, 2014

I'm not sure whether the current example is a good idea, because it exercises a bad practice that results in something you would not want to do. (That is adding quantities in wrong units.)

Yeah, I agree :-(. The alias example is under the type chapter because is highly related, but the problem is that at that point of the book I can't think of a good example for the type idiom, since it's main use is reducing the verbosity of type names as in type IoResult<T> = Result<T, IoError>.

At least it should be mentioned how the unit example can actually be implemented correctly

I'm thinking of moving that example to the staging area for maintenance. Until I decide where to put it, probably after struct and generic chapters.

Alternatively, we could just remove the example code along with the editor, and just leave the text?

(I think the idiomatic way is to use one-field structs).

Yes, that's the newtype pattern.

Also see https://blog.mozilla.org/research/2014/06/23/static-checking-of-units-in-servo/.

Yep, @mdinger is working on this in issue #145


Hmm, giving it more thought. I think we can group these topics under a newtye (maybe not the best name) chapter giving progressive examples using the unit checking theme:

  • type: type aliasing, no new type is created, can't do unit checking
  • newtype: unit checking using struct tuples: Inches(f64), Centimeters(f64)
  • phantom types: "better" encapsulation for unit checking.

@vks @mdinger What do you think about that last idea? And/or about temporally removing the type/alias example.

@vks
Copy link
Contributor Author

vks commented Jul 2, 2014

I think it is still useful to learn about type, so I would not remove it entirely, maybe just the example, but not the explanation.

Another possible use case could be defining

type Real = f32;

in some numerical code, so that it is trivial to switch to f64 if more precision is needed.
(This is a typical C pattern that is probably not as useful in Rust because of generics.)

@mdinger
Copy link
Contributor

mdinger commented Jul 2, 2014

I don't know if the alias example should be removed. If it isn't, you should link to where you discuss the proper thing to do. As it is, you say "don't do this" but don't tell them what to do.

I do think newtype usage seems to be similar to phantom types; that is, they can both help with unit conversion. So it would be helpful to introduce them together so they can be contrasted. However, phantom types comes from generics and newtype from struct so putting them together isn't as clean cut.

I'm finding it difficult to expand on phantom types without getting into new material because Generics is before Borrowing, ref, impl, lifetimes, and maybe more (aside from being new). If I don't expand on it, people will wonder what possible use phantom types are.

I think one solution is to describe it using the bare minimum syntax only requiring what has been learned so far. Then a difficult examples section where more elaborate examples can be included which use more comprehensive syntax. These can have varying difficulties of syntax and should use heavy referencing to other sections readers know where to look for answers.

Then, newtype could be introduced in struct and phantom types in generics. They could link to each other for comparison or something...

@steveklabnik
Copy link
Member

I'm going to close this in favor of #174

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

4 participants