Skip to content

Instantly share code, notes, and snippets.

@miry
Last active April 5, 2018 12:28
Show Gist options
  • Save miry/8f5ee6bb8d28dc2e8f5afbb67d2b3f6f to your computer and use it in GitHub Desktop.
Save miry/8f5ee6bb8d28dc2e8f5afbb67d2b3f6f to your computer and use it in GitHub Desktop.
How to convert type from `Node?` to `Node` ?
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
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