Mercurial > repos > other > SevenLanguagesInSevenWeeks
view 2-Io/day1-self-study.io @ 38:e8407d4e72dd
Add code on reflection - basically involves the object returned by "proto" message
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Wed, 13 Sep 2017 20:11:52 +0100 |
parents | 15eb99e79dd4 |
children |
line wrap: on
line source
#Truthiness: # We already know that 0 is truthy (true and 0) println # Empty strings are apparently truthy (true and "") println # As are empty lists (true and list()) println # But nil is falsey (true and nil) println # So basically anything except `false` and `nil` is truthy # = vs := vs ::= # Obvious part so far is that you must create with := before being able to assign with plain = myvar := Object clone myvar println myvar = Object clone # Docs say: # ::= Creates slot, creates setter, assigns value # := Creates slot, assigns value # = Assigns value to slot if it exists, otherwise raises exception # But what is a "setter" in this context? myvar foo ::= Object clone myvar println # It seems we get a "setX" method, which helps us do some things in certain contexts: # https://stackoverflow.com/a/5977757 # What slots does a prototype support? Direct ones are human readable in the output myvar proto println # But they're not listed indirectly SubClass := Object clone myvar2 := SubClass clone myvar2 proto println # Can we find it out programattically? # It's not recursive, but the type should tell us: myvar2 type proto println # Except the value of `type` is just a string (or Sequence in Io), so it doesn't myvar2 type proto type println # Or, of course, a Prototype is a named object that starts with a capital letter # So we can just use slotNames to get them programmatically (but that only shows direct slots) myvar slotNames println # And to get the parent ones then we just chain "proto" myvar proto slotNames println myvar proto proto slotNames println # If I knew enough Io then I'd do a recursion loop and walk up until proto == Object # Update: See day2-reflection.io! #Execute methods by slot name - use "perform" SubClass method1 := method("Method 1 was called" println) SubClass method2 := method("Method 2 was called" println) myvar2 perform("method1") myvar2 perform("method2") # Is Io strongly or weakly typed? (1 + 1) println (1 + "one") println # Exception: argument 0 to method '+' must be a Number, not a 'Sequence' # Therefore strongly typed