In the other julia-users thread
<https://groups.google.com/d/msg/julia-users/GXH8UBFYd0U/lVzIAVXdxlYJ>
linked, Stefan claims "[val] is a local variable inside of the scope of the
type block, not a field of the [X] type. In particular, there's no way to
manipulate it from outside...".
Given that this is very easily worked around, I wonder if the devs would
consider adding some syntax to access that variable, so that if this use
case arises, we wouldn't have to pollute the constructor methods with extra
ones that don't actually create an object.
Something like X:val, i.e. colon(::Type{X}, ::Symbol) seems like a good
option to me.
On Tuesday, January 6, 2015 12:56:28 PM UTC-5, Jason wrote:
>
> Thanks for sharing Josh, I also did not know that internal constructors
> worked like that. And it *does* actually work as written with 0.3.
>
> On Tue Jan 06 2015 at 11:37:37 AM Josh Langsfeld <[email protected]
> <javascript:>> wrote:
>
>> On Friday, January 2, 2015 9:19:10 PM UTC-5, [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
>>>
>>
>> This is a very interesting example of the edges of the language. I didn't
>> know you could write constructors that didn't return new type objects.
>> Going for maximum terseness, all you need is:
>>
>> type X
>> _val::Int = 0
>> X() = _val
>> X(v::Int) = _val = v
>> end
>>
>> And now, X(10) sets the value, X() retrieves it, and an actual X object
>> can never be constructed. Pretty cool actually, though I don't know if it
>> would work in 0.3.
>>
>