[julia-users] Re: Supertypes in function arguments

2015-06-26 Thread Tim Wheeler
Hello Linus,

This is based on how array types are defined. In general, Vector{subtype} 
is not a subtype of Vector{supertype}.
Try this:

f{R:Real}(x::Array{R}) = x

-Tim

On Friday, June 26, 2015 at 8:38:12 AM UTC-7, Linus Härenstam-Nielsen wrote:

 I ran into a problem with types today that I don't know how to interpret.

 If I define a function according to 
 f(x::Real) = x
 it returns x as expected as long as the type of x is a subtype of Real. 
 However if I define
 f(x::Array{Real}) = x
 it throws MethodError no matter what I pass as argument. For example when 
 I try to pass it an Int array:
 f([1 2 3; 3 4 5])
 ERROR: MethodError: `f` has no method matching f(::Array{Int64,2})

 I would expect it to work like
 f{T:Real}(x::Array{T}) = x
 and accept any array of reals. Am I missing something?





[julia-users] Re: Supertypes in function arguments

2015-06-26 Thread Linus Härenstam-Nielsen
Ok, that makes sense. In that case what would be the best way to write a 
funcion that accepts several Real arrays (of not necessarily the same 
type)? Currently I'm using 

function f{T1,T2,T3 : Real}(x::Array{T1}, y::Array{T2}, z::Array{T3})
 ...
end

But that gets messy very quickly if there are many arguments. 

On Friday, June 26, 2015 at 6:00:25 PM UTC+2, Tim Wheeler wrote:

 Hello Linus,

 This is based on how array types are defined. In general, Vector{subtype} 
 is not a subtype of Vector{supertype}.
 Try this:

 f{R:Real}(x::Array{R}) = x

 -Tim

 On Friday, June 26, 2015 at 8:38:12 AM UTC-7, Linus Härenstam-Nielsen 
 wrote:

 I ran into a problem with types today that I don't know how to interpret.

 If I define a function according to 
 f(x::Real) = x
 it returns x as expected as long as the type of x is a subtype of Real. 
 However if I define
 f(x::Array{Real}) = x
 it throws MethodError no matter what I pass as argument. For example when 
 I try to pass it an Int array:
 f([1 2 3; 3 4 5])
 ERROR: MethodError: `f` has no method matching f(::Array{Int64,2})

 I would expect it to work like
 f{T:Real}(x::Array{T}) = x
 and accept any array of reals. Am I missing something?





[julia-users] Re: Supertypes in function arguments

2015-06-26 Thread Scott T
I've seen a discussion on this somewhere... here it is: 
https://github.com/JuliaLang/julia/issues/6984, and there's more discussion 
at https://groups.google.com/forum/#!topic/julia-users/alavN8tRdyI

No built-in solution so far from what I can see, although following the 
first post there, you could define a type alias for an array that has 
elements that are all the same Real subtype:

typealias RealArray{R:Real} Array{R}

function f(x::RealArray, y::RealArray, z::RealArray) ...


On Friday, 26 June 2015 17:46:22 UTC+1, Linus Härenstam-Nielsen wrote:

 Ok, that makes sense. In that case what would be the best way to write a 
 funcion that accepts several Real arrays (of not necessarily the same 
 type)? Currently I'm using 

 function f{T1,T2,T3 : Real}(x::Array{T1}, y::Array{T2}, z::Array{T3})
  ...
 end

 But that gets messy very quickly if there are many arguments. 

 On Friday, June 26, 2015 at 6:00:25 PM UTC+2, Tim Wheeler wrote:

 Hello Linus,

 This is based on how array types are defined. In general, Vector{subtype} 
 is not a subtype of Vector{supertype}.
 Try this:

 f{R:Real}(x::Array{R}) = x

 -Tim

 On Friday, June 26, 2015 at 8:38:12 AM UTC-7, Linus Härenstam-Nielsen 
 wrote:

 I ran into a problem with types today that I don't know how to interpret.

 If I define a function according to 
 f(x::Real) = x
 it returns x as expected as long as the type of x is a subtype of Real. 
 However if I define
 f(x::Array{Real}) = x
 it throws MethodError no matter what I pass as argument. For example 
 when I try to pass it an Int array:
 f([1 2 3; 3 4 5])
 ERROR: MethodError: `f` has no method matching f(::Array{Int64,2})

 I would expect it to work like
 f{T:Real}(x::Array{T}) = x
 and accept any array of reals. Am I missing something?