Skip to content

Explicit type annotation of MetaGraphs #95

Closed

Description

@lenianiva

If I have a function that returns a MetaGraph is it possible to annotate the type?

e.g.

const CustomGraph = MetaGraph(Int64, SimpleDiGraph(Int64), ...)

function create_graph()::CustomGraph
    out = CustomGraph(SimpleDiGraph())
    return out
end

This schema doesn't work, and it says "no method matching ... exists"

Activity

  1. gdalle commented on Oct 6, 2025

    @gdalle
    Member

    Output type annotations are seldom used in Julia, can I ask why you want to do this in the first place?

  2. gdalle commented on Oct 8, 2025

    @gdalle
    Member

    Also, could you provide a complete runnable example? Here I'm not sure whether CustomGraph is meant to be a type or an object?

  3. lenianiva commented on Oct 9, 2025

    @lenianiva
    Author

    Output type annotations are seldom used in Julia, can I ask why you want to do this in the first place?

    I want to pass values with the CustomGraph type into other functions. I'm only using the output type as an example. CustomGraph is meant to be a type.

  4. gdalle commented on Oct 9, 2025

    @gdalle
    Member

    Can you give a complete code example? Usually you would rather constrain the input types of other functions for dispatch purposes.

  5. lenianiva commented on Nov 23, 2025

    @lenianiva
    Author

    Can you give a complete code example? Usually you would rather constrain the input types of other functions for dispatch purposes.

    e.g.

    using Graphs, MetaGraphsNext
    
    const ColorGraph = MetaGraph
    
    function generate_colors(n::Int)::ColorGraph
        graph = Graph(Edge.([(1, 2), (1, 3), (2, 3)]))
        vertices_description = [:red => (255, 0, 0), :green => (0, 255, 0), :blue => (0, 0, 255)]
        edges_description = [
            (:red, :green) => :yellow, (:red, :blue) => :magenta, (:green, :blue) => :cyan
        ]
    
        return MetaGraph(graph, vertices_description, edges_description, "additive colors")
    end
    
    function consume_colors(colors::ColorGraph)
        @info "$(nv(colors))"
    end
    
    g = generate_colors(123)
    @info "colors: $(typeof(g))"
    consume_colors(g)

    Instead of having ColorGraph = MetaGraph, would it be possible to have ColorGraph = MetaGraph(Int64, ...), and thus ensure it is not possible to pass in an unrelated MetaGraph into consume_colors?

  6. gdalle commented on Nov 24, 2025

    @gdalle
    Member

    First of all, argument annotation and output annotation have different behaviors:

    • argument annotations prevent the method from accepting anything else
    • output annnotations will try to convert the result if it doesn't have the right type

    My take is that output annotations are rarely necessary when you constrain the arguments on downstream methods anyway. With that in mind, you could use the full type of g as a type annotation, but it is rather convoluted and there is one type parameter (the var... at the end) which is a MetaGraphsNext internal.

    julia> typeof(g)
    MetaGraph{
        Int64,  # Code
        SimpleGraph{Int64},  # Graph
        Symbol,  # Label
        Tuple{Int64, Int64, Int64},  # VertexData
        Symbol,  # EdgeData
        String,  # GraphData
        MetaGraphsNext.var"#MetaGraph##14#MetaGraph##15",  # WeightFunction
        Float64  # Weight
    }

    So if I had this problem, I would probably define

    const ColorGraph = MetaGraph{
        Int64,  # Code
        SimpleGraph{Int64},  # Graph
        Symbol,  # Label
        Tuple{Int64, Int64, Int64},  # VertexData
        Symbol,  # EdgeData
        String,  # GraphData
    }

    and skip the internal weights type parameter, especially if you don't use it.

  7. lenianiva commented on Nov 26, 2025

    @lenianiva
    Author

    First of all, argument annotation and output annotation have different behaviors:

    * argument annotations prevent the method from accepting anything else
    
    * output annnotations will try to convert the result if it doesn't have the right type
    

    My take is that output annotations are rarely necessary when you constrain the arguments on downstream methods anyway. With that in mind, you could use the full type of g as a type annotation, but it is rather convoluted and there is one type parameter (the var... at the end) which is a MetaGraphsNext internal.

    julia> typeof(g)
    MetaGraph{
    Int64, # Code
    SimpleGraph{Int64}, # Graph
    Symbol, # Label
    Tuple{Int64, Int64, Int64}, # VertexData
    Symbol, # EdgeData
    String, # GraphData
    MetaGraphsNext.var"#MetaGraph##14#MetaGraph##15", # WeightFunction
    Float64 # Weight
    }

    So if I had this problem, I would probably define

    const ColorGraph = MetaGraph{
    Int64, # Code
    SimpleGraph{Int64}, # Graph
    Symbol, # Label
    Tuple{Int64, Int64, Int64}, # VertexData
    Symbol, # EdgeData
    String, # GraphData
    }

    and skip the internal weights type parameter, especially if you don't use it.

    This solution is perfect! Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions