Account Options

  1. Anmelden
Das alte Google Groups wird demnächst nicht mehr unterstützt. Die neue Version ist jedoch nicht kompatibel mit Ihrem Browser.
Google Groups-Startseite
« Google Groups-Startseite
Beginner question about "x match { case None => return; case Some( y ) => y.foo() }"
Gegenwärtig gibt es mehrere Themen in dieser Gruppe, die zuerst angezeigt werden sollen. Damit dieses Thema zuerst angezeigt werden kann, muss diese Option bei einem anderen Thema entfernt werden.
Bei der Bearbeitung Ihrer Anfrage ist ein Fehler aufgetreten. Versuchen Sie es erneut.
Kennzeichnen
  24 Nachrichten - Alle ausblenden  -  Alles übersetzen in die Sprache: Übersetzt (alle Originale anzeigen)
Bei der Gruppe, für die Sie eine Mitteilung verfassen, handelt es sich um eine Usenet-Gruppe. Wenn Sie in dieser Gruppe Nachrichten posten, ist Ihre E-Mail-Adresse für jeden im Internet sichtbar
Ihre Antwort wurde nicht gesendet.
Die Nachricht wurde übermittelt.
 
Von:
An:
Cc:
Nachtrag zu:
Cc hinzufügen | Nachtrag hinzufügen zu | Betreff bearbeiten
Betreff:
Bestätigung:
Geben Sie zur Bestätigung die im folgenden Bild angezeigten Zeichen ein bzw. die Zahlen, die durchgesagt werden, wenn Sie auf das Barrierefreiheitssymbol klicken. Hören Sie zu und geben Sie die gehörten Zahlen ein
 
Saxo  
Profil anzeigen   Übersetzen in die Sprache: Übersetzt (Original anzeigen)
 Weitere Optionen 15 Nov. 2012, 10:23
Von: Saxo <jeti...@web.de>
Datum: Thu, 15 Nov 2012 07:23:35 -0800 (PST)
Lokal: Do 15 Nov. 2012 10:23
Betreff: Beginner question about "x match { case None => return; case Some( y ) => y.foo() }"

Hi,

I'm actually not into Scala. Maybe just not yet ;-). I saw a conversion in
this thread on the Kotlin homepage:

http://confluence.jetbrains.net/display/Kotlin/Comparison+to+Scala

It is about writing

1. x match { case None => return; case Some( y ) => y.foo() }

or

2. return x.map( _.foo )

instead of just

3.

if (x == null)

>   return
> x.foo()

This appeared a little weird to me. My question is whether chosing a
solution as in 1. or 2. is normal Scala way of doing things or whether the
position of that Scala developer in that thread is not the the rule. I'm
just wondering. Maybe I'm not getting something. I believe some Scala
developers would say "Scala is multi-paradigm and I prefer 3. Period.". But
I'm curious here what some typical opinions are about this.

Regards, Oliver


 
Sie müssen sich anmelden, bevor Sie Nachrichten veröffentlichen können.
Bevor Sie eine Nachricht posten können, müssen Sie zunächst dieser Gruppe beitreten.
Bitte aktualisieren Sie vor dem Posten in den Abonnementeinstellungen Ihren Spitznamen.
Sie haben nicht die erforderliche Berechtigung zum Posten.
Ionuț G. Stan  
Profil anzeigen   Übersetzen in die Sprache: Übersetzt (Original anzeigen)
 Weitere Optionen 15 Nov. 2012, 10:41
Von: "Ionuț G. Stan" <ionut.g.s...@gmail.com>
Datum: Thu, 15 Nov 2012 17:41:08 +0200
Lokal: Do 15 Nov. 2012 10:41
Betreff: Re: [scala-user] Beginner question about "x match { case None => return; case Some( y ) => y.foo() }"
On 15/Nov/2012 17:23, Saxo wrote:

For this particular example I'd go with another comment from that thread:

   x.foreach(_.foo())

If you're returning nothing in the truthy branch of the third example,
it's obvious that x.foo() is a method called for its side-effects. So if
you only want some side-effects when something isn't null/None, then
foreach would be best. You can of course rewrite it as:

if (x != null) x.foo()

// or with Option

if (x.isDefined) x.get.foo()

I think any of these, or the `foreach` example is ok. I'd choose the
explicit null check if I were to work with a Java library and the null
comes from realms I don't have control over. This way I won't have to
wrap a value inside an Option for such a short period of time (i.e.
until I call `foo` on the wrapped value).

I'd definitely go with an Option when I want the API to signal a missing
value.

So, the least probable solution would be the one with explicit pattern
matching because Option already provides methods for most of the real
world patterns of usage.

--
Ionuț G. Stan  |  http://igstan.ro


 
Sie müssen sich anmelden, bevor Sie Nachrichten veröffentlichen können.
Bevor Sie eine Nachricht posten können, müssen Sie zunächst dieser Gruppe beitreten.
Bitte aktualisieren Sie vor dem Posten in den Abonnementeinstellungen Ihren Spitznamen.
Sie haben nicht die erforderliche Berechtigung zum Posten.
Saxo  
Profil anzeigen   Übersetzen in die Sprache: Übersetzt (Original anzeigen)
 Weitere Optionen 15 Nov. 2012, 11:36
Von: Saxo <jeti...@web.de>
Datum: Thu, 15 Nov 2012 08:36:19 -0800 (PST)
Lokal: Do 15 Nov. 2012 11:36
Betreff: Re: [scala-user] Beginner question about "x match { case None => return; case Some( y ) => y.foo() }"

Man, that is tough ... You just don't allow null to enter FP. I see, I
see... So Scala is really FP with classes to organize your functional code.
Imperative programming is eschewed in Scala. Have to mull this over for a
while... ;-).

Thanks for the answer,
Oliver

Am Donnerstag, 15. November 2012 16:41:17 UTC+1 schrieb Ionuț G. Stan:


 
Sie müssen sich anmelden, bevor Sie Nachrichten veröffentlichen können.
Bevor Sie eine Nachricht posten können, müssen Sie zunächst dieser Gruppe beitreten.
Bitte aktualisieren Sie vor dem Posten in den Abonnementeinstellungen Ihren Spitznamen.
Sie haben nicht die erforderliche Berechtigung zum Posten.
Matthew Pocock  
Profil anzeigen   Übersetzen in die Sprache: Übersetzt (Original anzeigen)
 Weitere Optionen 15 Nov. 2012, 12:49
Von: Matthew Pocock <turingatemyhams...@gmail.com>
Datum: Thu, 15 Nov 2012 17:49:15 +0000
Lokal: Do 15 Nov. 2012 12:49
Betreff: Re: [scala-user] Beginner question about "x match { case None => return; case Some( y ) => y.foo() }"

On 15 November 2012 16:36, Saxo <jeti...@web.de> wrote:

> Man, that is tough ... You just don't allow null to enter FP.

Well, it's more that you shouldn't allow null to enter any programming.
They suck.

I see, I see... So Scala is really FP with classes to organize your

> functional code.

In FP, we can better represent optional or missing values using a type like
Option. This type fits into a series of general design patterns that
generalises applying functions to values in containers (map, flatMap,
foreach, filter).

> Imperative programming is eschewed in Scala. Have to mull this over for a
> while... ;-).

I'm not sure that functional/imperative is exactly the split, but I
personally do try to structure my code so that I avoid side-effects as far
as possible (things that return void/unit), use immutable values everywhere
I can, and as far as sanity allows, use types to describe/enforce the
desired behaviour.

If you take:

val x: Option[String]

This documents that x will contain an optional value that is a String. If
you wanted to safely put that string into upper case, you can say:

val X = x map _.toUpper

If x was a String, then we'd say

val X = x.toUpper

So, there's minimal overhead to working with the raw String type and the
optional String type when you are editing the code, and you've made it
explicit that this value may be present or absent.

There are, as you've noticed, lots of ways to do this kind of job. Pick the
one that you find easiest to read and write.

Matthew

--
Dr Matthew Pocock
Integrative Bioinformatics Group, School of Computing Science, Newcastle
University
mailto: turingatemyhams...@gmail.com
mailto: matthew.poc...@ncl.ac.uk
gchat: turingatemyhams...@gmail.com
msn: matthew_poc...@yahoo.co.uk
irc.freenode.net: drdozer
skype: matthew.pocock
tel: (0191) 2566550
mob: +447535664143

 
Sie müssen sich anmelden, bevor Sie Nachrichten veröffentlichen können.
Bevor Sie eine Nachricht posten können, müssen Sie zunächst dieser Gruppe beitreten.
Bitte aktualisieren Sie vor dem Posten in den Abonnementeinstellungen Ihren Spitznamen.
Sie haben nicht die erforderliche Berechtigung zum Posten.
Saxo  
Profil anzeigen   Übersetzen in die Sprache: Übersetzt (Original anzeigen)
 Weitere Optionen 15 Nov. 2012, 13:23
Von: Saxo <jeti...@web.de>
Datum: Thu, 15 Nov 2012 10:23:29 -0800 (PST)
Lokal: Do 15 Nov. 2012 13:23
Betreff: Re: [scala-user] Beginner question about "x match { case None => return; case Some( y ) => y.foo() }"

> If x was a String, then we'd say
> val X = x.toUpper

I see. Of course,  of course ...

There are, as you've noticed, lots of ways to do this kind of job. Pick the

> one that you find easiest to read and write.

Okay, that is some remark that leaves a more balanced idea in my head about
all this.

Thanks a lot,
Oliver

Am Donnerstag, 15. November 2012 18:49:22 UTC+1 schrieb Matthew Pocock:


 
Sie müssen sich anmelden, bevor Sie Nachrichten veröffentlichen können.
Bevor Sie eine Nachricht posten können, müssen Sie zunächst dieser Gruppe beitreten.
Bitte aktualisieren Sie vor dem Posten in den Abonnementeinstellungen Ihren Spitznamen.
Sie haben nicht die erforderliche Berechtigung zum Posten.
Daniel Sobral  
Profil anzeigen   Übersetzen in die Sprache: Übersetzt (Original anzeigen)
 Weitere Optionen 15 Nov. 2012, 18:34
Von: Daniel Sobral <dcsob...@gmail.com>
Datum: Thu, 15 Nov 2012 21:34:20 -0200
Lokal: Do 15 Nov. 2012 18:34
Betreff: Re: [scala-user] Beginner question about "x match { case None => return; case Some( y ) => y.foo() }"

It's not that different, but it isn't exactly how things are done. In
idiomatic Scala, both null and they return keyword are generally avoided. I
think the problem is mostly that the example is contrived, and without
enough context to answer the question precisely.

So, the first question is whether you are returning some value or not. If
you are returning some value, the option 1 is downright incorrect. It would
typically be written like this:

x.map(_.foo()) // returns Some(x.get.foo()) if x.isDefined, or None if not.
x.map(_.foo()).getOrElse(z) // returns x.get.foo() is x.isDefined, or z if
not
x match {
  case Some(y) => y.foo()
  case None      => None

}

If that code is *not* returning a value -- if it's being executed just for
its side effects, then the code would likely read like this:

x.foreach(_.foo())
x match {
  case Some(y) => y.foo()
  case None      =>

}

*I* wouldn't use pattern matching in either case, but it happens to be
faster than the alternatives, and lots of people just like it better.

Now, if you have performance problems, you might resort to using null. But
that's usually kept hidden -- Scala APIs that return or take nulls are very
rare.

> Regards, Oliver

--
Daniel C. Sobral

I travel to the future all the time.


 
Sie müssen sich anmelden, bevor Sie Nachrichten veröffentlichen können.
Bevor Sie eine Nachricht posten können, müssen Sie zunächst dieser Gruppe beitreten.
Bitte aktualisieren Sie vor dem Posten in den Abonnementeinstellungen Ihren Spitznamen.
Sie haben nicht die erforderliche Berechtigung zum Posten.
Ziad Hatahet  
Profil anzeigen   Übersetzen in die Sprache: Übersetzt (Original anzeigen)
 Weitere Optionen 16 Nov. 2012, 04:11
Von: Ziad Hatahet <hata...@gmail.com>
Datum: Fri, 16 Nov 2012 01:10:29 -0800
Lokal: Fr. 16 Nov. 2012 04:10
Betreff: Re: [scala-user] Beginner question about "x match { case None => return; case Some( y ) => y.foo() }"

On Thu, Nov 15, 2012 at 3:34 PM, Daniel Sobral <dcsob...@gmail.com> wrote:
> Now, if you have performance problems, you might resort to using null. But
> that's usually kept hidden -- Scala APIs that return or take nulls are very
> rare.

Does Option[T] introduce that much overhead as to cause performance issues
when used pervasively?

--
Ziad


 
Sie müssen sich anmelden, bevor Sie Nachrichten veröffentlichen können.
Bevor Sie eine Nachricht posten können, müssen Sie zunächst dieser Gruppe beitreten.
Bitte aktualisieren Sie vor dem Posten in den Abonnementeinstellungen Ihren Spitznamen.
Sie haben nicht die erforderliche Berechtigung zum Posten.
Daniel Sobral  
Profil anzeigen   Übersetzen in die Sprache: Übersetzt (Original anzeigen)
 Weitere Optionen 16 Nov. 2012, 06:53
Von: Daniel Sobral <dcsob...@gmail.com>
Datum: Fri, 16 Nov 2012 09:53:30 -0200
Lokal: Fr. 16 Nov. 2012 06:53
Betreff: Re: [scala-user] Beginner question about "x match { case None => return; case Some( y ) => y.foo() }"

On Fri, Nov 16, 2012 at 7:10 AM, Ziad Hatahet <hata...@gmail.com> wrote:
> On Thu, Nov 15, 2012 at 3:34 PM, Daniel Sobral <dcsob...@gmail.com> wrote:

>> Now, if you have performance problems, you might resort to using null.
>> But that's usually kept hidden -- Scala APIs that return or take nulls are
>> very rare.

> Does Option[T] introduce that much overhead as to cause performance issues
> when used pervasively?

Option can slow down things in two ways:

1. It's another object, so we have two objects in the heap, without any
locality of reference. Other effects, that I expect to be minor, are
increased load due to extra allocations, and increased work on the garbage
collector.

2. If you use foreach/map/etc, certain optimizations made by JIT are
effectively disabled. There's also some penalty in the closure allocation.
Note that you can use pattern matching instead, which avoids this problem.

These are not insurmountable problems, but they are problems right now. As
compiler and JIT improves, it may go away. On the compiler side, the
possibility of effectively optimizing Option with null pointers is within
reach. The main problem is that Some(null) is valid, which would make it
impossible to represent None as null. Lift framework allegedly depends on
that. On the JIT side, optimization of megamorphic call sites has been
pursued for a long time, but the addition of functions to Java will make
this prolem a priority.

But, to answer your question strictly, Option does not introduce "that
much" overhead. It introduces *overhead*. If you have a performance
problem, then you need to decrease overhead.

> --
> Ziad

--
Daniel C. Sobral

I travel to the future all the time.


 
Sie müssen sich anmelden, bevor Sie Nachrichten veröffentlichen können.
Bevor Sie eine Nachricht posten können, müssen Sie zunächst dieser Gruppe beitreten.
Bitte aktualisieren Sie vor dem Posten in den Abonnementeinstellungen Ihren Spitznamen.
Sie haben nicht die erforderliche Berechtigung zum Posten.
Matthew Pocock  
Profil anzeigen   Übersetzen in die Sprache: Übersetzt (Original anzeigen)
 Weitere Optionen 16 Nov. 2012, 07:29
Von: Matthew Pocock <turingatemyhams...@gmail.com>
Datum: Fri, 16 Nov 2012 12:28:59 +0000
Lokal: Fr. 16 Nov. 2012 07:28
Betreff: Re: [scala-user] Beginner question about "x match { case None => return; case Some( y ) => y.foo() }"

Hi,

Performance is only a problem in performance-critical code. In general, the
safety that using Option[T] provides will save you a lot more development
time than it will cost you run-time. If the code that touches Option[T] is
outside of tight loops, it won't introduce any noticeable overhead at all.

I did have one case where a particular implementation of Map was boxing and
unboxing a lot of values to and from Option when doing lookups, and where
the map lookups where right inside a critical loop. In that case, it hurt
me. However, it was obvious what was going on, and there was a work-around.
Since then, I think that implementation has been fixed anyway. As the
library, scala compiler and Java VM all are improved, the overhead of
wrappers like Option[T] will continue to decrease without you needing to
touch your source.

So, the short answer is that IMHO it's not worth worrying about performance
with things like this unless you have problems with performance - it's more
important to have clear and maintainable code. If your program is slow,
then crack open a profiler and see why.

Matthew

On 16 November 2012 09:10, Ziad Hatahet <hata...@gmail.com> wrote:

--
Dr Matthew Pocock
Integrative Bioinformatics Group, School of Computing Science, Newcastle
University
mailto: turingatemyhams...@gmail.com
mailto: matthew.poc...@ncl.ac.uk
gchat: turingatemyhams...@gmail.com
msn: matthew_poc...@yahoo.co.uk
irc.freenode.net: drdozer
skype: matthew.pocock
tel: (0191) 2566550
mob: +447535664143

 
Sie müssen sich anmelden, bevor Sie Nachrichten veröffentlichen können.
Bevor Sie eine Nachricht posten können, müssen Sie zunächst dieser Gruppe beitreten.
Bitte aktualisieren Sie vor dem Posten in den Abonnementeinstellungen Ihren Spitznamen.
Sie haben nicht die erforderliche Berechtigung zum Posten.
Dennis Haupt  
Profil anzeigen   Übersetzen in die Sprache: Übersetzt (Original anzeigen)
 Weitere Optionen 16 Nov. 2012, 08:01
Von: "Dennis Haupt" <h-s...@gmx.de>
Datum: Fri, 16 Nov 2012 14:01:09 +0100
Lokal: Fr. 16 Nov. 2012 08:01
Betreff: Re: [scala-user] Beginner question about "x match { case None => return; case Some( y ) => y.foo() }"

-------- Original-Nachricht --------

> Datum: Fri, 16 Nov 2012 12:28:59 +0000
> Von: Matthew Pocock <turingatemyhams...@gmail.com>
> An: Ziad Hatahet <hata...@gmail.com>
> CC: Daniel Sobral <dcsob...@gmail.com>, scala-user <scala-user@googlegroups.com>
> Betreff: Re: [scala-user] Beginner question about "x match { case None => return; case Some( y ) => y.foo() }"
> Hi,

> Performance is only a problem in performance-critical code. In general,
> the
> safety that using Option[T] provides will save you a lot more development
> time than it will cost you run-time. If the code that touches Option[T] is
> outside of tight loops, it won't introduce any noticeable overhead at all.

100% agree. type safety has saved me so many times that i lost count.

exactly. my strategy is not to worry about performance at all (just to try to pick a smart algorithm, it has a big effect compared to low level optimizations). this speeds up the development process and doesn't complicate my code. if there is a problem (which rarely happens) i can optimize it where it needs to be done. be lazy! work smart, not hard :)


 
Sie müssen sich anmelden, bevor Sie Nachrichten veröffentlichen können.
Bevor Sie eine Nachricht posten können, müssen Sie zunächst dieser Gruppe beitreten.
Bitte aktualisieren Sie vor dem Posten in den Abonnementeinstellungen Ihren Spitznamen.
Sie haben nicht die erforderliche Berechtigung zum Posten.
nicolas.oury@gmail.com  
Profil anzeigen   Übersetzen in die Sprache: Übersetzt (Original anzeigen)
 Weitere Optionen 16 Nov. 2012, 08:28
