Raoul Duke | 1 Mar 2008 01:51
Picon

Re: The Scourge of the Nullpointer

> My highly personal opinion is that it should be less code to make something
> notnull than to have something nullable.

lots of +1s there.

Windemuth Andreas | 1 Mar 2008 01:53
Picon
Favicon

Re: The Scourge of the Nullpointer


> Tony Morris wrote:
> 
> Scala's Option does indeed solve the problem, however, avoiding the 
> ability to assign to null would sacrifice the ability to target the JVM 
> and inter-operate well with existing Java libraries. Also, the fact that 
> you can assign to null here is a slightly different issue. Without the 
> objective of inter-operating with Java, I suspect null would go away 
> altogether and the existence of other languages without this objective 
> are a testament to this.
> 
> It is in these cases that you have the best possible solution (unlike 
> less powerful alternatives such as the C# proposal (MSR influence 
> bubbling down no doubt)) for representing null with types so as long as 
> you accept the objective of interoperating with Java, I suspect (having 
> not thought it through to my own satisfaction) there's not much more to do.
>
> This is just a long winded way of saying, what do you propose instead?

I thought I had made a reasonably clear proposal in the rest of my message:

Introduce a type qualifier ("some" or "notnull") that will exclude null as a
valid value. Disallow (at compile time) assignment of nullable types to notnull
types (notnull <: nullable), and disallow method dispatch on nullable
types. This would be entirely compatible with Java. Java types are all
nullable, which would require some null-checking at the interface, a good thing. 
Most Scala code would work with notnull types, except for those cases where
null makes sense, i.e. those were you would use the Option monad in languages
that do not have null. Think of nullable types as a built-in kind of monad.

(Continue reading)

Windemuth Andreas | 1 Mar 2008 02:19
Picon
Favicon

Re: The Scourge of the Nullpointer


Viktor Klang wrote:

> My highly personal opinion is that it should be less code to make
> something notnull than to have something nullable.
I agree with that, although either would do the trick. The benefit is so great
that the difference really doesn't matter much. The "notnull" is backwards compatible,
the "nullable" is not. For Java, it would certainly make sense to go with "notnull" to
preserve the codebase.

> nullval = nullable value
> null var = nullable variable
This would not work, I think, as nullability has to be associated with type, or
at least you need to be able to specify it for function parameters, return types,
and patterns as well as for value and variable declarations.

Perhaps a question mark as part of the type definition (?String, ?(x => x*x),
?Foo[T], etc.) would work. I don't know much about Nice, but isn't that how
it works there?

> My 2 dineros,
same here...

Andreas


Be a better friend, newshound, and know-it-all with Yahoo! Mobile. Try it now.
Jamie Webb | 1 Mar 2008 02:24

Re: scaladoc variation preview

On 2008-02-29 00:31:18 David Bernard wrote:
> Hi,
> 
> I deploy a preview of the scaladoc generated by "my scaladoc
> variation" : take a look at, and send me your opinions/feedback :
> 
> http://scala-tools.org/tmp/scaladocs-demo/
> http://scala-tools.org/tmp/scaladocs-lift/
> 
> Some of the features :
> * basic support for package.html and overview.html so you could write
> it
> * display package info in the overview page
> * supports colorized code sample (use <pre name="code"
> class="scala">....</pre>)
> * links with source code (colorized)
> * links between companion object and class
> * use ajax to show/hide info in left bar and in the main page
>    * inherited item are hidden by default, need to click on "show
> inherited"
>    * details can be show/hide by clicking on [details] (no need to
> scroll between summary and details)
> * sort item by name (include inherited)
> * use a unique list to display classes and object
> * list classes and object by short name, not by long name, because
> when I read a code in general I see the short name not the long
> * use tooltip/title to display fullname of a type (keep the mouse
> over a type 2s)

Looks pretty nice David! Though I agree with the others that the CSS
could do with a little work.

Something that's occurred to me a few times that you might want to
consider adding:

In Scala I find myself writing quite a lot of one-line methods. Often
the documentation comment is longer and less clear than the code, and
it looks pretty silly when reading the source. It would be nice to have
an annotation that just causes the source code of a particular member
to be copied verbatim into the documentation.

/J

David MacIver | 1 Mar 2008 02:39
Picon

Re: The Scourge of the Nullpointer

Honestly, I don't think this is an issue. I used to think it was, but
I've since changed my mind.

Basically the only places I get NPEs in pure Scala code is when
dealing with things like constructor initialization order,
accidentally using a val in a method before it's been assigned, etc
(and these tend to be things which always go wrong or never go wrong.
It's not a subtle error like a lot of NPEs in Java). These are not
things which your proposal will help with, because they're
"non-nullable" variables which can still be null if you use them at
the wrong time. I get it a little more when using Java APIs, but only
a little.

The reason NPEs are a plague in Java is because it encourages a style
of initialize everything as null and assign to it at some later date,
and it's very hard to keep track of exactly what might and might not
be null at a given time. Scala discourages this sort of ubiquitous
mutability. If you keep most things immutable and keep your use of
nulls to a minimum this problem gets downgraded to an annoyance. In
particular, a significantly smaller annoyance than having to decorate
anything that touchs Java code with nullable declarations and adding
yet another little language feature would be.

Regards,
David

James Iry | 1 Mar 2008 03:28
Picon
Gravatar

Re: The Scourge of the Nullpointer

On Fri, Feb 29, 2008 at 4:53 PM, Windemuth Andreas <windemut <at> yahoo.com> wrote:

I thought I had made a reasonably clear proposal in the rest of my message:

Sure.  See Nice and the IntelliJ IDE for nullability types.  Tony and I have talked about those before, so I know he's aware of the concept.

Introduce a type qualifier ("some" or "notnull") that will exclude null as a

I never want my Scala variables to be nullable except when working with Java libraries.  So it's the wrong default.  In Scala I want to use Option[T] when something's optional.

 Think of nullable types as a built-in kind of monad.

Except that they don't compose as nicely.   Read the comments posted after my article for a discussion on the trade-offs.

I do not see why you think the Option wrapper solves the problem. As I see it,

It doesn't entirely.  In fact, without rethinking the way constructors work there's probably no completely sound nullable type or option-type solution for Scala.  See this LtU comment and the paper it refers to for an explanation: http://lambda-the-ultimate.org/node/1478#comment-16993

It does the exact opposite, it turns the pesky "null or not"? question into a
"null or Some or None?" three-way question, which I cannot see as an improvement.

Yup, from a type systems perspective that's true.  In practice null is a problem when working with Java code and when dealing with constructor issues as mentioned in the linked comment.  Otherwise, good Scala coding practice is to avoid null like the scourge it is.

Windemuth Andreas | 1 Mar 2008 04:22
Picon
Favicon

Re: The Scourge of the Nullpointer


This all makes sense, sort of. The initializer problem you mention may not allow completely safe not-nulls for variables, but initializers are a special case and tricky with or without notnull. So, I still think that there has to be a mechanism to disallow null. Short of complete abolishment of null, a type qualifier would go almost all the way, at minimal cost.

Just the thought of being able to safely write

def engageMartian(m: some Martian) {
     m.getRayGun().getTarget(this).duck();
}

without worrying about NPEs makes my mouth water. Compiler checking is always better than self-discipline, which is why we want a strongly typed language in the first place.


Andreas


----- Original Message ----
From: James Iry <jamesiry <at> gmail.com>
To: Windemuth Andreas <windemut <at> yahoo.com>
Cc: scala <at> listes.epfl.ch
Sent: Friday, February 29, 2008 9:28:00 PM
Subject: Re: [scala] The Scourge of the Nullpointer

On Fri, Feb 29, 2008 at 4:53 PM, Windemuth Andreas <windemut <at> yahoo.com> wrote:
I thought I had made a reasonably clear proposal in the rest of my message:

Sure.  See Nice and the IntelliJ IDE for nullability types.  Tony and I have talked about those before, so I know he's aware of the concept.

Introduce a type qualifier ("some" or "notnull") that will exclude null as a

I never want my Scala variables to be nullable except when working with Java libraries.  So it's the wrong default.  In Scala I want to use Option[T] when something's optional.

 Think of nullable types as a built-in kind of monad.

Except that they don't compose as nicely.   Read the comments posted after my article for a discussion on the trade-offs.

I do not see why you think the Option wrapper solves the problem. As I see it,

It doesn't entirely.  In fact, without rethinking the way constructors work there's probably no completely sound nullable type or option-type solution for Scala.  See this LtU comment and the paper it refers to for an explanation: http://lambda-the-ultimate.org/node/1478#comment-16993

It does the exact opposite, it turns the pesky "null or not"? question into a
"null or Some or None?" three-way question, which I cannot see as an improvement.

Yup, from a type systems perspective that's true.  In practice null is a problem when working with Java code and when dealing with constructor issues as mentioned in the linked comment.  Otherwise, good Scala coding practice is to avoid null like the scourge it is.



Looking for last minute shopping deals? Find them fast with Yahoo! Search.
Paul Chiusano | 1 Mar 2008 04:27
Picon

Re: The Scourge of the Nullpointer

+1

I also haven't found this to be a problem.

If it weren't for Java interop, null could just be eliminated from the Scala language. The only time the word "null" should ever appear in Scala code is if you need to pass null to some legacy Java code that still uses null as a bastardized Option type. If you don't actively put "null" in your Scala code, you won't get NPEs, unless (*ahem*) you are doing something ugly like calling a polymorphic method in a base class constructor, which IMO you should never do!

Since it's so easy to make NPEs a non problem in Scala, I don't see why we'd need a language feature like what you are describing. You could literally have your build script scan all Scala files for the word null, and fail the build if it finds one! Compare this to Java, where even if you never type "null", if you forget to initialize a variable or field, its value is set to null implicitly. (Why the Java creators thought these semantics would be useful is beyond me.)

On Fri, Feb 29, 2008 at 8:39 PM, David MacIver <david.maciver <at> gmail.com> wrote:
Honestly, I don't think this is an issue. I used to think it was, but
I've since changed my mind.

Basically the only places I get NPEs in pure Scala code is when
dealing with things like constructor initialization order,
accidentally using a val in a method before it's been assigned, etc
(and these tend to be things which always go wrong or never go wrong.
It's not a subtle error like a lot of NPEs in Java). These are not
things which your proposal will help with, because they're
"non-nullable" variables which can still be null if you use them at
the wrong time. I get it a little more when using Java APIs, but only
a little.

The reason NPEs are a plague in Java is because it encourages a style
of initialize everything as null and assign to it at some later date,
and it's very hard to keep track of exactly what might and might not
be null at a given time. Scala discourages this sort of ubiquitous
mutability. If you keep most things immutable and keep your use of
nulls to a minimum this problem gets downgraded to an annoyance. In
particular, a significantly smaller annoyance than having to decorate
anything that touchs Java code with nullable declarations and adding
yet another little language feature would be.

Regards,
David

Imam Tashdid ul Alam | 1 Mar 2008 06:20
Picon
Favicon

Re: Scala native logging library using call-by-name's


Entertaining...

But I've come up with something slightly different that the log4j library. I
prefer recursive methods so sometimes I want my methods to recur :-/ and my
logs make much more sense if they are indented.

So I often do this:

def mult(that: Binary, level: Int) // this level is the level of recurrence

so that my (oversimplified) debug function looks like

def debug(msg: String, level: Int)
 = if(DEBUG) println(indent(level) + msg)

the problem is, my recursive methods tend to be named like "*" rather than
"mult". It's annoying to carry around the "level" parameter.

The solution I hit was...

class IntHolder(var value: Int)

and

def * (that: Binary)(implicit level: IntHolder) 

and I do this

level.value = level.value + 1
// could it be any clearer?
// note: my + and << methods use indented debug messages
val result = ((xL * yL) << (2 * m)) + (xL * yR + xR * yL) << m) + (xR * yR) 
level.value = level.value - 1

I know. Not elegant. But is working for me! 

--

-- 
View this message in context: http://www.nabble.com/-scala--Scala-native-logging-library-using-call-by-name%27s-tp15578551p15772687.html
Sent from the Scala mailing list archive at Nabble.com.

David Pollak | 1 Mar 2008 07:46
Gravatar

Should the Java Generic type <?> translate to C <: AnyRef

Because it seems that it translates to C <: Any


Gmane