Skip to content
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

Dont use abstract return types #374

Closed
dehann opened this issue Apr 3, 2020 · 1 comment · Fixed by #1092
Closed

Dont use abstract return types #374

dehann opened this issue Apr 3, 2020 · 1 comment · Fixed by #1092
Assignees
Milestone

Comments

@dehann
Copy link
Member

dehann commented Apr 3, 2020

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

@dehann dehann added this to the mediumlist milestone Apr 3, 2020
@Affie
Copy link
Member

Affie commented Apr 6, 2020

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

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

Successfully merging a pull request may close this issue.

2 participants