-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
added default option to get_prop #41
Conversation
I think it's a good idea, and it doesn't hurt users since it's just an additional method signature |
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## master #41 +/- ##
==========================================
+ Coverage 98.69% 98.71% +0.02%
==========================================
Files 5 5
Lines 382 388 +6
==========================================
+ Hits 377 383 +6
Misses 5 5 ☔ View full report in Codecov by Sentry. |
I added a check for existence of a vertex or edge and throw an julia> get_prop(mg, 2, 4, :testedgeprop)
ERROR: KeyError: key :testedgeprop not found
Stacktrace:
[1] getindex(h::Dict{Symbol, Any}, key::Symbol)
@ Base ./dict.jl:484
[2] get_prop
@ ~/.julia/dev/MetaGraphs/src/MetaGraphs.jl:277 [inlined]
[3] get_prop(g::MetaGraph{Int64, Float64}, u::Int64, v::Int64, prop::Symbol)
@ MetaGraphs ~/.julia/dev/MetaGraphs/src/MetaGraphs.jl:292
[4] top-level scope
@ REPL[50]:1
julia> get_prop(mg, 2, 4, :testedgeprop, "placeholder")
ERROR: ArgumentError: Edge 2 => 4 not in graph
Stacktrace:
[1] get_prop(g::MetaGraph{Int64, Float64}, e::Graphs.SimpleGraphs.SimpleEdge{Int64}, prop::Symbol, default::String)
@ MetaGraphs ~/.julia/dev/MetaGraphs/src/MetaGraphs.jl:288
[2] get_prop(g::MetaGraph{Int64, Float64}, u::Int64, v::Int64, prop::Symbol, default::String)
@ MetaGraphs ~/.julia/dev/MetaGraphs/src/MetaGraphs.jl:293
[3] top-level scope
@ REPL[51]:1 which throws a |
I think we could just use the |
you mean something like this get_prop(graph, edge, prop, default) = get(props(graph, edge), prop, default) ? |
Yeah but at least like you pointed out it's consistend with the rest of the implem |
Had to use the
has_prop(g, e, :prop) ? get_prop(g, e, :prop) : default_value
pattern way too much, found issue #37, and decided to fix this problem. Here is my very simple solution.Given that the default value has a type, we could maybe even do something like:
get_prop(g, e, prop, default::T)::T where T = has_prop(....) ? get_prop(....) : default
making it typestable, which could be nice.
On the other hand, this would exclude all the cases, where properties can have wildly different types from using this feature. Given that
MetaGraphsNext.jl
is the typestable successor to this package, it might be nice to not constrain the users in that way.Not sure though. What do you think?