Von: "nicolas.o...@gmail.com" <nicolas.o...@gmail.com>
Datum: Fri, 16 Nov 2012 13:28:35 +0000
Lokal: Fr. 16 Nov. 2012 08:28
Betreff: Re: [scala-user] Beginner question about "x match { case None => return; case Some( y ) => y.foo() }"

> 100% agree. type safety has saved me so many times that i lost count.

> The problem is that Option does not bring a lot of type safety as

non-nullity (either null or Some null) is not checked at compile time.
So your program can type check and go wrong.

 
Sie müssen sich anmelden, bevor Sie Nachrichten veröffentlichen können.
Bevor Sie eine Nachricht posten können, müssen Sie zunächst dieser Gruppe beitreten.
Bitte aktualisieren Sie vor dem Posten in den Abonnementeinstellungen Ihren Spitznamen.
Sie haben nicht die erforderliche Berechtigung zum Posten.
Seth Tisue  
Profil anzeigen   Übersetzen in die Sprache: Übersetzt (Original anzeigen)
 Weitere Optionen 16 Nov. 2012, 08:33
Von: Seth Tisue <s...@tisue.net>
Datum: Fri, 16 Nov 2012 08:33:05 -0500
Lokal: Fr. 16 Nov. 2012 08:33
Betreff: Re: [scala-user] Beginner question about "x match { case None => return; case Some( y ) => y.foo() }"
On Fri, Nov 16, 2012 at 8:28 AM, nicolas.o...@gmail.com

<nicolas.o...@gmail.com> wrote:
> The problem is that Option does not bring a lot of type safety as
> non-nullity (either null or Some null) is not checked at compile time.

Disagree strongly. In practice, Option brings a great deal of type
safety. It's true that the null issue means it isn't perfect, but in
practice, it just isn't that hard to avoid using null at all in your
code. Nulls simply aren't a big issue for working Scala programmers.

We all look forward to a future in which null is ruled out at compile
time. But the present is actually already very good — much better than
the Option-less past.

--
Seth Tisue - s...@tisue.net - http://tisue.net


 
Sie müssen sich anmelden, bevor Sie Nachrichten veröffentlichen können.
Bevor Sie eine Nachricht posten können, müssen Sie zunächst dieser Gruppe beitreten.
Bitte aktualisieren Sie vor dem Posten in den Abonnementeinstellungen Ihren Spitznamen.
Sie haben nicht die erforderliche Berechtigung zum Posten.
Alec Zorab  
Profil anzeigen   Übersetzen in die Sprache: Übersetzt (Original anzeigen)
 Weitere Optionen 16 Nov. 2012, 08:36
Von: Alec Zorab <aleczo...@googlemail.com>
Datum: Fri, 16 Nov 2012 13:35:58 +0000
Lokal: Fr. 16 Nov. 2012 08:35
Betreff: Re: [scala-user] Beginner question about "x match { case None => return; case Some( y ) => y.foo() }"

Option(null) returns None, so it certainly adds a veneer of respectability
to the entire affair. As we seem to have to revisit every time we have one
of these conversations, you can't (using either the kotlin based annotation
approach or Option) actually guarantee non-nullity on the JVM anyway, so
the question becomes:

Which of these two tactics is more likely to stop NPEs?

I'd argue that building a culture where returning null is considered akin
to spitting in someone's eye certainly helps reduce the problem. As ever, I
offer the anecdata that I've seen less than a doezen NPEs in the last 6
months, all during development and all arising from initialisation order
issues when I'm elbow deep in some cake pattern magic.

On 16 November 2012 13:33, Seth Tisue <s...@tisue.net> wrote:


 
Sie müssen sich anmelden, bevor Sie Nachrichten veröffentlichen können.
Bevor Sie eine Nachricht posten können, müssen Sie zunächst dieser Gruppe beitreten.
Bitte aktualisieren Sie vor dem Posten in den Abonnementeinstellungen Ihren Spitznamen.
Sie haben nicht die erforderliche Berechtigung zum Posten.
Daniel Sobral  
Profil anzeigen   Übersetzen in die Sprache: Übersetzt (Original anzeigen)
 Weitere Optionen 16 Nov. 2012, 09:10
Von: Daniel Sobral <dcsob...@gmail.com>
Datum: Fri, 16 Nov 2012 12:09:45 -0200
Lokal: Fr. 16 Nov. 2012 09:09
Betreff: Re: [scala-user] Beginner question about "x match { case None => return; case Some( y ) => y.foo() }"

On Fri, Nov 16, 2012 at 11:28 AM, nicolas.o...@gmail.com <

nicolas.o...@gmail.com> wrote:

> 100% agree. type safety has saved me so many times that i lost count.

>> The problem is that Option does not bring a lot of type safety as
> non-nullity (either null or Some null) is not checked at compile time.
> So your program can type check and go wrong.

That's bs.

Option provides a way to REPLACE null. If you don't have nulls, you don't
get NPE, and it's as simple as that.

--
Daniel C. Sobral

I travel to the future all the time.


 
Sie müssen sich anmelden, bevor Sie Nachrichten veröffentlichen können.
Bevor Sie eine Nachricht posten können, müssen Sie zunächst dieser Gruppe beitreten.
Bitte aktualisieren Sie vor dem Posten in den Abonnementeinstellungen Ihren Spitznamen.
Sie haben nicht die erforderliche Berechtigung zum Posten.
Dennis Haupt  
Profil anzeigen   Übersetzen in die Sprache: Übersetzt (Original anzeigen)
 Weitere Optionen 16 Nov. 2012, 09:20
Von: "Dennis Haupt" <h-s...@gmx.de>
Datum: Fri, 16 Nov 2012 15:19:56 +0100
Lokal: Fr. 16 Nov. 2012 09:19
Betreff: Re: [scala-user] Beginner question about "x match { case None => return; case Some( y ) => y.foo() }"
i don't return null or pass null as a parameter, so i never get null or Some(null) later

-------- Original-Nachricht --------


 
Sie müssen sich anmelden, bevor Sie Nachrichten veröffentlichen können.
Bevor Sie eine Nachricht posten können, müssen Sie zunächst dieser Gruppe beitreten.
Bitte aktualisieren Sie vor dem Posten in den Abonnementeinstellungen Ihren Spitznamen.
Sie haben nicht die erforderliche Berechtigung zum Posten.
nicolas.oury@gmail.com  
Profil anzeigen   Übersetzen in die Sprache: Übersetzt (Original anzeigen)
 Weitere Optionen 16 Nov. 2012, 09:30
Von: "nicolas.o...@gmail.com" <nicolas.o...@gmail.com>
Datum: Fri, 16 Nov 2012 14:29:53 +0000
Lokal: Fr. 16 Nov. 2012 09:29
Betreff: Re: [scala-user] Beginner question about "x match { case None => return; case Some( y ) => y.foo() }"

On Fri, Nov 16, 2012 at 2:19 PM, Dennis Haupt <h-s...@gmx.de> wrote:
> i don't return null or pass null as a parameter, so i never get null or
> Some(null) later

It is somewhat akin to "I never do type error so I don't need a type
checker."
And usually you don't personally write all the code you run.

It is great to have Option, it would be even better if the policy was
enforced by the type checker in some way.


 
Sie müssen sich anmelden, bevor Sie Nachrichten veröffentlichen können.
Bevor Sie eine Nachricht posten können, müssen Sie zunächst dieser Gruppe beitreten.
Bitte aktualisieren Sie vor dem Posten in den Abonnementeinstellungen Ihren Spitznamen.
Sie haben nicht die erforderliche Berechtigung zum Posten.
Dennis Haupt  
Profil anzeigen   Übersetzen in die Sprache: Übersetzt (Original anzeigen)
 Weitere Optionen 16 Nov. 2012, 09:44
Von: "Dennis Haupt" <h-s...@gmx.de>
Datum: Fri, 16 Nov 2012 15:44:44 +0100
Lokal: Fr. 16 Nov. 2012 09:44
Betreff: Re: [scala-user] Beginner question about "x match { case None => return; case Some( y ) => y.foo() }"
no, it's not. not passing null around is easy (in my own code). not making type errors is almost impossible.

-------- Original-Nachricht --------


 
Sie müssen sich anmelden, bevor Sie Nachrichten veröffentlichen können.
Bevor Sie eine Nachricht posten können, müssen Sie zunächst dieser Gruppe beitreten.
Bitte aktualisieren Sie vor dem Posten in den Abonnementeinstellungen Ihren Spitznamen.
Sie haben nicht die erforderliche Berechtigung zum Posten.
Daniel Sobral  
Profil anzeigen   Übersetzen in die Sprache: Übersetzt (Original anzeigen)
 Weitere Optionen 16 Nov. 2012, 11:23
Von: Daniel Sobral <dcsob...@gmail.com>
Datum: Fri, 16 Nov 2012 14:23:09 -0200
Lokal: Fr. 16 Nov. 2012 11:23
Betreff: Re: [scala-user] Beginner question about "x match { case None => return; case Some( y ) => y.foo() }"

On Fri, Nov 16, 2012 at 12:29 PM, nicolas.o...@gmail.com <

nicolas.o...@gmail.com> wrote:
> On Fri, Nov 16, 2012 at 2:19 PM, Dennis Haupt <h-s...@gmx.de> wrote:

>> i don't return null or pass null as a parameter, so i never get null or
>> Some(null) later

> It is somewhat akin to "I never do type error so I don't need a type
> checker."
> And usually you don't personally write all the code you run.

> It is great to have Option, it would be even better if the policy was
> enforced by the type checker in some way.

There's a huge difference between the two. Writing "null" is a very
conscious act, and checking against that is as easy as doing a grep.
Scalastyle and IntelliJ, for that matter, produce warnings/errors (user's
choice) for that.

But while checking for null "creation" is trivial, checking for improper
null validation is not, and not checking for null is not a conscious act.
So while what Scala offers doesn't make errors impossible, it does prevent
them to a large extent.

And, yes, external code is a problem, particularly from Java. I don't know
any Scala library which receives or returns null intentionally, except
those wrapping Java libraries that work that way. Maybe there are examples,
but they are not particularly common.

And, of course, when interfacing with Java you have to pay as much
attention to what returns nulls as you would in Java, except that you can
convert a nullable reference into an Option at the call site and forget
about it elsewhere.

And that's why Option really makes NPE a non-problem: not only there are
less nulls being passed, but anywhere you do get an NPE, all you have to do
is refactor to use Option, and the compiler will tell guide you through the
rest.

--
Daniel C. Sobral

I travel to the future all the time.


 
Sie müssen sich anmelden, bevor Sie Nachrichten veröffentlichen können.
Bevor Sie eine Nachricht posten können, müssen Sie zunächst dieser Gruppe beitreten.
Bitte aktualisieren Sie vor dem Posten in den Abonnementeinstellungen Ihren Spitznamen.
Sie haben nicht die erforderliche Berechtigung zum Posten.
Geir Hedemark  
Profil anzeigen   Übersetzen in die Sprache: Übersetzt (Original anzeigen)
 Weitere Optionen 16 Nov. 2012, 11:40
Von: Geir Hedemark <geir.hedem...@gmail.com>
Datum: Fri, 16 Nov 2012 17:40:29 +0100
Lokal: Fr. 16 Nov. 2012 11:40
Betreff: Re: [scala-user] Beginner question about "x match { case None => return; case Some( y ) => y.foo() }"

On 2012, Nov 16, at 2:28 PM, nicolas.o...@gmail.com wrote:

> 100% agree. type safety has saved me so many times that i lost count.

> The problem is that Option does not bring a lot of type safety as non-nullity (either null or Some null) is not checked at compile time.
> So your program can type check and go wrong.

Now, I understand how you get a null. But how do you manage to get a Some(null)? Won't Option(null) give you a None?

Geir


 
Sie müssen sich anmelden, bevor Sie Nachrichten veröffentlichen können.
Bevor Sie eine Nachricht posten können, müssen Sie zunächst dieser Gruppe beitreten.
Bitte aktualisieren Sie vor dem Posten in den Abonnementeinstellungen Ihren Spitznamen.
Sie haben nicht die erforderliche Berechtigung zum Posten.
Kevin Wright  
Profil anzeigen   Übersetzen in die Sprache: Übersetzt (Original anzeigen)
 Weitere Optionen 16 Nov. 2012, 12:03
Von: Kevin Wright <kev.lee.wri...@gmail.com>
Datum: Fri, 16 Nov 2012 17:03:25 +0000
Lokal: Fr. 16 Nov. 2012 12:03
Betreff: Re: [scala-user] Beginner question about "x match { case None => return; case Some( y ) => y.foo() }"

Option(null) will, but you can explicitly create Some(null) or get it in a
few other scenarios

e.g.

val m = Map(1->"a", 2 -> null)
m get 1 // returns Some("a")
m get 2 // returns Some(null)
m get 3 // returns None

On 16 November 2012 16:40, Geir Hedemark <geir.hedem...@gmail.com> wrote:


 
Sie müssen sich anmelden, bevor Sie Nachrichten veröffentlichen können.
Bevor Sie eine Nachricht posten können, müssen Sie zunächst dieser Gruppe beitreten.
Bitte aktualisieren Sie vor dem Posten in den Abonnementeinstellungen Ihren Spitznamen.
Sie haben nicht die erforderliche Berechtigung zum Posten.
Daniel Sobral  
Profil anzeigen   Übersetzen in die Sprache: Übersetzt (Original anzeigen)
 Weitere Optionen 16 Nov. 2012, 12:04
Von: Daniel Sobral <dcsob...@gmail.com>
Datum: Fri, 16 Nov 2012 15:03:57 -0200
Lokal: Fr. 16 Nov. 2012 12:03
Betreff: Re: [scala-user] Beginner question about "x match { case None => return; case Some( y ) => y.foo() }"

On Fri, Nov 16, 2012 at 2:40 PM, Geir Hedemark <geir.hedem...@gmail.com>wrote:

> On 2012, Nov 16, at 2:28 PM, nicolas.o...@gmail.com wrote:

> 100% agree. type safety has saved me so many times that i lost count.

>> The problem is that Option does not bring a lot of type safety as
> non-nullity (either null or Some null) is not checked at compile time.
> So your program can type check and go wrong.

> Now, I understand how you get a null. But how do you manage to get a
> Some(null)? Won't Option(null) give you a None?

Option(null) == None
Some(null) == Some(null)

Simple. :-0

You might also start with a Some(v), then map something that turns v into a
null.

> Geir

--
Daniel C. Sobral

I travel to the future all the time.


 
Sie müssen sich anmelden, bevor Sie Nachrichten veröffentlichen können.
Bevor Sie eine Nachricht posten können, müssen Sie zunächst dieser Gruppe beitreten.
Bitte aktualisieren Sie vor dem Posten in den Abonnementeinstellungen Ihren Spitznamen.
Sie haben nicht die erforderliche Berechtigung zum Posten.
Niels  
Profil anzeigen   Übersetzen in die Sprache: Übersetzt (Original anzeigen)
 Weitere Optionen 16 Nov. 2012, 15:21
Von: Niels <nielshoog...@gmail.com>
Datum: Fri, 16 Nov 2012 12:21:27 -0800 (PST)
Lokal: Fr. 16 Nov. 2012 15:21
Betreff: Re: [scala-user] Beginner question about "x match { case None => return; case Some( y ) => y.foo() }"

> I don't know any Scala library which receives or returns null
>> intentionally, except those wrapping Java libraries that work that way.
>> Maybe there are examples, but they are not particularly common.

I have one case where I deliberately use nulls in scala, though it's
tightly controled. In the situation where I have a class with several
arguments being Options of some type, I have created a companion object
with an apply with default arguments where the default value is null.
The apply method transforms the values into options. It makes writing some
constructors a little bit easier. One can supply a value instead of
Some(value). Other than that, I have not personally used a null, other than
interfacing with Java.

 
Sie müssen sich anmelden, bevor Sie Nachrichten veröffentlichen können.
Bevor Sie eine Nachricht posten können, müssen Sie zunächst dieser Gruppe beitreten.
Bitte aktualisieren Sie vor dem Posten in den Abonnementeinstellungen Ihren Spitznamen.
Sie haben nicht die erforderliche Berechtigung zum Posten.
Seth Tisue  
Profil anzeigen   Übersetzen in die Sprache: Übersetzt (Original anzeigen)
 Weitere Optionen 16 Nov. 2012, 15:39
Von: Seth Tisue <s...@tisue.net>
Datum: Fri, 16 Nov 2012 15:39:14 -0500
Lokal: Fr. 16 Nov. 2012 15:39
Betreff: Re: [scala-user] Beginner question about "x match { case None => return; case Some( y ) => y.foo() }"
On Fri, Nov 16, 2012 at 9:29 AM, nicolas.o...@gmail.com

<nicolas.o...@gmail.com> wrote:
> It is great to have Option, it would be even better if the policy was
> enforced by the type checker in some way.

Everyone agrees on that.

But it's highly unclear how it would be achieved without impairing
either Java interop or performance or both.

--
Seth Tisue | Northwestern University | http://tisue.net
developer, NetLogo: http://ccl.northwestern.edu/netlogo/


 
Sie müssen sich anmelden, bevor Sie Nachrichten veröffentlichen können.
Bevor Sie eine Nachricht posten können, müssen Sie zunächst dieser Gruppe beitreten.
Bitte aktualisieren Sie vor dem Posten in den Abonnementeinstellungen Ihren Spitznamen.
Sie haben nicht die erforderliche Berechtigung zum Posten.
Geir Hedemark  
Profil anzeigen   Übersetzen in die Sprache: Übersetzt (Original anzeigen)
 Weitere Optionen 16 Nov. 2012, 16:27
Von: Geir Hedemark <geir.hedem...@gmail.com>
Datum: Fri, 16 Nov 2012 22:27:33 +0100
Lokal: Fr. 16 Nov. 2012 16:27
Betreff: Re: [scala-user] Beginner question about "x match { case None => return; case Some( y ) => y.foo() }"
On 2012, Nov 16, at 6:03 PM, Kevin Wright wrote:

> Option(null) will, but you can explicitly create Some(null) or get it in a few other scenarios

> e.g.

> val m = Map(1->"a", 2 -> null)
> m get 1 // returns Some("a")
> m get 2 // returns Some(null)
> m get 3 // returns None

There you go. This is what I get for living too close to java-land.

Geir


 
Sie müssen sich anmelden, bevor Sie Nachrichten veröffentlichen können.
Bevor Sie eine Nachricht posten können, müssen Sie zunächst dieser Gruppe beitreten.
Bitte aktualisieren Sie vor dem Posten in den Abonnementeinstellungen Ihren Spitznamen.
Sie haben nicht die erforderliche Berechtigung zum Posten.
Ende der Nachrichten
« Zurück zu Diskussionen « Neueres Thema     Älteres Thema »