2017-09-06 94 views
0

type FilePartHandler[A] = FileInfo => Accumulator[ByteString, FilePart[A]]类型关键字

def handleFilePartAsFile: FilePartHandler[File] = { 
    case FileInfo(partName, filename, contentType) => 
    val perms = java.util.EnumSet.of(OWNER_READ, OWNER_WRITE) 
    val attr = PosixFilePermissions.asFileAttribute(perms) 
    val path = Files.createTempFile("multipartBody", "tempFile", attr) 
    val file = path.toFile 
    val fileSink = FileIO.toFile(file) 
    val accumulator = Accumulator(fileSink) 
    accumulator.map { case IOResult(count, status) => 
     FilePart(partName, filename, contentType, file) 
    }(play.api.libs.concurrent.Execution.defaultContext) 
} 

我抄从播放文件上面的代码上传例子。我很难用关键字type的语法。如果我说这样的话 type mytype = Int => String。我可以用它说像下面

def method2(f:mytype) = "20" 
def f(v:Int) = "hello" 
method2(f) 

但我基于什么我明白,我在如何以下语法在方法handleFilePartAsFile使用总损失?它甚至意味着什么呢?

type FilePartHandler[A] = FileInfo => Accumulator[ByteString, FilePart[A]] 

回答

1

这个想法完全一样。你只需要一个类型参数(就像你之前可能在类和方法中看到的那样),它可以被任何类型替换,例如, FilePartHandler[File]FileInfo => Accumulator[ByteString, FilePart[File]],你可以写handleFilePartAsFile作为

def handleFilePartAsFile: FileInfo => Accumulator[ByteString, FilePart[File]] = { ... 

可以用参数从类型到类型的函数想到类型的同义词。

+0

在飞机上英语是否意味着“handleFilePartsAsFile”是一种方法,它采用另一种方法(无论是命名还是匿名),并且该方法的输入是“FileInfo”,输出为Accumulator ...? 如果我上面写的是正确的,那么在本示例中,我们正在编写一个方法“handleFilePartAsFile”,该方法的块是满足上述条件的匿名函数? – curiousengineer

+0

不,这是一个函数_returns_(不是方法),并且该函数的输入是'FileInfo'。和'{case FileInfo(...)=> ...}'定义了这样一个函数。 –

+0

是的,我写错了,所以详细阐述了你写的内容,''handleFilePartAsFile''是一种方法。这个handleFilePartsAsFile返回一个函数。这是将FileInfo作为输入并将Accumulator作为输出返回的函数? – curiousengineer