Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Boolean Value from VM Parameter in Scala

Tags:

idioms

scala

How do I elegantly get a Boolean value from a VM parameter? My solution is not as easy as I

  1. have to test if it is null otherwise I will get IllegalArgumentException

  2. have to convert the entire String to upper or lower case and test if it is exactly "false" or "true" otherwise I will have the same exception thrown at my face.

i.e.

    val property = System.getProperty("some.property.name")
    property != null && property.toLowerCase == "true"

Is there another way out of this? Thanks.

like image 219
thlim Avatar asked Oct 24 '25 15:10

thlim


1 Answers

Scala has the sys.props object which wraps the Java system properties API, so you can deal with Option instead of null.

sys.props.get("property.name") exists (_ equalsIgnoreCase "true")
like image 71
Ben James Avatar answered Oct 26 '25 12:10

Ben James



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!