Emojicode Documentation 0.8
Guides Language Reference Package Index

References

The return of a method is normally immutable (which, of course, only concerns value types). However, the following code does work and it is very desirable that it do so:

馃弫馃崌
  馃崹馃崹 馃敜A馃敜 馃崋馃崋 鉃★笍 馃枍馃啎 array
  馃惢馃惤array 0鉂楋笍 馃敜B馃敜鉂楋笍

  馃榾 馃惤馃惤array 0鉂楋笍1鉂楋笍鉂楋笍
馃崏

We can observe that is possible to mutate the content of array by mutating the return of a method, which is, as we established before, not allowed in general.

The above code works because 馃崹鈥檚 馃惤 does not really return an Element but a 鉁达笍Element:

鉂楋笍 馃惤 index 馃敘 鉃★笍 鉁达笍Element

鉁达笍Element is a reference. A reference is like a pointer to a value type and allows you to mutate a value type in another object. Reference types can only be declared as return types. A returned reference is only mutable if the callee is mutable and mutating the reference indicates to the compiler that the callee whose method returned the reference was mutated.

References cannot be copied. Assigning a reference to a variable immediately causes the reference to be dereferenced and a copy to be stored into the variable. The following program will crash therefore:

馃弫馃崌
  馃崹馃崹 馃敜A馃敜 馃崋馃崋 鉃★笍 馃枍馃啎 array
  馃惤array 0鉂楋笍鉃★笍 馃枍馃啎 subarray
  馃惢subarray 馃敜B馃敜鉂楋笍

  馃榾 馃惤馃惤array 0鉂楋笍1鉂楋笍鉂楋笍
馃崏

Because references to value types could be an origin of severe memory bugs, Emojicode emposes strict rules that make their use very safe.

Obtaining A Reference

The only way to obtain a reference to a value type is by using the return statement inside a method whose return type is a reference. You can only safely return a reference to an instance variable, like in the example below:

馃晩 馃 馃崌
馃崏

馃晩 馃惌 馃崌
  馃枍馃啎 bacon 馃 猬咃笍 馃啎馃馃啎鉂楋笍

  馃啎馃崌馃崏

  鉂楋笍 馃嵆 鉃★笍 鉁达笍馃 馃崌
    鈫╋笍 bacon
  馃崏
馃崏

You cannot return a reference to value type in a local variable or to a temporary object, which clearly contradicts the purporse of references.

Forwarding References

Sometimes it is necessary to return a reference, that was returned from another function.

This cannot be considered a safe operation, though. Consider the following example that would lead to a crash if allowed:

馃晩 馃惌 馃崌
  馃枍馃啎 bacon 馃 猬咃笍 馃啎馃馃啎鉂楋笍

  馃啎馃崌馃崏

  鉂楋笍 馃嵆 鉃★笍 鉁达笍馃 馃崌
    鈫╋笍 bacon
  馃崏

  馃悋鉂楋笍 馃崉 鉃★笍 鉁达笍馃 馃崌
    鈫╋笍 馃嵆馃啎馃惌馃啎鉂楋笍鉂楋笍
  馃崏
馃崏

馃弫 馃崌
  馃崉馃晩馃惌鉂楋笍
馃崏

But there are cases where this can be done safely. In such a case you can wrap the return statement into an unsafe block, which allows the forwarding of mutable references. The example below is from the 馃崹 implementation.

鉂楋笍 馃惤 index 馃敘 鉃★笍 鉁达笍Element 馃崌
  馃挱 ...
  鈽o笍 馃崌
    鈫╋笍 馃惤 馃data鉂楋笍馃悮Element馃崋 index鉁栵笍鈿栵笍Element鉂楋笍
  馃崏
馃崏

馃鈥檚 馃惤 returns a reference too.鹿 Since we can be sure that the underlying storage of a list (returned by 馃data鉂楋笍) will not be released until the list itself is released, the above method is really safe.

鹿 Although 馃 is a value type, 馃馃惤 always returns a mutable reference, even if the callee is not mutable. All references returned by primitive value types are always mutable.

鈫 Previous Next Up: 鈥淎ppendix: The Emojicode Compiler鈥 鈫
Something not quite right? Improve this page