5 ms·
_Why Yown Is Funny So You Can Laugh Too
- mapleoin 16y agoI don't think I can read Io that well, but this doesn't seem like a very unique trick; Ruby has method_missing (like the first commenter mentions) and Python has __getattr__. Is there something else I'm not seeing in there?
- draegtun 16y agoAnd Perl has AUTOLOAD. However the difference to these is that Io is being lazy in the examples provided in the blog post. NB. its all just messages and no commas (ie. no args). ref: "Lazy Bricks, Lazy Mortar" by why - WayBackMachine link: http://web.archive.org/web/20080430135919/http://hackety.org/2008/01/10/lazyBricksLazyMortar.html http://web.archive.org/web/20080430135919/http://hackety.org...
- draegtun 16y agoBTW here is a quick & dirty example of Io laziness and shows what can be done without forward (did-not-understand/method_missing/__getattr__/AUTOLOAD): Hash := Object clone do ( with := method ( hash := Map clone call message arguments foreach (arg, # arg = k(v) for eg. t(0) k := arg name v := arg argAt(0) asString hash atPut (k, doString (v)) ) hash ) ) h := Hash with (t(0), f(1+1), with("hello"), writeln("not writeln!")) h2 := Hash with (t(100), f("hello" asUppercase), at("my at"), atPut("my atPut")) Above lazily goes through the Hash with() building the key/value pairs without ever running methods/functions with, writeln, at, atPut and simply takes the method/func name and evaluates its argument into a key/value pair: h at ("writeln") println # => "not writeln!" h at ("f") println # => 2 h2 at ("f") println # => "HELLO" This is the reflection alternative the author is referring to in his post.
- draegtun 16y agoPersonally I thought Yown was a work in progress and just _why dabbling with Io rather than a practical joke. I think Yown got me more interested in playing with Io than anything else. However the Builder is limited (because everything gets converted to a html tag). There are more complete HTML builders for Io on github. Bruce Tate also provides a nice example of a HTML builder in *Seven languages in 7 weeks". Below is my slightly amended version of it: Builder := Object clone Builder forward := method ( write ("<", call message name, ">") call message arguments foreach (arg, content := self doMessage (arg); if (content type == "Sequence" or content type == "Number", write(content)) ) write ("</", call message name, ">") ) Builder do ( html ( h1 ("header") div ( p ("paragraph", b ("bold"), "more text") ul (list(1,2,3) foreach (n, li (n))) ) ) )
- vito 16y agoSort of tooting my own horn here, but here's an HTML builder in the language I'm working on: http://gist.github.com/513228 http://gist.github.com/513228 (Example usage and output at the bottom.) I write these a lot to test a language's flexibility. Seems like Io's "forward" is a lot like "did-not-understand" (and similar mechanisms in Ruby/etc. languages), but it takes priority over messages that would've otherwise been understood.
- draegtun 16y agoThats OK, toot away! But I'll toot my own as well then :) http://github.com/draegtun/builder http://github.com/draegtun/builder Mine (in perl) is more akin to Ruby's Builder by Jim Weirich (http://builder.rubyforge.org/ http://builder.rubyforge.org/) than out and out HTML builder. BTW, other HTML builders have been mentioned on HN before: http://news.ycombinator.com/item?id=1518096 http://news.ycombinator.com/item?id=1518096
- vito 16y agoNeat! They're pretty fun to make; I write them a lot to test a language's flexibility.