Skip to content

Instantly share code, notes, and snippets.

@dacr
Last active May 25, 2024 10:20
Show Gist options
  • Save dacr/a1731565a4bdd9fe71e5d15600c6c747 to your computer and use it in GitHub Desktop.
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
// 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