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

Aliasing descendant class types allow to use class which is not aliased. #2881

Closed
schovi opened this issue Jun 20, 2016 · 2 comments
Closed

Comments

@schovi
Copy link
Contributor

schovi commented Jun 20, 2016

Hi,

I am new to Crystal and I am still in phase of playing against documentation. I (maybe) found following bug.

Having this code. One class with 3 descendants:

class Animal;end

class Dog < Animal;end
class Cat < Animal;end
class Bird < Animal;end

When I want to check type for Dog or Cat:

def check_animal(animal : Dog | Cat)
end

check_dog_or_cat(Dog.new)
check_dog_or_cat(Cat.new)
check_dog_or_cat(Bird.new)

Error is expected:

Error in ./src/main.cr:14: no overload matches 'check_animal' with type Bird
Overloads are:
 - check_dog_or_cat(animal : Dog | Cat)

check_dog_or_cat(Bird.new)
^~~~~~~~~~~~

But with aliasing Dog | Cat it allows to pass Bird too :S

alias DogOrCat = Dog | Cat

def check_alias(animal : DogOrCat)
end

check_alias(Dog.new)
check_alias(Cat.new)
check_alias(Bird.new)

EDIT: Version Crystal 0.18.2 (2016-06-17)

@jhass
Copy link
Member

jhass commented Jun 20, 2016

This is because it currently simplifies the union to the base class in this situation: https://carc.in/#/r/1230

Afaik the plan is to stop doing that, not sure if we already have a tracking issue for that, possibly #2665 or #2733

@asterite
Copy link
Member

#2661

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