There are two possible ways to implement return values from functions that are 'const': either the compiler checks that const-ness is maintained (as in C++), or the value has a read-only flag in it that is checked at run-time by the setindex! routine. The latter solution would entail a slowdown for setindex! across the board for all users. Since there already thousands of Julia users who would not like to see their existing codes slowed in order to support a new feature, I would say that the run-time flag approach can be ruled out.
This leaves the compile-time approach to verifying const-ness. But for this to work at compile time, the compiler needs to be able to trace const-ness throughout the program. In other words, if function f returns a 'const' array A, and then the user invokes function g with A as an argument, then the compiler needs to see that g declares A as a const argument. My point is that in order to preserve the const-ness of a returned value, it would be necessary to also extend the language with const arguments for functions. I don't see how to have one without the other. -- Steve Vavasis On Tuesday, October 27, 2015 at 4:52:21 AM UTC-4, Carlo Lucibello wrote: > > I found your proposal > > > https://groups.google.com/forum/#!searchin/julia-users/vavasis/julia-users/Hq00EyeazJM/HgYgDcnbhs4J > > https://groups.google.com/forum/#!searchin/julia-users/vavasis$20const/julia-users/FK_29Dj4eDo/nX5QnhfKc40J > > but that is a different feature, having cont `in` argument, while I want > const return types. > > Having const in would be nice, but i understand that its implementation > would be more difficult. > > On the other hand having a const return valure seems easier to my > (non-expert) eyes > > > > > Il giorno martedì 27 ottobre 2015 04:27:44 UTC+1, [email protected] ha > scritto: >> >> In 2014 when I first learned about Julia, I also suggested on this >> newsgroup that there should be a 'const' keyword as in C++ to annotate >> function arguments and return variables that are supposed to be read-only. >> Possibly you can find the old thread with google. I received a lot of >> feedback from experienced Julia users and core developers that convinced me >> that this is probably not a good idea. Here are some reasons that I can >> recall from the earlier discussion that adding 'const' to Julia may not be >> a good idea. >> >> (1) The 'const' keyword would make the multiple-dispatch system much more >> confusing because it would entail new rules about how the 'const' keyword >> affects closeness in the type hierarchy. >> >> not a problem for const return value > > >> (2) You can already get the desired effect in Julia by defining your own >> subtype of DenseArray in which getindex works as usual but setindex! throws >> an error. >> >> this solution is not general, it works only on arrays > >> (3) The promise that a routine won't change a 'const' argument could >> easily be defeated by aliasing (i.e., a function is invoked with a const >> argument, but another non-const argument refers to the same piece of data), >> so it may give the user a false sense of security. >> > not a problem for const return value > > -- Steve Vavasis >> >> >> >> >> >> >> >> On Monday, October 26, 2015 at 10:29:34 PM UTC-4, Carlo Lucibello wrote: >>> >>> It would be nice to annotate the return type of methods with a constant >>> qualifier, in order to have >>> an efficient and safe behaviour at the same time. >>> >>> I mean something like this: >>> >>> type A >>> data::Vector{Int} >>> end >>> >>> # invalid but desiderable julia code >>> const function getdata(a::A) >>> return a.data >>> end >>> >>> a = A(ones(10)) >>> data = getdata(a) >>> >>> data[1] = 2 # ERROR >>> a.data[1] = 2 # OK >>> >>> >>
