Skip to content

Instantly share code, notes, and snippets.

@dacr
Last active April 2, 2023 10:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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/6e69157aadef333bee1bafd55a02d1e6119a18a4
// 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.1.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