[英]type keyword in scala file upload exampe
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)
}
I have copied the above code from Play file upload example. 我已从播放文件上传示例复制了以上代码。 I am having a hard time with syntax of
type
keyword. 我很难用
type
关键字的语法。 If I say something like this type mytype = Int => String
. 如果我说这样的话,请
type mytype = Int => String
。 I can use it say like below 我可以像下面这样说
def method2(f:mytype) = "20"
def f(v:Int) = "hello"
method2(f)
But I based on whatever I understand, I am at total loss of how the following syntax is being used in the method handleFilePartAsFile
and what does it even mean? 但是基于我的理解,我完全不知道在
handleFilePartAsFile
方法中使用以下语法的方式,这甚至意味着什么?
type FilePartHandler[A] = FileInfo => Accumulator[ByteString, FilePart[A]]
The idea is exactly the same. 这个想法是完全一样的。 You just have a type parameter (like you've probably seen on classes and methods before) which can be substituted by any type, so eg
FilePartHandler[File]
is FileInfo => Accumulator[ByteString, FilePart[File]]
and you could write handleFilePartAsFile
as 您只需拥有一个可以用任何类型替换的类型参数(就像您之前在类和方法上可能看到的那样),例如
FilePartHandler[File]
为FileInfo => Accumulator[ByteString, FilePart[File]]
,您可以编写handleFilePartAsFile
为
def handleFilePartAsFile: FileInfo => Accumulator[ByteString, FilePart[File]] = { ...
You can think of type synonyms with parameters as functions from types to types. 您可以将带有参数的类型同义词视为类型之间的函数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.