Acknowledged

RFC: Sugar for has, instanceof

Currently we have

foo has :update

While this is nice, negating the thing is !(foo has :update) or foo has :update === false

Could we get some sugar and make has an object method: foo.has(:update), this would make it easy to write

if (foo.has(:update)) { } or if (!foo.has(:update)) { }

 

If this cannot be has, I'm also ok with the use of can

Perhaps we can also have this for instanceof?

 

Parents Comment Children
  • I would actually love some kind of function that tells me the instance name of an object, eg fooType = whatami(foo); we can do some more introspection of things during testing and debugging.

  • Sure, null is weird in that respect because it is nothing and nothing can never be instanceof anything, not even null itself. But that would still allow us to do things with things we know are valid objects, eg

        function foo(str as String) {
          t.isa(str, Lang.String, "We have a string so we cannot fail");
        }
    
        (:test)
        function testFooFunction(logger) {
          foo("String"); # test passes
          foo(1);        # doesn't even reach the test
                         # ERROR: fenix6xpro: /home/ciq/src/source/t/typechecking.mc:44:
                         # Invalid '$.Toybox.Lang.Number' passed as parameter 1 of type 
                         # '$.Toybox.Lang.String'.  
          return true;
        }