Hi Craig,
You can use a queried type for the formal argument for 'A' to get the
behavior you're looking for. I'd move the definition for 'proc ='
outside of the record and define it as:
proc =(ref lhs: A(?T), rhs: T): void {
lhs.val = rhs;
}
The query expression is defined in section 10.8 of the language
specification:
https://chapel-lang.org/docs/1.17/_downloads/chapelLanguageSpec.pdf .
The reason Takeshi's where clause version hits an error when 'T' has a
default type is that when the type for 'T' is left off of the formal
argument it takes on its default type, in this case 'A(real)'.
So:
proc =(ref lhs: A, rhs): void where lhs.T == rhs.type ...
Is equivalent to:
proc =(ref lhs: A(real), rhs): void where lhs.T == rhs.type
Because of this, there is no '=' function that matches for the 'ai =
100;' line, so the assignment results in a type mismatch error.
Thanks,
David
On 6/12/18 5:13 PM, Takeshi Yamamoto wrote:
Hi Craig,
I'm also still learning Chapel so not veryn sure whether
this is the right way, but the following seems to work (with
chapel v1.16 on my computer):
record A {
type T;
var val : T;
}
proc =( ref lhs: A, rhs ): void where lhs.T == rhs.type {
lhs.val = rhs;
}
var ar: A( T = real );
ar = 1.23;
show( ar );
var ai: A( int );
ai = 100;
show( ai );
var astr: A( string );
astr = "hi";
show( astr );
proc show( arg: A ) {
writeln( "val = ", arg.val, " T = ", arg.T:string );
}
Results:
val = 1.23 T = real(64)
val = 100 T = int(64)
val = hi T = string
But if I add the default type to 'T' as
record A {
type T = real;
var val : T;
}
the program gives an error:
type mismatch in assignment from int(64) to A(int(64))
I don't know why this error occurs... (and hope other people will
give more hints :)
PS. I guess asking a question on Stackoverflow may also be useful,
because other people (not subscribing to this list) can see the Q/A also.
Best,
Takeshi
2018-06-13 5:24 GMT+09:00 Craig Reese <[email protected]
<mailto:[email protected]>>:
Chapel 1.17.1 newbie question (my real effort is much more
complicated, but this illustrates the issue):
record A {
type T = real;
var val : T;
proc =(ref lhs: A, rhs : T) : void {
lhs.val = rhs;
}
}
yields:
assign.chpl:4: warning: The left operand of '=' and '<op>='
should have 'ref' intent.
There are a couple work arounds:
1) put the "proc =" outside the record (but then the T type is no
longer visible).
2) 1+ move "type T" outside the "record" (though it really doesn't
otherwise need that wider visibility).
I've run into this in a number of larger cases. Is there some
magic to doing assignment as a member vs.
non-member function?
------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Chapel-users mailing list
[email protected]
<mailto:[email protected]>
https://lists.sourceforge.net/lists/listinfo/chapel-users
<https://lists.sourceforge.net/lists/listinfo/chapel-users>
------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Chapel-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/chapel-users
------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Chapel-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/chapel-users