JuliaRobotics / DistributedFactorGraphs.jl

Abstraction layer for spanning factor graphs over various technologies
Apache License 2.0
22 stars 2 forks source link

Dont use abstract return types #374

Closed dehann closed 2 days ago

dehann commented 4 years ago

Multiple dispatch is all about the calling types and not the return type. Furthermore, trying to force a Abstract return type does not perform inheritance, but rather trying to find the necessary converters. For example, this does not work:

   _       _ _(_)_     |  Documentation: https://docs.julialang.org
  (_)     | (_) (_)    |
   _ _   _| |_  __ _   |  Type "?" for help, "]?" for Pkg help.
  | | | | | | |/ _` |  |
  | | |_| | | | (_| |  |  Version 1.3.1 (2019-12-30)
 _/ |\__'_|_|_|\__'_|  |  Official https://julialang.org/ release
|__/                   |

julia> abstract type MyAbstract end

julia> struct MyType <: MyAbstract
         a::Int
         end

julia> function ff(x::Int)::T where {T <: MyAbstract}
         MyType(x)
       end
ff (generic function with 1 method)

julia> ff(1)
ERROR: UndefVarError: T not defined
Stacktrace:
 [1] ff(::Int64) at ./REPL[3]:2
 [2] top-level scope at REPL[4]:1

Also, by defining a return type, Julia simply calls the convert and an assert on the return type. It is not sensible to call convert(::Abstract, ::Hardtype) given a type stable function body.

cc @GearsAD , @Affie

Affie commented 4 years ago

xref julia docs on return types: https://docs.julialang.org/en/v1/manual/functions/#Return-type-1