Skip to content

uuid!() and iota!() (and maybe random!()) syntax extensions #7104

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
graydon opened this issue Jun 13, 2013 · 11 comments
Closed

uuid!() and iota!() (and maybe random!()) syntax extensions #7104

graydon opened this issue Jun 13, 2013 · 11 comments
Labels
A-syntaxext Area: Syntax extensions C-enhancement Category: An issue proposing an enhancement or a PR with one. E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.

Comments

@graydon
Copy link
Contributor

graydon commented Jun 13, 2013

uuid!() should generate a UUID (http://en.wikipedia.org/wiki/Universally_unique_identifier). It should take arguments (or there should be multiple variants of the call) to indicate the UUID-variant. The default should just be random. Could possibly defer to a random!() syntax extension that generates a random value at compile time.

iota!() should do something like what the iota expression in Go does: http://golang.org/ref/spec#Iota -- evaluate to a constant integer starting from zero and increment the counter, resetting on some reaosnable boundary such as "per module" or "per scope".

(Nominating for feature complete.)

@emberian
Copy link
Member

I really, really dislike the name iota. Not only does the usage of the name in various languages map to different concepts (APL/C++/D vs Go), the name has no semantic content. It'd be like using i().

@brson
Copy link
Contributor

brson commented Jun 13, 2013

After consideration I don't think that uuid! is suitable for TLS keys, at least not on its own. A UUID alone doesn't encode any type information so a UUID TLS key is not type safe.

@graydon
Copy link
Contributor Author

graydon commented Jun 13, 2013

Ah, fair point. Would need a typed declaration. const v : TlsVar<T> = TlsVar(uuid!()); Could be macroized pretty easily. Not sure at that point it's much better than the address-of-a-function thing we do now (which can also be macroized) but maybe it'll work better cross-crate and on windows?

@graydon
Copy link
Contributor Author

graydon commented Jun 13, 2013

@cmr concerning the name iota, it has a use in english meaning "a small amount", and at least a reasonable number of uses in programming languages: std::iota(begin, end, v) in C++, the (iota n) function in scheme, the (iota) built-in function in APL, and as a magic constant expression in at least two Pike languages (Limbo and Go).

@graydon
Copy link
Contributor Author

graydon commented Jun 13, 2013

Oh I see, you pointed out the languages that have it (including D, I didn't see!) but just think it's inconsistently used. Hmm. I dunno. This is a syntax extension. It's not like you're going to call it without awareness of what it's for. I guess I'm curious what else you'd want here instead? The use case is:

const CODE_FOO : u8 = iota!();
const CODE_BAR : u8 = iota!();
// ...
const CODE_QUUX : u8 = iota!();

In order to define an arbitrary sequence of values that you don't care about the numbering of, just "one more than previous". Similar to use in an enum. I suppose most of the time this can be done as:

const CODE_FOO : u8 = 0;
const CODE_BAR : u8 = CODE_FOO + 1;
const CODE_BAR : u8= CODE_BAZ + 1;
...

It's just a lot wordier (have to repeat each variant) and more prone to copy-paste errors.

@emberian
Copy link
Member

I agree that it's nice to have, but I'd call it incr_id!() or something.
It's just the name I dislike.

@bstrie
Copy link
Contributor

bstrie commented Jun 14, 2013

I actually feel like iota does basically the same thing in D and C++ and Go. iota!() is fine for this purpose.

bors added a commit that referenced this issue Aug 17, 2013

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
Addresses part of #7104

This module adds the ability to generate UUIDs (on all Rust-supported platforms).

I reviewed the existing UUID support in libraries for a range of languages; Go, D, C#, Java and Boost++. The features were all very similar, and this patch essentially covers the union.  The implmentation is quite straightforward, and uses the underlying rng support which is assumed to be sufficiently strong for this purpose.

This patch is not complete, however I have put this up for review to gather feedback before finalising. It has tests for most features and documentation for most functions.

Outstanding issues:

* Only generates V4 (Random) UUIDs. Do we want to support the SHA-1 hash based flavour as well?
* Is it worth having the field-based struct public as well as the byte array?
* Formatting the string with '-' between groups not done yet.
* Parsing full string not done as there appears to be no regexp support yet. I can write a simple manual parser for now?
* D has a generator as well. This would be easy to add. However, given the simple interface for creating a new one, and the presence of the macro, is this useful?
* Is it worth having a separate UUID trait and specific implementation? Or should it just have a struct+impl with the same name? Currently it feels weird to have the trait (which can't be named UUID so as to conflict) a separate thing.
* Should the macro be visible at the top level scope?

As this is a first attempt, some code may not be idiomatic. Please comment below...

Thanks for all feedback!
@catamorphism
Copy link
Contributor

Just a bug, de-nominating

@pongad
Copy link
Contributor

pongad commented Jan 11, 2014

I am not quite sure why we need iota. It seems to me the reason Go uses iota is because it lacks enums. If there's still need for it, however, I would suggest something like

iota!(u8, FOO, BAR, BAZ)

as it would make the resets clear. One can also align the argument like

iota!(u8, FOO,
          BAR,
          ...)

@pongad
Copy link
Contributor

pongad commented Feb 13, 2014

Does something like this look right? https://gist.github.com/pongad/8968393

@brson
Copy link
Contributor

brson commented Jan 13, 2015

Old bug, demand has subsided. I don't think there's any value keeping it around.

@brson brson closed this as completed Jan 13, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-syntaxext Area: Syntax extensions C-enhancement Category: An issue proposing an enhancement or a PR with one. E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.
Projects
None yet
Development

No branches or pull requests

6 participants