Skip to content

Instantly share code, notes, and snippets.

@JeffreySarnoff
Created February 1, 2018 01:52
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JeffreySarnoff/8143414d9110546b56fcfbe0ff0187ee to your computer and use it in GitHub Desktop.
Save JeffreySarnoff/8143414d9110546b56fcfbe0ff0187ee to your computer and use it in GitHub Desktop.
dispatching with NamedTuples
julia> nt_proto_a_names = (:first, :last)
(:first, :last)
julia> nt_proto_b_names = (:larger, :smaller)
(:larger, :smaller)
julia> nt_proto_a = NamedTuple{nt_proto_a_names}
NamedTuple{(:first, :last),T} where T<:Tuple
julia> nt_proto_b = NamedTuple{nt_proto_b_names}
NamedTuple{(:larger, :smaller),T} where T<:Tuple
julia> nt_values_a1 = (1, 9)
(1, 9)
julia> nt_values_a2 = (17, 98)
(17, 98)
julia> nt_a1 = nt_proto_a( nt_values_a1 )
(first = 1, last = 9)
julia> nt_a2 = nt_proto_a( nt_values_a2 )
(first = 17, last = 98)
julia> nt_values_b1 = ("abcdefg", "ab")
("abcdefg", "ab")
julia> nt_values_b2 = (:History, :yesterday)
(:History, :yesterday)
julia> nt_b1 = nt_proto_b( nt_values_b1 )
(larger = "abcdefg", smaller = "ab")
julia> nt_b2 = nt_proto_b( nt_values_b2 )
(larger = :History, smaller = :yesterday)
julia> test(::Type{nt_proto_a}) = "nt_proto_a"
test (generic function with 1 method)
julia> test(::Type{nt_proto_b}) = "nt_proto_b"
test (generic function with 2 methods)
julia> test(x::nt_proto_a) = string("nt_proto_a ", values(x))
test (generic function with 3 methods)
julia> test(x::nt_proto_b) = string("nt_proto_b ", values(x))
test (generic function with 4 methods)
julia> test(nt_proto_a)
"nt_proto_a"
julia> test(nt_proto_b)
"nt_proto_b"
julia> test(nt_a1)
"nt_proto_a (1, 9)"
julia> test(nt_a2)
"nt_proto_a (17, 98)"
julia> test(nt_b1)
"nt_proto_b (\"abcdefg\", \"ab\")"
julia> test(nt_b2)
"nt_proto_b (:History, :yesterday)"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment