Thanks a lot! On Wed, Jan 6, 2016 at 2:00 PM, Milan Bouchet-Valat <nalimi...@club.fr> wrote:
> Le mercredi 06 janvier 2016 à 13:48 +0100, Michael Krabbe Borregaard a > écrit : > > Interesting. I come from R, and one of the main advantages I feel > > Julia has is that I do not have to do any in-function testing of what > > types of arguments the user has passed into the function, generating > > error messages etc. In Julia, all of this is handled automatically if > > I specify a function argument. > Yes, that's a feature that I really appreciate too! When it is obvious > that one is expecting e.g. a Number or an AbstractString, there's no > reason not to restrict the type (both for correctness and > documentation). > > > I would think the ideal way of doing it would be to specify a > > function argument with a broad type (e.g. Number) that is meaningful > > for all the operations in the function, and prevents the user from > > e.g. passing in a string and getting a weird error message, or a bool > > and getting something spurious. Unfortunately, most of my functions > > do not take simple types, they take Vectors, and a Vector{Int} does > > not inherit from a Vector{Number}! - meaning that the whole idea of > > inheritance is a bit lost for me. I guess I can do foo{T <: > > Number}(a::Vector{T}) but that goes very far from the easy non > > -specifying format that you support. > > > > In short, I am really confused about how to use types well in Julia > > functions - and I did read the style guide. Can you guide me a bit > > more towards a good approach? > You're hitting a property called type invariance. See > http://docs.julialang.org/en/release-0.4/manual/types/#parametric-compo > site-types > > There's nothing wrong with writing foo{T<:Number}(a::Vector{T}). > That's the recommended way, though that it's a bit verbose (maybe a > shorter syntax will be added at some point). > > > Regards > > > > Thanks > > > > On Wed, Jan 6, 2016 at 1:27 PM, James Gilbert <jgrgilb...@gmail.com> > > wrote: > > > You'd think, reading this forum, that the preferred style is to > > > specify the type of function arguments. I think that's what feels > > > right for people with experience in other languages. Specifying > > > argument types doesn't necessarily improve speed, and harms the > > > reusability of code. > > > >