Skip to content

Instantly share code, notes, and snippets.

@bitmage
Created September 28, 2016 04:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bitmage/8cfb434625e0b66b34c8ceb2999428d4 to your computer and use it in GitHub Desktop.
Save bitmage/8cfb434625e0b66b34c8ceb2999428d4 to your computer and use it in GitHub Desktop.
enumeration example
import Base.==
import Base.convert
sources = Set([:given, :self, :old, :neighbor])
immutable Source
s
Source(s) = in(s, sources) ? new(s) : error("Invalid source: $(s). Valid options are: $(sources)")
end
==(l::Source, r::Source) = l.s == r.s
convert(::Source, s::Symbol) = Source(s)
convert(Source, :given)
# => MethodError: Cannot `convert` an object of type Symbol to an object of type Source
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment