Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 635417
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T20:24:08+00:00 2026-05-13T20:24:08+00:00

trait NotNull {} I’ve been trying to see how this trait can guarantee that

  • 0
trait NotNull {}

I’ve been trying to see how this trait can guarantee that something is not null and I can’t figure it out:

def main(args: Array[String]) {
  val i = List(1, 2) 
  foo(i) //(*)
}

def foo(a: Any) = println(a.hashCode)

def foo(@NotNull a: Any) = println(a.hashCode) //compile error: trait NotNull is abstract

def foo(a: Any with NotNull) = println(a.hashCode) //compile error: type mismatch at (*)

And:

val i = new Object with NotNull //compile-error illegal inheritance

There is obviously some special compiler treatment going on because this compiles:

trait MyTrait {}

def main(args: Array[String]) {
  val i: MyTrait = null
  println(i)
}

Whereas this does not:

def main(args: Array[String]) {
  val i: NotNull = null //compile error: found Null(null) required NotNull
  println(i)
} 

EDIT: there’s nothing about this I can find in programming in Scala

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T20:24:09+00:00Added an answer on May 13, 2026 at 8:24 pm

    Try and error:

    scala> class A extends NotNull
    defined class A
    
    scala> val a : A = null
    <console>:5: error: type mismatch;
     found   : Null(null)
     required: A
           val a : A = null
                       ^
    
    scala> class B
    defined class B
    
    scala> val b : B = null
    b: B = null
    

    This works only with Scala 2.7.5:

    scala> new Object with NotNull
    res1: java.lang.Object with NotNull = $anon$1@39859
    
    scala> val i = new Object with NotNull
    i: java.lang.Object with NotNull = $anon$1@d39c9f
    

    And the Scala Language Reference:

    If that member has a type which
    conforms to scala.NotNull, the
    member’s valuemust be initialized to a
    value different from null, otherwise a
    scala.UnitializedError is thrown.

    For every class type T such that T <:
    scala.AnyRef and not T <:
    scala.NotNull one has scala.Null <: T.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to mixin the MultiMap trait with a HashMap like so: val children:MultiMap[Integer,
I'm looking for a an R package which can be used to train a
A DB Audit Trail captures the User Last Modified, Modified Date, and Created Date.
We have to train the client professionals on the source code for an application
I'm writing a report view of an audit trail, and I need to display
Is there a way to emulate mixins or traits in java? basically, I need

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.