The compiler is giving "Invalid tuple expansion" when try to expand tuple parameter with a single value but when I use two or more tuple elements it works well.
Source Code:
This error does not compile.
module Main{
proc main(){
foo("select %s from table",("name"));
}
proc foo(str:string, params){
try{
writeln(str.format((...params)));//here is the problem
}catch{
writeln("Error");
}
}
}
but this one compiles well.
module Main{
proc main(){
foo("select %s, %s from table",("name","email"));
}
proc foo(str:string, params){
try{
writeln(str.format((...params)));
}catch{
writeln("Error");
}
}
}
chpl Version 1.17.0 pre-release (4b9d25676f)
gcc (Ubuntu 7.2.0-8ubuntu3) 7.2.0
CHPL_TARGET_PLATFORM: linux64
CHPL_TARGET_COMPILER: gnu
CHPL_TARGET_ARCH: native
CHPL_LOCALE_MODEL: flat
CHPL_COMM: none +
CHPL_TASKS: fifo +
CHPL_LAUNCHER: none
CHPL_TIMERS: generic
CHPL_UNWIND: none
CHPL_MEM: cstdlib +
CHPL_ATOMICS: intrinsics
CHPL_GMP: none +
CHPL_HWLOC: none
CHPL_REGEXP: re2 +
CHPL_AUX_FILESYS: none
This issue is related to :
("name") isn't actually a tuple, it's equivalent to just "name" without parenthesis. To make it into a tuple, you can add a trailing comma: ("name", ). This is defined in section 15.2 (Tuple Values) of the Chapel Spec. Adding the trailing comma to make it a tuple fixes your first example.
As an alternative to requiring one-tuples, you could provide two code paths within foo - one for tuples and one for non-tuples:
proc foo(str:string, params) {
try {
if isTuple(params) then
writeln(str.format((...params)));
else
writeln(str.format(params));
} catch {
writeln("Error");
}
}
Or provide two overloads of foo:
proc foo(str:string, params) where isTuple(params) {
try {
writeln(str.format((...params)));
} catch {
writeln("Error");
}
}
proc foo(str:string, params) where !isTuple(params) {
try {
writeln(str.format(params));
} catch {
writeln("Error");
}
}
Okay! That makes sense, but I would have never thought of it. This now works
use Postgres, Cdo;
const DB_HOST="localhost",
DB_USER="buddha",
DB_NAME="buddha",
DB_PWD="buddha";
var con = PgConnectionFactory(host=DB_HOST, user=DB_USER, database=DB_NAME, passwd=DB_PWD);
var w = "SELECT %s FROM r.cho_names";
var cur = con.cursor();
//cur.query(w, ("name"));
cur.query(w, ("name",));
for row in cur {
writeln(row);
}
So just adding the , is the workaround until and overload can be built, I suppose. Thanks!
Thanks for handling this, David! @marcoscleison, let us know if you agree this issue can be closed now. Thanks!
Yes, I think that all are clarified now.
Thank you very much.