On Thu, Oct 15, 2015 at 8:53 AM, Roger Luo <rogerluo.r...@gmail.com> 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 <rogerluo.r...@gmail.com>:
>>
>> 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 <yyc1...@gmail.com>:
>>>
>>> I have some trouble understand your question.
>>>
>>> On Fri, Sep 25, 2015 at 8:22 AM, Roger Luo <rogerluo.r...@gmail.com>
>>> 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 <mauro...@runbox.com>:
>>> >>
>>> >> 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}
>>> >
>>> >
>>
>>
>

Reply via email to