Michael,
On Sat, 20 Jun 2020, Ferguson, Michael Paul Pratt (Chapel Developer) wrote:
I am after a value type which does not need 'new'
In Chapel, to create a record value while passing parameters to the
initializer, one uses `new MyRecord(args)`. Unlike in C++, the
difference between allocating on the stack or the heap (for a local
variable) is not the use of `new` but rather the use of a class or a
record.
Thanks for the insight. I am learning all the time.
Here is a version of your program that does that:
type a6 = [1..6] int;
record element
{
type T;
var data : T;
}
proc main
{
var x : [1..6] int = [ 1, 2, 3, 4, 5, 6 ];
var t = new element(x);
writeln(t);
}
Note that because `element` is a record, `new element(x)`
corresponds to what in C++ one would write as `element(x)`.
Thanks for the pointer!
Being behind the times with my version of the compiler, I found that with
the slightly older 1.20, I need to do it ever so slightly differently
record element
{
type T;
var data : T;
}
proc element.init=(type T, data : T)
{
this.T = T;
this.data = data;
}
proc main
{
var x : [1..6] int = [ 1, 2, 3, 4, 5, 6 ];
type xType = x.type;
var t = new element(xType, x);
writeln(t);
}
But, the error message I got from your version actually told me what the
signature of the init= method had to be. Once I followed its instruction,
it all worked for me. All good.
// this allows typed initialization from a value of type T
proc init=(other : [])
{
this.T = other.type;
this.data = other;
}
Shouldn't the comment be
// this allows typed initialization from an array of some base-type
where base-type is what Chapel calls an eltType?
I was also very pleasantly surprised (and very happy) that the following
worked. Actually,
record element
{
type T;
var n : int;
var data : [1..n] T;
}
proc element.init=(type T, n : int)
{
this.T = T;
this.n = n;
}
proc main
{
var x : [1..6] int = [ 1, 2, 3, 4, 5, 6 ];
var t = new element(int, 6);
t.data = x;
writeln(t);
}
It is what I really wanted to do in the first place but I was missing the
use of 'new' in my earlier experiments. This allows me to have a generic
record for a 'finite-element' where data contains the nodal connectivity
information, a vector which will be of different lengths depending on the
shape of the element (like tetrahedra, pentahedra and hexahedra in 3D) and
the type of function basis used throughout that shape (like linear or
quadratic polynomials).
When is 1.23 due? I was being lazy and planning to skip 1.21 and 1.22.
But if 1.23 is a way off, I will load up 1.22 ASAP.
Thanks - Damian
Pacific Engineering Systems International, 277-279 Broadway, Glebe NSW 2037
Ph:+61-2-8571-0847 .. Fx:+61-2-9692-9623 | unsolicited email not wanted here
Views & opinions here are mine and not those of any past or present employer
_______________________________________________
Chapel-developers mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/chapel-developers