News for hackers. Stuff that matters.
Re:Ruby gaining a safe navigation operator: '.?' by Udo on Monday October 26, 2015 @06:51AM (#10450550)
I think a good case could be made that, if you're designing a new dynamically typed language from scratch that doesn't have to support older code, safe array/object navigation should be the default.

My argument for this is you should design for the most common use case first, and most commonly people will want '.?' behavior. You can still check explicitly for null in the other cases, or even introduce a more cumbersome non-safe navigation operator. The resulting code will overall need fewer checks, be more readable, you'll have fewer crashes and exceptions, and it will generally do the right thing.

I occasionally write toy languages and this is one behavior I think I got right recently. Along the same lines, but as a more extreme measure, I would argue that invoking a method call on a null object should return null instead of throwing an exception.

Re:Ruby gaining a safe navigation operator: '.?' by skrebbel on Monday October 26, 2015 @06:54AM (#10450561)
I wildly disagree, because I get waking nightmares from the amount of implicit "wtf why doesn't it even enter my method" behavior that would imply, but I upvoted you because I think it's a very interesting idea and I wonder what others think.
Re:Ruby gaining a safe navigation operator: '.?' by Udo on Monday October 26, 2015 @06:59AM (#10450578)
I agree that the invokation idea is relatively wild, that's why I made it into a separate point. There are somewhat successful languages out there where sending a message to a null object doesn't do anything. If this is a class of bug that happens to you frequently, it's probably not for you. But once you get into the mindset that each method call is prepended by a virtual conditional, my hunch is you would know pretty quickly why your method isn't being called.
Re:Ruby gaining a safe navigation operator: '.?' by Peaker on Monday October 26, 2015 @07:24AM (#10450663)
I see this behavior as implicit error silencing.

Wouldn't you prefer seeing:

  foo .? bar . baz .? roo
To know that nil is not even a possibility for `bar`?

Why not make the nil check (or error silencing) explicit when it is so cheap to do so?

Re:Ruby gaining a safe navigation operator: '.?' by Udo on Monday October 26, 2015 @07:31AM (#10450686)
I already talked about my reasoning. But you are correct when you say this is a form of implicit error silencing. Dynamic languages usually have many features which could be described as implicit error silencing, and even statically typed ones often do. Implicit type conversions would be an example. You could very well argue any behavior that deviates from how the processor architecture handles data types and conditionals constitutes a silenced error of some kind. The question becomes where you personally draw the line.
Re:Ruby gaining a safe navigation operator: '.?' by Peaker on Tuesday October 27, 2015 @05:12PM (#10461171)
Indeed I believe in Python's motto of "Errors should never pass silently. Unless explicitly silenced."

I think that Javascript's silencing of wrong parameter counts, Perl's (non-strict) allowing string concat, and non-well-founded implicit coercions (i.e: ones that change the semantics of the value) are all terribly horrible.

I believe that those who value convenience over safety deserve neither and will lose both.

Re:Ruby gaining a safe navigation operator: '.?' by noblethrasher on Monday October 26, 2015 @06:37PM (#10454992)
> There are somewhat successful languages out there where sending a message to a null object doesn't do anything.

The reasons that is defensible in message passing systems is because such systems are basically recapitulations of computer networks, and object nullity is equivalent to a network partition. That's the kind of failure mode that you handle in the policy layer, which is above the runtime layer in an application system just as it would be above the network layer in a distributed system.

Re:Ruby gaining a safe navigation operator: '.?' by bjeanes on Monday October 26, 2015 @07:18AM (#10450647)
And most Objective-C (one of those languages) developers I've talked to about this would recall nightmares about that behaviour.
Re:Ruby gaining a safe navigation operator: '.?' by e28eta on Monday October 26, 2015 @11:34AM (#10452088)
I would simply point to Apple's new language, Swift, dropping that feature as proof that they don't think it's the best way to design a language.

Our team is split (I don't know how evenly) on whether Obj-C or Swift's behavior is better. There are some staunch defenders of Obj-C's pattern being better.

I personally prefer requiring the programmer to explicitly handle nil/null. I think it improves maintainability, because future developers can see the code is correct, and it protects against changes (ex: you knew nil could never happen when your code was written, but then someone changed it so nil does occur and now your code is broken). It also saves future developers from spending time reasoning about the behavior of the code when nil occurs.

Re:Ruby gaining a safe navigation operator: '.?' by cballard on Monday October 26, 2015 @04:27PM (#10454212)
I prefer Optional to implicit nullability, but the debate isn't really between Optional and implicit nullability (is there any question there?).

The distinction is between segfault and returning 0 bits, and I'd say that returning 0 bits is generally better, because most of the time, in iOS apps, especially in absence of a "??" operator, it's what you want. For example, in a table view data source you don't have to check if your data array is non-null, just return its count, it'll be 0 if it is.

i.e. Optional monad > return all 0 bits > segfault

Re:Ruby gaining a safe navigation operator: '.?' by Udo on Monday October 26, 2015 @07:21AM (#10450655)
It's debatable whether this behavior was a good fit for Objective-C, since it's not actually a dynamically typed language. People reasonably expect their Obj-C compiler to do static checks and in this context dynamic behavior is an unwelcome surprise. It would be interesting if, for example, Smalltalk developers share the same frustration. My guess would be: probably not.

My go-to example in favor of the idea would be languages and libraries where 'null' and 'empty list' cause the same behavior in list-processing functions.

Re:Ruby gaining a safe navigation operator: '.?' by cballard on Monday October 26, 2015 @04:23PM (#10454189)
Objective-C is definitely dynamically typed, feel free to use "id" and go to town. To say nothing of swizzling.
Re:Ruby gaining a safe navigation operator: '.?' by dazmax on Monday October 26, 2015 @10:56AM (#10451853)
The main problem I've seen with Objective-C is due to the fact that nil has truthiness, so this code is dangerous:

    if (input.isInvalid()) {
      reject()
    } else {
      accept()
    }
If input is accidentally nil, it's an invisible bug.

The other main complaint is when you have a long string of methods unexpectedly turn out nil at the end, it's a pain to figure out which one it was.

Re:Ruby gaining a safe navigation operator: '.?' by uxp on Monday October 26, 2015 @06:00PM (#10454800)
This is entirely incorrect.

nil is a pointer to the value 0, and in C there is only one value that evaluates to a logical false, which is 0. Invoking a method on nil returns 0. This itself alleviates the kind of nil-check-chains discussed in this thread:

    [[[user profile] name] isEqualToString:@"Paul"];

    [user profile] && [[user profile] name] && [[[user profile] name] isKindOf:NSString] && [[[user profile] name] isEqualToString:@"Paul"];
if user.profile.name is nil, then that expression evaluates to the value 0, which as we discovered, is the _only value that evaluates to false in C_
Re:Ruby gaining a safe navigation operator: '.?' by cballard on Monday October 26, 2015 @04:19PM (#10454156)
Huh?

    (int)nil == 0
Re:Ruby gaining a safe navigation operator: '.?' by MatmaRex on Monday October 26, 2015 @07:16AM (#10450639)
PHP does this (with a warning). I'm not sure if this should be an argument for or against.

    <?php
    $foo = array();
    echo $foo['a']['b']['c']; // not an error
Re:Ruby gaining a safe navigation operator: '.?' by juriansluiman on Monday October 26, 2015 @08:02AM (#10450791)
That's where the coalesce operator ("??") is for. Exactly to suppress the warning and avoid verbose isset() calls:

    echo $foo['a']['b'] ?? 'default_value';
Re:Ruby gaining a safe navigation operator: '.?' by Udo on Monday October 26, 2015 @08:32AM (#10450921)
I'm looking forward to this in PHP7. My current workaround, which exists for the same reason as the coalesce operator (https://wiki.php.net/rfc/isset_ternary) is a custom function:

  $v = @first($var1, $var2, 'some-default');
which will simply return the first parameter that has a non-null, non-empty-string value.
Re:Ruby gaining a safe navigation operator: '.?' by vidarh on Monday October 26, 2015 @07:37AM (#10450717)
Here's a slightly evil way of handling this specific case in Ruby:

   d = lambda {|hash,key| Hash.new(&d) }
   foo = Hash.new(&d)
   p foo['a']['b']['c']
The argument to "Hash.new" is the default value, or a on object that takes the hash object itself and the key, and returns the default value (and optionally stores it in the hash, which makes it great for memoization). In this case, whenever accessing a non-existent hash key, it returns another hash with the same closure as the default...

Of course it's likely to blow up in all kinds of ways since anything trying to do "if foo['a']" to check of 'a' is present in foo will break, for example.

Re:Ruby gaining a safe navigation operator: '.?' by dragonwriter on Monday October 26, 2015 @11:13AM (#10451958)
If you don't have a legacy burden, just including and having the standard library use something like Maybe with a good comprehension syntax seems better than adopting the antipattern of using nil all over the place to start with.