Thanks!
在 2015年10月15日星期四 UTC+8下午10:13:08,Yichao Yu写道: > > On Thu, Oct 15, 2015 at 10:05 AM, Roger Luo <[email protected] > <javascript:>> wrote: > > But what if I want to restrict the number of a array I use? > > if the type is like > > > > type particle > > pos > > end > > > > and I want to restrict the pos's type to be an Array but the dimension > > should be 1,but do not restrict the Type of the Array > > or is there any other type can act like a vector but not the Array type? > > AbstractVector > > > > > > 在 2015年10月15日星期四 UTC+8下午9:06:32,Yichao Yu写道: > >> > >> On Thu, Oct 15, 2015 at 8:53 AM, Roger Luo <[email protected]> > wrote: > >> > I just start to write the program so I may not have any code to post. > >> > I want to define a position of a particle like > >> > > >> > type particle{T} > >> > pos::T > >> > end > >> > > >> > but I hope the type of pos can be limited rather than the supertype > any > >> > > >> > 2015-10-15 20:51 GMT+08:00 Roger Luo <[email protected]>: > >> >> > >> >> It's like what if I want to define a 3-dimension linear space R^3 as > an > >> >> abstract type and the Array{Float,3} is a subtype-like type of the > >> >> abstract > >> >> type 3-dimension linear space? > >> >> > >> >> I want a linear space as abstract type and some subtypes that can do > >> >> the > >> >> linear algebra. > >> > >> You can define your abstract type (e.g. R^3 Vector) and also define > >> your custom concrete type (e.g. R^3 Vector for Float64) that is a > >> subtype of it. You cannot change an existing type (like > >> Array{Float64,3}) to be a subtype of any abstract type you define. > >> (Also note that Float is not a type and Array{Float64,3} is a > >> 3-dimensional array and is not a vector in R^3). > >> > >> To restrict a type parameter to your type, simply use, > >> > >> type partical{T<:R3Vector} > >> pos::T > >> end > >> > >> >> > >> >> 2015-09-25 20:36 GMT+08:00 Yichao Yu <[email protected]>: > >> >>> > >> >>> I have some trouble understand your question. > >> >>> > >> >>> On Fri, Sep 25, 2015 at 8:22 AM, Roger Luo <[email protected]> > >> >>> wrote: > >> >>> > Thanks,I've read about the arrays,but just don't know how to > define > >> >>> > some > >> >>> > existed type. > >> >>> > >> >>> You cannot redefine a type. Or do you mean how are certain existed > >> >>> types implemented? For that you can just check out the source code. > >> >>> > >> >>> > though a type may be defined as: > >> >>> > type a{T<:Integer}<:Real > >> >>> > xxx > >> >>> > end > >> >>> > > >> >>> > but that's will work in my function.I tried to pass different > types > >> >>> > into a > >> >>> > function, so I asked this question. > >> >>> > still do not understand why can define a existed type become a > >> >>> > subtype > >> >>> > of > >> >>> > some other type? > >> >>> > >> >>> You cannot change the type inheritance of an existing type, period. > As > >> >>> Mauro pointed out, you are probably looking for something else. It > >> >>> would be better if you can post the original problem you are trying > to > >> >>> solve. > >> >>> > >> >>> > > >> >>> > 2015-09-25 15:11 GMT+08:00 Mauro <[email protected]>: > >> >>> >> > >> >>> >> Julia's parametric types are invariant (except tuples). To > figure > >> >>> >> out > >> >>> >> what that means have a read through > >> >>> >> > >> >>> >> > >> >>> >> > >> >>> >> > http://docs.julialang.org/en/release-0.4/manual/types/#parametric-composite-types > > >> >>> >> > >> >>> >> > How to make Array{BigFloat,1}<:Array{Real,1}? > >> >>> >> > >> >>> >> This is always false (that is invariance). But presumably you > want > >> >>> >> a > >> >>> >> method which takes any Array with eltype<:Real, try something > like: > >> >>> >> > >> >>> >> julia> f{T<:Real}(ar::Array{T,1}) = typeof(ar) > >> >>> >> f (generic function with 1 method) > >> >>> >> > >> >>> >> julia> f(Array(BigFloat, 2)) > >> >>> >> Array{BigFloat,1} > >> >>> > > >> >>> > > >> >> > >> >> > >> > >
