- Learn Scala Programming
- Slava Schmidt
- 190字
- 2021-06-10 19:35:51
Extending functions
Nothing prevents a developer from extending a FunctionN trait the same way it is done with PartialFunction, though it seldom makes sense because of the limitations imposed by the referential transparency constraint. This means that such an implementation of the function should not have a shared state, nor should it mutate state.
It might be tempting, for example, to implement a loaner pattern as a function, so that a used resource would be automatically closed after function application, but it won't be referentially transparent and thus won't satisfy the requirements for a function.
Here is how the implementation could look:
class Loan[-T <: AutoCloseable, +R](app: T => R) extends (T => R) {
override def apply(t: T): R = try app(t) finally t.close()
}
And this is what happens if we call it:
scala> new Loan((_: java.io.BufferedReader).readLine())(Console.in)
res13: String = Hello
scala> [error] (run-main-0) java.io.IOException: Stream Closed
[error] java.io.IOException: Stream Closed
[error] at java.io.FileInputStream.read0(Native Method)
[error] at java.io.FileInputStream.read(FileInputStream.java:207)
[error] at jline.internal.NonBlockingInputStream.read(NonBlockingInputStream.java:245)
...
Unfortunately, it is not even possible to test whether the second call would produce the same result (obviously it will not) because we broke the REPL by closing the Console.in.
- Instant Testing with CasperJS
- 大學(xué)計(jì)算機(jī)基礎(chǔ)(第三版)
- Android項(xiàng)目開發(fā)入門教程
- Learning Spring 5.0
- Rake Task Management Essentials
- 數(shù)據(jù)結(jié)構(gòu)(C語言)
- MySQL數(shù)據(jù)庫管理與開發(fā)實(shí)踐教程 (清華電腦學(xué)堂)
- Learning OpenCV 3 Computer Vision with Python(Second Edition)
- Instant Debian:Build a Web Server
- Elasticsearch Essentials
- C編程技巧:117個(gè)問題解決方案示例
- Python Automation Cookbook
- Python深度學(xué)習(xí)(第2版)
- C# 7 and .NET Core 2.0 Blueprints
- Learning Rust