That mirrors elextr's suggestion from shortly before your post.
If you want to implement your code directly, you can use the fact that a
type block already introduces a new variable scope:
type MyType
data
otherdata
_x = Any[]
MyType(d) = new(d,_x)
end
The link you gave also gives a good example of how to define mutable data
for a class-static function.
This works well if you're type doesn't have parameters. If you want to have
method data that does depend on the type parameters, you're probably better
off going with a Dict anyways.
On Fri Jan 02 2015 at 9:48:43 PM Jason <[email protected]> wrote:
> Turns out what I was remembering was something like this
> <https://github.com/JuliaLang/julia/blob/8e5d019f005659c5cadcd3a02651b05c7981e14d/base/combinatorics.jl#L358-L377>
> for
> static function variables rather than type variables. In trying to
> duplicate it I run into the fact that you can't declare Types inside of
> local scope:
>
> julia> let _x = Any[]
> global MyType
> type MyType
> data
> mystatic
> MyType(d) = new(d,_x)
> end
> end
> ERROR: error compiling anonymous: type definition not allowed inside a
> local scope
>
> On Fri Jan 02 2015 at 8:19:13 PM <[email protected]> wrote:
>
>> This seems to work, providing the equivalent to C++ class data members,
>> any better suggestions?
>>
>> immutable MyObj_a end
>>
>> type MyObj
>> MyObj() = new()
>>
>> a::Int=0
>> MyObj(::Type{MyObj_a}) = a
>> function MyObj(::Type{MyObj_a}, i::Int)
>> a = i
>> end
>> end
>>
>> # nicer wrappers
>>
>> get_a(::Type{MyObj}) = MyObj(MyObj_a)
>> set_a(::Type{MyObj}, i::Int) = MyObj(MyObj_a, i)
>>
>> # and an immutable for luck
>>
>> get_b(::Type{MyObj}) = 55
>>
>> Cheers
>> Lex
>>
>