• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Is there a way to disallow null for val parameter in constructor?

 
Ranch Hand
Posts: 239
12
Scala IntelliJ IDE Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'd like to create a class that takes a val parameter as a constructor but not allow it to be null.

How to do this?

With this code..


...you can still say myClass(null), and with this code...



..you get a reassignment to val error.

If you change it to a "var" it is no longer immutable.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can use require(), which is like an assert, to check if an argument satisfies an arbitrary condition. Ofcourse it's a runtime check, not a compile-time check.

If you want to automatically use empty string whenever null is passed, you could do something like this:

Note that in Scala, "if" is an expression, not a statement (as it is in Java) - it evaluates to a value.
 
Scott Shipp
Ranch Hand
Posts: 239
12
Scala IntelliJ IDE Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jesper, that helps a lot! Is "require" like "assert" in that it takes a special flag in the jvm to be set in order to be active?
 
Bartender
Posts: 2407
36
Scala Python Oracle Postgres Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why not just use default parameter values?

Also, try to avoid getting into situations where you have to check for nulls in the first place e.g. through the use of Options.
 
Scott Shipp
Ranch Hand
Posts: 239
12
Scala IntelliJ IDE Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Default parameter values was my first thought too but it does the opposite of what I am trying to do, which is that when someone specifically passes null in, it sets it to something else. I have found that default parameters are supplied when nothing is passed in at all, but won't overwrite null being passed in.
 
Scott Shipp
Ranch Hand
Posts: 239
12
Scala IntelliJ IDE Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes I really want to use Options, also case classes. But in this case I was doing a proof-of-concept of replacing one of our Java classes at work with an equivalent Scala class. So it had to snap-in right in place, unfortunately. The good news, though, is that after awhile of doing that, we will refactor the whole package to the Scala way. It's just that there are some small steps for awhile rather than one big bang integration.
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Scott Shipp wrote:Jesper, that helps a lot! Is "require" like "assert" in that it takes a special flag in the jvm to be set in order to be active?


I don't think so. The check is always done and there's no way to switch it off, just like Java asserts. If the condition is false, you'll get an IllegalArgumentException with the specified message.

About the use of null: I agree with Chris, in Scala you should really not use null at all - it really only exists for interoperability with Java. Use Option / Some / None instead.
 
reply
    Bookmark Topic Watch Topic
  • New Topic