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 infer the type of instance variable '@name' of Person #3894

Closed
smellyshovel opened this issue Jan 14, 2017 · 3 comments
Closed

Can't infer the type of instance variable '@name' of Person #3894

smellyshovel opened this issue Jan 14, 2017 · 3 comments

Comments

@smellyshovel
Copy link

In section "Instance variables type inference" of Docs we can read the following:

Did you notice that in all of the previous examples we never said the types of a Person's @name and
@Age? This is because the compiler inferred them for us.

Then we see an example of what is said above:
`class Person
getter name

def initialize(@name)
@Age = 0
end
end

john = Person.new "John"
john.name #=> "John"
john.name.size #=> 4`

But, what's an irony, exactly about this code compiler says that it's invalid, saying

The type of a instance variable, if not declared explicitly with
@name : Type, is inferred from assignments to it across
the whole program.

The assignments must look like this:

  1. @name = 1 (or other literals), inferred to the literal's type
  2. @name = Type.new, type is inferred to be Type
  3. @name = Type.method, where method has a return type
    annotation, type is inferred from it
  4. @name = arg, with 'arg' being a method argument with a
    type restriction 'Type', type is inferred to be Type
  5. @name = arg, with 'arg' being a method argument with a
    default value, type is inferred using rules 1, 2 and 3 from it
  6. @name = uninitialized Type, type is inferred to be Type
  7. @name = LibSome.func, and LibSome is a lib, type
    is inferred from that fun.
  8. LibSome.func(out @name), and LibSome is a lib, type
    is inferred from that fun argument.

Other assignments have no effect on its type.

Can't infer the type of instance variable '@name' of Person

def initialize(@name)

So it seems like Docs say that compiler must infer the type of incoming value for an instance variable by itself, but in practice we see that compiler can't do this and we should exactly specify the type. Who's wronge? The compiler or the Docs?

@ysbaddaden
Copy link
Contributor

Outdated docs.

@smellyshovel
Copy link
Author

Should i read API instead? Aren't they outdated?

@mverzilli
Copy link

@smellyshovel read the book to understand Crystal and its philosophy, and read the API as reference material. You'll probably find outdated bits here and there, but they're the main sources of information about Crystal.

When you find something outdated, you can send PRs to this repo for API docs, and to https://github.com/crystal-lang/crystal-book for the book. Or, if you're not sure how to fix a problem, just open issues like this one.

I'm closing this in favor of crystal-lang/crystal-book#74

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