I have a special kind of variables, the value of which can be assigned only once. When assigning an object, a link to it is assigned.

But at the same time the state of the object can be changed. Constant object is suitable or not?

  • Just curious, in which PL you can change the properties of an object with a constant link? oO - niXman
  • The object is ordinary; you can link to it from your constant link. - yapycoder
  • For example in Ruby. Everything that begins with a capital letter is a constant: <pre>> class A> attr_accessor: moped> end => nil> AConst = A.new => # <A: 0xa4794d8>> AConst.moped = 1 => 1> AConst.moped = 2 => 2> AConst.moped => 2> AConst = A.new (irb): 9: warning: already initialized constant AConst => # <A: 0xa15a6f8> </ pre> - Vladimir Gordeev
  • Or, for example, in Java. May be also called final :) - yozh
  • @niXman on a constant link in any language can not change the properties of the object. That is the essence of the term "constant". It is for this reason that the author is looking for another term and raised this question here. - kirelagin

4 answers 4

In the theory of programming languages, this is called name binding , in other words, a comparison of a specific object with a name, as opposed to a variable, which is a slot in memory, where you can put the value of an object (if it is, say, a number), or a link to it .

If you just decided to create a container class that can be initialized with a certain value, but you cannot assign another, and decide how to name it, then you can use the term "cell" ( cell ), as is done in the Scala language:

scala> val container = new Cell(256) container: Cell[Int] = Cell(256) scala> container.elem res1: Int = 256 scala> container.elem = 257 <console>:6: error: reassignment to val container.elem = 257 ^ 

From the example it can be seen that the Cell container contains some element (elem), but it cannot be changed. At the same time, if, for example, an array is “put” while creating in Cell, then the elements of this array can legitimately be changed. You can also use the more colloquial name ImmutableCell or PersistentCell .

In the end, you can, without further ado, simply use the canonical term value , as opposed to variable. Scala authors do this (note the keywords: val (value) and var (variable)):

 scala> val x = 10 x: Int = 10 scala> var y = 10 y: Int = 10 scala> y = 20 y: Int = 20 scala> x = 20 <console>:6: error: reassignment to val x = 20 ^ 
  • Great answer, thanks. - Vladimir Gordeev

I would call immutable reference .

In my opinion, a very suitable term.

At least, this is a common terminology. It is quite natural and generally accepted that when they talk about const (for example, a link or a list), the state of internal objects cannot be changed, and if the collection is immutable , then it does not follow that the objects it contains are also immutable .

PS Oh, carefully read the answer, which is the longest. There, too, at the end offer immutable ... In my opinion, an excellent and reasonable option.

  • Looks good too. - Vladimir Gordeev

I would call ConstReference, once the link.

    And what is the area? What programming language? For example, in C ++ Const Reference will mean the inability to change the object:

     const Object& obj_ref = obj; 

    so we say that obj_ref is a link to obj, but this link will not allow you to change obj.

    If it is just a Reference, it will be assumed that the link has been assigned once, at the very beginning, but the object to which the link is going can be changed.

    So in C ++ terms, just Reference.

    • Programming language ruby. - Vladimir Gordeev
    • I solved this question for myself: I’ll just call constant. But as it is called correctly and generally accepted - while the question is open. - Vladimir Gordeev
    • It, by the way, is truly noted that in terms of C it will be just reference. The problem is that reference-oriented languages ​​(which are probably the majority, including Ruby) have a slightly different concept of reference. Hence the need to clarify the term. - kirelagin