Last active
April 5, 2018 12:28
-
-
Save miry/8f5ee6bb8d28dc2e8f5afbb67d2b3f6f to your computer and use it in GitHub Desktop.
How to convert type from `Node?` to `Node` ?
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
class Node | |
property value : String | |
property next : self | Nil | |
def initialize(@value : String | Nil) | |
end | |
def append(node : self) | |
last : self = self | |
while last.next | |
last = last.next | |
end | |
last.next = node | |
end | |
def values | |
result = [] of String? | |
current = self | |
while !current.nil? | |
result << current.value | |
current = current.next | |
end | |
result | |
end | |
end | |
first : Node = Node.new("first") | |
second : Node = Node.new("second") | |
first.append(second) | |
puts first.values |
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
Error in list/node.cr:30: instantiating 'Node#append(Node)' | |
first.append(second) | |
^~~~~~ | |
in list/node.cr:12: type must be Node, not (Node | Nil) | |
last = last.next | |
^~~~ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment