1. 13
  1. 4

    Oil adopts weak typing. đŸ˜± I guess that makes sense for a shell language, but what about an explicit string-to-number conversion rune, like #'1'?

    1. 8

      Glad readers are paying attention :) You COULD call it weak typing – there is implicit conversion. But I think weak typing has the connotation of “footguns”, and I have taken pains to avoid the footguns. It’s supposed to give you the convenience of shell, PHP, and Perl, but without the downsides :)

      Of course I welcome people to test that claim! If you find any problems we can still change it. (OSH is stable now, but Oil is not.)

      There is an explicit Int('42') function now, which is just Python’s int() now. Types are capitalized.


      I think Oil’s behavior is more predictable than Python 2, JavaScript, and arguably Python 3. The key difference is that operators aren’t overloaded in Oil:

      • + is always addition (it’s not also concatenation as in Python and JavaScript)
      • ++ is always concatenation (for strings and lists)
      • There is no == operator as in Python and JS. You have to choose === for exact equality or ~== for type converting quality. The latter is quite useful for shell-like code.
        • So I wouldn’t frame it in terms of “weak typing” – I would frame it as WHEN conversions are done.
      • < is always numeric comparison (it’s never lexicographical comparison, which will probably be cmp() or something, akin to strcmp())

      Prior to this change, Oil inherited Python 2’s comparison behavior, which was changed in Python 3. EVERY object was comparable to every other, and objects of different type were compared arbitrarily – by their type names.

      $ python2 -c 'print("2" < 3)'
      False
      
      $ python3 -c 'print("2" < 3)'
      Traceback (most recent call last):
        File "<string>", line 1, in <module>
      TypeError: '<' not supported between instances of 'str' and 'int'
      

      Overall I would say Oil has more coherent and consistent data types than shell/Perl/PHP, BUT it’s also more concrete than Python.

      It doesn’t have Python’s highly overloaded object model, and extensible data types. Oil has simple JSON-like data types, more like JavaScript.

      I’ve taught Python to beginners and I notice they get confused by the difference between (x in mystr), (x in mylist), and (x in mydict), as well as a < b meaning different things depending on the type, etc. Python code can be very clean and minimal, but I’d also say there are TOO few syntactic cues in some cases.

      This doc is incomplete but contains much of this info: https://www.oilshell.org/release/latest/doc/oil-vs-python.html

      Let me know what you think :) Ideally after testing. Hint, the = operator lets you evaluate expressions:

      oil$ = 1 === "1"
      (Bool)   False
      
      oil$ = 1 < "2"
      (Bool)   True
      

      (it’s like Lua, the RHS is evaluated and printed)