dpjtn48868 2019-02-07 21:13
浏览 42
已采纳

如何声明必须返回其参数之一的函数的签名? (任何语言*)

How does one express the signature for a function that must return an argument (or this) that it receives (is called on), in TypeScript? Is there a programming language where this is possible?*

// In TypeScript (or consider it pseudo-code)
class C {
  // EXAMPLE 1 – Not polymorphic
  chainable(x): this                 // MUST not only return some C,
  {}                                 // but the same instance it was called on
}
// EXAMPLE 2
function mutate<T>(a: T[], x): T[]   // MUST return a, not a new Array
{
  /* So that this doesn't compile */ return Array.from(a);
  /* But this is OK */               return a;
}

Conversely, how about a function that must return a new instance?

// EXAMPLE 3
function slice<T>(a: T[], x, y): T[] // MUST return a new Array

❌TypeScript


Go 2?

Would the following contract achieve the above?

contract referentiallyIdentical(f F, p P) {
  f(p) == p
  v := *p
}
type returnsSameIntSlice(type T, *[]int referentiallyIdentical) T
func main() {
  var mutate returnsSameIntSlice = func(a *[]int) *[]int {
    b := []int{2}
    /* Would this compile? */ return &b
    /* This should */         return a
  }
}  

C++20?

Could the above be expressed as a C++ concept?


✅Scala


*Originally, the question was about doing this in TypeScript, but since that isn't possible, I am curious if it is in another language.

Feel free to remove a tag if that language's type system can't express this

  • 写回答

2条回答 默认 最新

  • duanbinmi8970 2019-02-07 22:40
    关注

    You can - in Scala.

    Class with a method returning this.type:

    class C {
      var x = 0 
    
      /** Sets `x` to new value `i`, returns the same instance. */
      def with_x(i: Int): this.type = {
        x = i
        this   // must be `this`, can't be arbitrary `C`
      } 
    }
    

    In-place sort that guarantees to return exactly the same array (doesn't really sort anything here):

    def sortInPlace[A: Ordered](arr: Array[A]): arr.type = {
      /* do fancy stuff with indices etc. */
      arr
    }
    

    If you attempt to return a different array,

    def badSortInPlace(arr: Array[Int]): arr.type = Array(1, 2, 3) // won't compile
    

    you'll get an error at compile time:

    error: type mismatch;
    found   : Array[Int]
    required: arr.type
          def badSortInPlace(arr: Array[Int]): arr.type = Array(1, 2, 3)
                                                               ^
    

    This is called a singleton type, and is explained in the spec.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 虚心请教几个问题,小生先有礼了
  • ¥30 截图中的mathematics程序转换成matlab