Introduce types for forward compiler compatiblity

After Crystal 0.15, compiler will require declare the types used
by instance variables on classes.

This require changes to the usage of `Radix::Tree` by introducing
the type of payload elements it will handle:

    # Will only support symbols as payload
    tree = Radix::Tree(Symbol).new
    tree.add "/", :root

    # Error: cannot add node with anything other than Symbol
    tree.add "/meaning-of-life", 42

The changes ensure future compatibility with Crystal and also
enforces a more declarative usage of `Radix::Tree`.

If necessary, you can combine multiple types to ensure a tree
can contain all the wide range of payloads you need:

    tree = Radix::Tree.new(Foo | Bar | Symbol).new
    tree.add "/", :root
    tree.add "/foo", foo_instance

This change includes:

- Tree, Node and Result has been updated to require types.
- Node is capable of have optional payload (from defined type).
- Documentation has been updated to reflect this change.
This commit is contained in:
Luis Lavena 2016-04-14 20:46:12 -03:00
parent 18bf9b132e
commit 9003075ec7
8 changed files with 190 additions and 109 deletions

View file

@ -5,15 +5,15 @@ module Radix
describe "#found?" do
context "a new instance" do
it "returns false when no payload is associated" do
result = Result.new
result = Result(Nil).new
result.found?.should be_false
end
end
context "with a payload" do
it "returns true" do
node = Node.new("/", :root)
result = Result.new
node = Node(Symbol).new("/", :root)
result = Result(Symbol).new
result.use node
result.found?.should be_true
@ -24,15 +24,15 @@ module Radix
describe "#key" do
context "a new instance" do
it "returns an empty key" do
result = Result.new
result = Result(Nil).new
result.key.should eq("")
end
end
context "given one used node" do
it "returns the node key" do
node = Node.new("/", :root)
result = Result.new
node = Node(Symbol).new("/", :root)
result = Result(Symbol).new
result.use node
result.key.should eq("/")
@ -41,9 +41,9 @@ module Radix
context "using multiple nodes" do
it "combines the node keys" do
node1 = Node.new("/", :root)
node2 = Node.new("about", :about)
result = Result.new
node1 = Node(Symbol).new("/", :root)
node2 = Node(Symbol).new("about", :about)
result = Result(Symbol).new
result.use node1
result.use node2
@ -54,8 +54,8 @@ module Radix
describe "#use" do
it "uses the node payload" do
node = Node.new("/", :root)
result = Result.new
node = Node(Symbol).new("/", :root)
result = Result(Symbol).new
result.payload?.should be_falsey
result.use node
@ -64,8 +64,8 @@ module Radix
end
it "allow not to assign payload" do
node = Node.new("/", :root)
result = Result.new
node = Node(Symbol).new("/", :root)
result = Result(Symbol).new
result.payload?.should be_falsey
result.use node, payload: false