Last active
May 25, 2024 10:20
-
-
Save dacr/a1731565a4bdd9fe71e5d15600c6c747 to your computer and use it in GitHub Desktop.
partial function scala feature / published by https://github.com/dacr/code-examples-manager #dfb0a855-dab3-4ec8-bb54-cfbfb52e9687/5e730aec81b7b5af59a0a863071eabe1e9699695
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// summary : partial function scala feature | |
// keywords : scala, language-feature, partial-function, isDefinedAt, @testable | |
// publish : gist | |
// authors : David Crosson | |
// license : Apache NON-AI License Version 2.0 (https://raw.githubusercontent.com/non-ai-licenses/non-ai-licenses/main/NON-AI-APACHE2) | |
// id : dfb0a855-dab3-4ec8-bb54-cfbfb52e9687 | |
// created-on : 2022-06-15T20:54:56+02:00 | |
// managed-by : https://github.com/dacr/code-examples-manager | |
// run-with : scala-cli $file | |
// --------------------- | |
//> using scala "3.4.2" | |
// --------------------- | |
val pf:PartialFunction[Any, String] = | |
case _:Int => "OK" | |
case 1L => "ok" | |
assert(pf.isDefinedAt("42") == false) | |
assert(pf.isDefinedAt(42) == true) | |
println(pf.apply(42)) | |
println(pf(42)) | |
val pf2:Any=>String = // NOT EXACTLY A PARTIAL FUNCTION IN PF2 !!!! | |
case _:Int => "OK" | |
case 1L => "ok" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment