Last active
November 20, 2017 17:37
-
-
Save RyanScottLewis/db5a9d9401f73c5fd4446936634ab1bb to your computer and use it in GitHub Desktop.
Error when trying to have a parent class initialize a child class using `self`
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Code | |
abstract class Interface | |
def initialize(@application : Application) | |
end | |
abstract def call | |
end | |
class Interface::CommandLine < Interface | |
def call | |
puts "Running application through the command-line interface." if @application.verbose | |
end | |
end | |
class Interface::Graphical < Interface | |
def call | |
puts "Running application through the graphical interface." if @application.verbose | |
end | |
end | |
class Application | |
def self.call(interface_class) | |
new(interface_class).call | |
end | |
def initialize(interface_class : Interface.class) | |
@interface = interface_class.new(self) | |
end | |
getter verbose : Bool = true | |
getter! interface : Interface | |
delegate call, to: @interface | |
end | |
Application.call(Interface::CommandLine) | |
# Error: | |
# | |
# Error in examples/app_problem.cr:45: instantiating 'Application:Class#call(Interface::CommandLine:Class)' | |
# | |
# Application.call(Interface::CommandLine) | |
# ^~~~ | |
# | |
# in examples/app_problem.cr:31: instantiating 'Application#call()' | |
# | |
# new(interface_class).call | |
# ^~~~ | |
# | |
# in examples/app_problem.cr:41: expanding macro | |
# | |
# delegate call, to: @interface | |
# ^ | |
# | |
# in macro 'delegate' /usr/local/Cellar/crystal-lang/0.23.1_3/src/object.cr:1067, line 3: | |
# | |
# 1. | |
# 2. def call(*args, **options) | |
# > 3. @interface.call(*args, **options) | |
# 4. end | |
# 5. | |
# 6. def call(*args, **options) | |
# 7. @interface.call(*args, **options) do |*yield_args| | |
# 8. yield *yield_args | |
# 9. end | |
# 10. end | |
# 11. | |
# 12. | |
# | |
# undefined method 'call' for Nil (compile-time type is (Interface | Nil)) | |
# Error: 'self' was used before initializing instance variable '@interface', rendering it nilable | |
# | |
# Rerun with --error-trace to show a complete error trace. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment