Hi Kenjiro --

I wanted to follow up on Sung's response a bit, as I remembered things a 
little differently than she did (or else interpreted your question 
differently).

It is possible in Chapel today to have an array field of a record or class 
alias some other array as long as the alias is established at its 
constructor's call time using => in the constructor argument list.  AFAIK, 
this requires using the keyword-/name-based means of passing arguments to
the constructor to identify the field by name.

For example, here's an example of this that works for me:

---

config const n = 10;

//
// create types with array fields
//
record R {
   var X: [1..n] real;
}

class C {
   var Y: [1..n] real;
}

//
// create an array
//
var A: [1..n] real = [i in 1..n] i;

//
// create objects whose fields alias A:
//
var myR = new R(X=>A);
var myC = new C(Y=>A);

//
// modify the fields of the objects
//
myR.X[1] += 0.1;
myC.Y[n] += 0.2;

//
// verify that writes to object fields have modified A
//
writeln("A is: ", A);

---


On this general topic, I also wanted to mention that I'm personally a 
little unhappy with the => operator due to how specific to arrays it is 
(and a little due to fear of its fragility in the implementation).  So 
thinking about ways to retire it, I wanted to mention as a sidebar that 
we're working on adding the ability to create 'ref' variables that support 
more general aliasing (not specific to arrays), similar to C++.

As in the array alias case, these aliases need to be established at 
initialization time and can't be changed thereafter. The ultimate keyword 
for this will be 'ref' but in the repository today, it is 'refvar' due to 
some parser ambiguities that we still need to resolve.

As an example, you can write:

---

var x = 1;
refvar y = x;

y = 2;

writeln("x is: ", x);  // prints 2

---

These aren't working correctly for record/class members yet, but once they 
are, you could imagine changing the declarations of X and Y in my code 
above to be 'ref[var]' fields as an alternate means of aliasing A in the 
members.  In the limit, this may be the means of retiring the use of => 
for array aliasing, if it turns out to be sufficient.  Or maybe I just 
need to get over my dislike of => ...  :)

Hope this is helpful to you,
-Brad



On Thu, 21 Aug 2014, Sung-Eun Choi wrote:

> Hi Kenjiro,
>
> As you noted, the alias operator (=>) does not work for fields of
> records or classes.  FWIW, this a known bug in our implementation, and
> not a limitation in the language.
>
> In the meantime, you might be able to get around this problem using a
> primitive that does the low level C assignment.  I'll send you some
> notes off list.
>
> -- Sung
>
> On Wed, Aug 20, 2014 at 01:05:08PM +0900, Kenjiro Taura wrote:
>>
>> Dear Chapel developers:
>>
>> Sorry if this is a frequently-asked question (I
>> lightly searched the web and ML archive).
>>
>> Q: how to have two records or classes point to the
>> same array?
>>
>> For example, a list whose cells all point to the
>> same array, like this:
>>
>> list
>>   +--+   +--+   +--+   +--+   +--+
>>   |  |-->|  |-->|  |-->|  |-->|  |
>>   +--+   +--+   +--+   +--+   +--+
>>    |      |      |      |      |
>>  +---------------------------------+
>>  |   array                         |
>>  +---------------------------------+
>>
>> Having an alias to an array always seems tricky in
>> Chapel, due to its copy assignment semantics (A =
>> B moves the content of B into A, not making A
>> share the same array with B). '=>' allows two
>> VARIABLES point to the same array, but the trick
>> cannot be applied to fields, as far as I know.
>>
>> Any help appreciated.
>>
>>
>>
>> ------------------------------------------------------------------------------
>> Slashdot TV.
>> Video for Nerds.  Stuff that matters.
>> http://tv.slashdot.org/
>> _______________________________________________
>> Chapel-developers mailing list
>> [email protected]
>> https://lists.sourceforge.net/lists/listinfo/chapel-developers
>
> ------------------------------------------------------------------------------
> Slashdot TV.
> Video for Nerds.  Stuff that matters.
> http://tv.slashdot.org/
> _______________________________________________
> Chapel-developers mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/chapel-developers
>

------------------------------------------------------------------------------
Slashdot TV.  
Video for Nerds.  Stuff that matters.
http://tv.slashdot.org/
_______________________________________________
Chapel-developers mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/chapel-developers

Reply via email to