如何指定Chapel中未知大小数组的返回值 [英] How to specify a return of an array of unknown size in Chapel

查看:131
本文介绍了如何指定Chapel中未知大小数组的返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图依靠类型推理来实现一个带签名的函数:

proc mode(data:[?] int)

,但编译器说它无法解析返回类型(这本身就是一个警告,我想只有两个返回语句)。我试过:

proc mode(data:[?] int):[?] int

但编译器然后说有一个内部错误:

内部错误:CAL0057 chpl版本1.13.1.518d486

什么是正确的方式来指定一个函数返回的数组的长度只能在运行时知道?

I tried to rely on type inference for a function with signature: proc mode(data: [?]int) but the compiler said it could not resolve the return type (which is a warning in in itself I guess given there are only two return statements). I tried: proc mode(data: [?]int): [?]int but the compiler then said there was an internal error: internal error: CAL0057 chpl Version 1.13.1.518d486 What is the correct way of specifying that the length of an array returned by a function can only be known at run time?

推荐答案

如果域/返回数组的大小不能在函数原型中直接描述,我相信你现在最好的选择是省略返回类型的任何描述,并依赖Chapel的类型推断机制来确定你返回的是一个数组(如你试图)。例如,下面是一个读取以前未知大小的数组并返回的过程:

If the domain/size of the array being returned cannot be described directly in the function prototype, I believe your best bet at present is to omit any description of the return type and lean on Chapel's type inference machinery to determine that you're returning an array (as you attempted). For instance, here is a procedure that reads in an array of previously unknown size and returns it:

proc readArrFromConsole() {
  var len = stdin.read(int);
  var X: [1..len] real;
  for x in X do
    x = stdin.read(real);

  return X;
}

var A = readArrFromConsole();
writeln(A);

运行它并在控制台输入以下内容:

Running it and typing this at the console:

3 1.2 3.4 5.6

生成:

1.2 3.4 5.6

你的问题提到了多个返回语句,这引发了一个问题:Chapel如何在不同的数组之间统一类型。一个简单的例子有多个相同类型的数组(每个数组都有一个唯一的域,大小和边界)似乎可以工作:

Your question mentions multiple return statements, which opens up the question about how aggressively Chapel unifies types across distinct arrays. A simple example with multiple arrays of the same type (each with a unique domain, size, and bounds) seems to work:

proc createArr() {
  var len = stdin.read(int);
  if (len > 0) {
    var X: [1..len] real;
    return X;
  } else {
    var Y: [-1..1] real;
    return Y;
  }
}

var A = createArr();
writeln(A);

要理解编译器无法解析示例中的返回类型的原因,可能需要更多关于什么包含您的程序主体/返回语句。

To understand why the compiler couldn't resolve the return type in your example may require more information about what your procedure body / return statements contained.

这篇关于如何指定Chapel中未知大小数组的返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