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 <jdla...@gmail.com> wrote: > On Friday, January 2, 2015 9:19:10 PM UTC-5, ele...@gmail.com 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. >