可以通过推理来定义函数类型吗? [英] Can function type be defined by inference?

查看:148
本文介绍了可以通过推理来定义函数类型吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Scala类型推断非常好,用起来很容易,不用再写两次。当你需要的时候,伤害越多。一个这样的例子是函数类型。



有时我想为某些函数签名创建一个命名类型。以某种方式可能吗?有什么方法可以获得函数的编译时类型,以便在定义 FType 时不必再次输入它?

  object Foo {
def f(a:Int,b:Int,x:Double,y:Double,name:String):Unit = {}

// type FType = typeOf(f)//编译器能否以某种方式为我提供编译时类型?
type FType =(Int,Int,Double,Double,String)=>单元

def callF(func:FType)= func(0,0,0,0,)
}

是否有类似于C ++ decltype 的内容Scala可以用于这个目的吗?

解决方案

我不太确定你想在这里实现什么,如果我理解你想要避免输入(a:Int,b:Int,x:Double,y:Double,name:String)两次。



如何自己定义 FType ,然后在 f 中简单重用,以及 callF

  object Foo {
type FType =( Int,Int,Double,Double,String)=> Unit

def f:FType =(a,b,x,y,name)=> ()

def callF(func:FType)= func(0,0,0,0,)
}

如果你真的想抽象 FType ,这是一个非常不同的问题,但它似乎并不你可以通过调用 func(0,0,0,0,)来强制这种类型。



您在Scala中没有 decltype ,因为类型不是头等公民,就像它们可以在Idris中一样。也就是说,您应该可以使用无形和/或宏编写它。

如果您想修复类型和参数并重新使用它们,最简单的解决方案是将它们转换为 case class 。您可以使用 import 直接访问您的字段:

  object Foo {
case class FArgs(a:Int,b:Int,x:Double,y:Double,name:String)

def f(args:FArgs):Unit = {
import args._
println(name)//或任何你想做的事

$ b def callF(func:FArgs => Unit)= func(FArgs (0,0,0,0,))
}


Scala type inference is really nice and it is easy to get used not to have to write things twice. The more it hurts when you have to. One such example are function types.

Sometimes I would like to create a named type for some function signature. Is it possible somehow? Is there some way to get the compile-time type of the function so that I do not have to type it again when defining FType?

object Foo {
  def f(a:Int, b:Int, x:Double, y:Double, name:String) : Unit = {}

  //type FType = typeOf(f) // can compiler provide me a compile time type somehow?
  type FType = (Int,Int,Double,Double,String) => Unit

  def callF( func:FType) = func(0,0,0,0,"")
}

Is there something like C++ decltype in Scala which could be used for this purpose?

解决方案

I'm not quite sure what you're trying to achieve here, if I understand correctly you want to avoid typing (a:Int, b:Int, x:Double, y:Double, name:String) two times.

What about defining FType yourself upfront and then simply reusing it in f and callF?

object Foo {
  type FType = (Int,Int,Double,Double,String) => Unit

  def f: FType = (a, b, x, y, name) => ()

  def callF(func: FType) = func(0,0,0,0,"")
}

If you actually want to abstract over FType, it's a significantly different problem, but it doesn't seem to be the case as you're forcing the type by calling func(0,0,0,0,"").

You don't have decltype in Scala because types are not first class citizens like they can be in Idris for example. That said, you should be able to write it using Shapeless and/or macros.

If you want to fix the types and the arguments and reuse them, the simplest solution is to turn them into a case class. You can then use import to access your fields directly:

object Foo {
  case class FArgs(a: Int, b: Int, x: Double, y: Double, name: String)

  def f(args: FArgs): Unit = {
    import args._
    println(name) // or whatever you want to do
  }

  def callF(func: FArgs => Unit) = func(FArgs(0,0,0,0,""))
}

这篇关于可以通过推理来定义函数类型吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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