I can call any method on Nil and this feels wrong

jja

I spent considerable time debugging a script recently, and when I finally found the problem it was because of code that looked like this:

class Foo {
    has $.bar;
    method () {
        # do stuff
        $!.bar;
    }
}

It turned out the problem was with that $!.bar, which should have been either $!bar or $.bar. I get this.

But why doesn't this die?

Looking at this in more detail, it looks like the issue here is that I'm trying to call a (non-existent) method bar on $!, which at this point is Nil because there haven't been any errors.

And it looks like I can actually call any method I want on Nil and they all silently return Nil, including stuff like Nil.this-is-a-fake-method and Nil.reverse-entropy(123).

Is this a feature? If so, what's the rationale?

hobbs

It's intended and documented, yes. The headline for Nil is "Absence of a value or a benign failure", and the class documentation mentions

Any method call on Nil of a method that does not exist, and consequently, any subscripting operation, will succeed and return Nil.

say Nil.ITotallyJustMadeThisUp;  # OUTPUT: «Nil␤» 
say (Nil)[100];                  # OUTPUT: «Nil␤» 
say (Nil){100};                  # OUTPUT: «Nil␤»

Synopsis 2 states "Any undefined method call on Nil returns Nil, so that Nil propagates down method call chains. Likewise any subscripting operation on Nil returns Nil", so the intent seems to be allowing expressions like $foo.Bar()[0].Baz() without requiring checks for Nil at every step, or special "Nil-safe" method call and subscripting operators.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

I can call any method on Nil and this feels wrong

From Dev

Can I Hook into any WCF events just prior to operation method call?

From Dev

My ubuntu macbook pro feels warm, can I solve this?

From Dev

My ubuntu macbook pro feels warm, can I solve this?

From Dev

Wrong method call?

From Dev

How can I use reflect.Value.Call() with a nil input?

From Dev

Can I call a set Method inside a Constructor

From Dev

Can I call an async method from OnOptionsItemSelected?

From Dev

Why can I call the .between? method on a Fixnum?

From Dev

How can I debounce a method call?

From Dev

How can I call Timer as a method?

From Dev

How can I call a method from ViewController?

From Dev

How can I call a method in Squeak?

From Dev

How can I "intercept" a method call for a test?

From Dev

Can I call $.ajax method inside $.getJson()?

From Dev

How can I call a method from ViewController?

From Dev

How can I call the displayAd() method?

From Dev

Can't call any method of authenticate webapi, even login or register

From Dev

Can I call a non static method from a static method?

From Dev

Why I can not call super in define_method with overloading method?

From Dev

How can I call a method in another method in the same class?

From Dev

Is there any way I can call Excel VBA function through Python?

From Dev

Why can't I call any methods in my code?

From Dev

Can I call methods of anonymous classes in any time?

From Dev

Why can't I call any page created in node express?

From Dev

How can i call extra method that i define in anonymous class?

From Dev

#<NoMethodError: undefined method `any?' for nil:NilClass>

From Dev

#<NoMethodError: undefined method `any?' for nil:NilClass>

From Dev

How can I make a method take an array of any type as a parameter?

Related Related

  1. 1

    I can call any method on Nil and this feels wrong

  2. 2

    Can I Hook into any WCF events just prior to operation method call?

  3. 3

    My ubuntu macbook pro feels warm, can I solve this?

  4. 4

    My ubuntu macbook pro feels warm, can I solve this?

  5. 5

    Wrong method call?

  6. 6

    How can I use reflect.Value.Call() with a nil input?

  7. 7

    Can I call a set Method inside a Constructor

  8. 8

    Can I call an async method from OnOptionsItemSelected?

  9. 9

    Why can I call the .between? method on a Fixnum?

  10. 10

    How can I debounce a method call?

  11. 11

    How can I call Timer as a method?

  12. 12

    How can I call a method from ViewController?

  13. 13

    How can I call a method in Squeak?

  14. 14

    How can I "intercept" a method call for a test?

  15. 15

    Can I call $.ajax method inside $.getJson()?

  16. 16

    How can I call a method from ViewController?

  17. 17

    How can I call the displayAd() method?

  18. 18

    Can't call any method of authenticate webapi, even login or register

  19. 19

    Can I call a non static method from a static method?

  20. 20

    Why I can not call super in define_method with overloading method?

  21. 21

    How can I call a method in another method in the same class?

  22. 22

    Is there any way I can call Excel VBA function through Python?

  23. 23

    Why can't I call any methods in my code?

  24. 24

    Can I call methods of anonymous classes in any time?

  25. 25

    Why can't I call any page created in node express?

  26. 26

    How can i call extra method that i define in anonymous class?

  27. 27

    #<NoMethodError: undefined method `any?' for nil:NilClass>

  28. 28

    #<NoMethodError: undefined method `any?' for nil:NilClass>

  29. 29

    How can I make a method take an array of any type as a parameter?

HotTag

Archive