Skip to content
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

Add include and extend to Enum #8946

Open
confact opened this issue Mar 26, 2020 · 6 comments
Open

Add include and extend to Enum #8946

confact opened this issue Mar 26, 2020 · 6 comments

Comments

@confact
Copy link

confact commented Mar 26, 2020

I would like the posibility to use include and extend on Enum.

Now they are added to Enum.class but not to the actual Enum.

Say we do:

enum Status
  Opened
  Closed
end

And extend or include in an enum it will be only in Enum. And using include and extend won't work inside enum Status

So this won't work, but would probably solve the issue i am telling in detail below:

enum Status
  Opened
  Closed

  include OrmEnumType
  extend OrmEnumExtendType
end

Why I want this?
Most ORM adds module or methods to parse, get data from database and to database to a type, so-called type extensions. Enum is handling include and extend differently making this not work. And can therefore not use Enums as a type for columns without some type of wrapper class around the enum to cast it correctly and so on.

@vlazar
Copy link
Contributor

vlazar commented Mar 26, 2020

Already addressed by Ary in #8943 ❤️

@Blacksmoke16
Copy link
Member

Related it's also not possible to use enums as a namespace https://play.crystal-lang.org/#/r/auuz.

enum Test
  One
  Two
  
  class Bar
  end
end
 
pp Test::Bar.new # => Error: unexpected token: class

Maybe there's a reason for this but seems a bit unexpected.

@asterite
Copy link
Member

The reason is that all constants inside the enum are the enum members. If we allow classes or modules then that's not true anymore. But maybe it's a restriction we can remove.

@straight-shoota
Copy link
Member

If we consider allowing to use types in enum namespace, we could also consider arbitrary, non-member constants. This would allow doing this: #10281 (comment) (see OP for context).

@HertzDevil
Copy link
Contributor

HertzDevil commented Nov 15, 2021

There is a possibly unintended alternative to extend:

enum A
  X
end

module B
  def foo
  end
end

alias AClass = A.class

class AClass
  include B # okay
end

A.foo # okay

The only difference is it would call a different macro hook. (Note that extending a module inside AClass would be semantically similar to including it inside Class, which affects all metaclasses.)

@jcads
Copy link

jcads commented Feb 23, 2023

Why was #8943 not merged?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants