[julia-users] Re: constants and containers
But this is not the same thing. AFAIK, this is not possible yet as a Base class. But I'm far from knowing everything. I've seen discussions about providing a way to inherit default methods from a field of a composite type but I can't find them. One problem of the method is type preservation. But whatever, you could still do that and set the methods appropriately. This would be tedious but should work: all you have to do is to not set a setindex! method for your new type. Or you may want instead to inherit from AbstractArray and define everything missing from there and again, set setindex! to something else for your new type. I've never done it, I don't know what it would require to make that working. While I would love to see immutability by default on variables which would avoid a whole class of bugs and inefficiencies for a modest cost (except maybe in the global scope for the REPL? or maybe that would solve the global scope efficiency problem?), I'm more sceptical about immutable arrays/dict. Not because it is not sensible, but because if someone is tempted to change one of your array values, nothing can prevent him to do so (in Julia and in many other languages). My take on this would be to go for the composite type and prefix the private things with private or _ or whatever and add a comment stating that this should never be accessed directly.
[julia-users] Re: constants and containers
On Sunday, September 14, 2014 6:04:20 PM UTC-4, Yakir Gagnon wrote: My original intention was to ask if there was any way we could declare a const array who's elements are also constants. Right now, the only way to do this is if you declare a new AbstractArray subtype, call it ReadOnlyArray, that has getindex but not setindex! methods.
[julia-users] Re: constants and containers
I may have missed something but wouldn't immutable t x y end immutable t x y end type u x y end work? julia myvar = t(1,2) julia myvar.x=5 ERROR: type t is immutable julia v = u(t(1,2), t(3,4)) u(t(1,2),t(3,4)) julia v.x t(1,2) v.x=t(5,6) t(5,6) v.x.x=42 ERROR: type t is immutable If you really want to guaranty constant fields, you have to type them to some constant type.
[julia-users] Re: constants and containers
My original intention was to ask if there was any way we could declare a const array who's elements are also constants. Since the following is possible: julia const a = [1,2,3] 3-element Array{Int64,1}: 1 2 3 julia a[1] = 2 2 and it would be useful to have arrays that are as constant as a variable can be, without the need of declaring a new immutable type. For instance, can we have an immutable Dict, who's fields AND their values are const? Const arrays would be nice though. On Monday, September 15, 2014 12:09:25 AM UTC+10, gael@gmail.com wrote: I may have missed something but wouldn't immutable t x y end immutable t x y end type u x y end work? julia myvar = t(1,2) julia myvar.x=5 ERROR: type t is immutable julia v = u(t(1,2), t(3,4)) u(t(1,2),t(3,4)) julia v.x t(1,2) v.x=t(5,6) t(5,6) v.x.x=42 ERROR: type t is immutable If you really want to guaranty constant fields, you have to type them to some constant type.