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

Can't cast symbol to enum in initialize() arg list #6279

Closed
hinrik opened this issue Jun 27, 2018 · 5 comments
Closed

Can't cast symbol to enum in initialize() arg list #6279

hinrik opened this issue Jun 27, 2018 · 5 comments

Comments

@hinrik
Copy link
Contributor

hinrik commented Jun 27, 2018

I wanted to replace the fully-qualified enum references in my code with symbols following #6074 being released, but I can't do that everywhere it seems.

enum MyEnum
  One Two
end

# works
foo(:two)

def foo(number : MyEnum = MyEnum::One)
  p number
end

# works
Bar.new(:two)

class Bar
  @number : MyEnum
  def initialize(number : MyEnum = MyEnum::One)
    @number = number
    p @number
  end
end

# works
Baz.new(:two)

class Baz
  def initialize(@number : MyEnum = MyEnum::One)
    p @number
  end
end

# doesn't work: instance variable '@number' of Quux must be MyEnum, not Symbol
Quux.new(:two)

class Quux
  def initialize(@number = MyEnum::One)
    p number
  end
end

# doesn't work: instance variable '@number' of Bla must be MyEnum, not Symbol
Bla.new(:two)

class Bla
  @number : MyEnum
  def initialize(number = MyEnum::One)
    @number = number
    p @number
  end
end

Can this be made to work?

@asterite
Copy link
Member

Right, the rule is: if there's an enum type restriction, you can pass a symbol. Going from there to "if there's an argument with a default value, and that argument gets assigned to an instance variable, and that instance variable has an enum type, then..." is a bit of a stretch, and it's way harder to implement and understand. Please let's not go this route.

It's simpler to add the type restrictions. It takes less time than writing a whole github issue ;-)

@hinrik
Copy link
Contributor Author

hinrik commented Jun 27, 2018

I see. I thought a default value also imposed a type restriction on the argument.

@asterite
Copy link
Member

Not necessarily. A default value's type could be a subset of the type. For example the default value could be MyEnum::One and the type could be MyEnum | String. Then we'd need to somehow record that the type restriction is a superset of MyEnum, because of the default value, but that will make things super complex to implement, really. I'd try to avoid this route.

@hinrik
Copy link
Contributor Author

hinrik commented Jun 27, 2018

Makes sense.

@asterite
Copy link
Member

By the way, you can also write:

def initialize(number : MyEnum = :one)
end

which is more or less the same as typing:

def initialize(number = MyEnum::One)
end

(if the problem you are having is having to type a lot)

@RX14 RX14 closed this as completed Jun 29, 2018
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

3 participants