3

While experimenting with the online crystal compiler (which is awesome), I've run into an error which I can't seem to find an explanation for:

class Person
  class Current < self
  end

  class Destroyed < self
  end  
end

Error: Person+ is not a class, it's a Person+

If I change the code to

class Person
  class Current < Person
  end

  class Destroyed < Person
  end  
end

Then everything works fine.

If I just do

class Person
  class Current < self
  end      
end

Then everything works fine.

Questions

  1. What is a Class+ ( / Person+... I'm assuming Person is a class)
  2. It appears that being inherited is mutating the Person class in some way. What's going on here?
John
  • 9,249
  • 5
  • 44
  • 76
  • i'm guessing like many oop languages `self` (or `this`) refers to the instance. – Daniel A. White Dec 31 '17 at 20:56
  • @DanielA.White Hmm, so you're comment caused me to play around some more. It looks / sounds like a class inherits a type, rather than a class object? Maybe this is standard for typed languages, but I'm coming from a Ruby background and didn't realize this. Do you have any idea what the `Person+` message means? And why I'm able to inherit an object once? I re-read the Crystal Docs on types and inheritance. It doesn't seem like this is covered. The section on inheritance says that "a class inherits from another class" --> I interpret as inherits a class object (which, above, `self` is--I think). – John Dec 31 '17 at 21:21
  • 1
    The answer is simple: this is just a bug. Please report it on the crystal issue tracker. Inheriting a class does mutate the parent class in the compiler, and here that's causing this bug. This mutation should be invisible to the user, just an implementation detail. Here a bug in the compiler is exposing it to you by accident. – Stephie Dec 31 '17 at 21:28
  • @RX14 ah, cool! Will do. – John Dec 31 '17 at 21:30

1 Answers1

1

As @RX14 stated in a comment, turns out this is a bug. Issue opened here: https://github.com/crystal-lang/crystal/issues/5495

John
  • 9,249
  • 5
  • 44
  • 76