Skip to content

pseudo-overloading functions with enums #1838

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
erickt opened this issue Feb 14, 2012 · 6 comments
Closed

pseudo-overloading functions with enums #1838

erickt opened this issue Feb 14, 2012 · 6 comments
Labels
A-type-system Area: Type system C-enhancement Category: An issue proposing an enhancement or a PR with one.

Comments

@erickt
Copy link
Contributor

erickt commented Feb 14, 2012

I've been working on a couple different serialization libraries, and I often find taking advantage of enum variants living in a different namespace than types. It's really nice to declare:

enum t {
  int(int),
  str(str),
  vec([t]),
}

fun to_str(x: t) {
   alt x {
     int(i) { int::to_str(i) }
     str(s) { s }
     vec(v) { "[" + str::concat(vec::map(v, to_str), ",") + "]" }
   }
}

But calling these functions can get a bit noisy, especially when using arrays:

to_str(vec([int(1), str("foo"), vec([int(2), str("bar")])]))

I'm not sure if we're planning on supporting overloading functions, but what if the type system could automatically box arguments if an enum name is the same as the type? As in allowing us to write:

to_str([1, "foo", [2, "bar"]])

and automatically convert it to the boxed form from above. I'm not sure if this would play well with generics or typeclasses, but it'd really clean up some code I'm writing. Is this possible, or even a good idea?

@marijnh
Copy link
Contributor

marijnh commented Feb 14, 2012

This looks like a poster-child use-case for impls. Wouldn't that be a much simpler and more efficient way to do it?

iface to_str { fn to_str() -> str; }
impl of to_str for int { fn to_str() -> str { int::str(self) } }
impl of to_str for str { fn to_str() -> str { self } }
impl <A: to_str> of to_str for [A] { fn to_str() -> str {
    "[" + str::connect(vec::map(self, {|e| e.to_str()}), ", ") + "]"
} }

fn main() { [1, 2, 3].to_str(); }

@nikomatsakis
Copy link
Contributor

@elly wanted this too.

I agree with @marijnh that impls are good here, except that they cannot accommodate something like ["hi", 1].to_start().

@marijnh
Copy link
Contributor

marijnh commented Feb 14, 2012

except that they cannot accommodate something like ["hi", 1].to_start().

Using boxed iface values, they can.

@nikomatsakis
Copy link
Contributor

Using boxed iface values, they can.

Yes, I know... what I meant is, you can't write it that way. We'd have to auto-coerce to a boxed iface. I think that so long as boxed iface allocations require allocation this isn't such a good idea, but maybe with #1567 it would be more reasonable.

@catamorphism
Copy link
Contributor

Looks like "revisit this when #1567 is done" to me.

@graydon
Copy link
Contributor

graydon commented Aug 29, 2012

This bug is effectively asking for implicit-conversion operators. At the point we're at now, I'm .. pretty disinterested in those as a feature; they have a lot of expressive power but they interact with a lot of already-overcomplex pieces of the language. Also, IME they tend to bring a lot of headache to maintenance programmers / readers when trying to figure out what code does (for example, in C++).

I think we have a bunch of reasonable ways of doing polymorphic code now that are not too painful, and could grow more (for example, I think we're nearly at the point of being able to write the any type in plain library code, using visitors), as far as expressing the motivating case for the bug itself. So I'm going to close with effectively a WONTFIX here, but if you vigorously disagree, reopen and state why.

@graydon graydon closed this as completed Aug 29, 2012
celinval added a commit to celinval/rust-dev that referenced this issue Jun 4, 2024
bors pushed a commit to rust-lang-ci/rust that referenced this issue Jan 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-type-system Area: Type system C-enhancement Category: An issue proposing an enhancement or a PR with one.
Projects
None yet
Development

No branches or pull requests

5 participants