<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-279435714709312740</id><updated>2011-04-21T13:52:55.196-07:00</updated><category term='asinstanceof'/><category term='AnyVal'/><category term='reflection'/><category term='clone'/><category term='implicit'/><category term='scala'/><category term='method'/><category term='java'/><category term='call'/><category term='dynamic'/><category term='null'/><category term='actors'/><title type='text'>dzhigital joy of coding</title><subtitle type='html'>Tricks and elegant solutions in Scala or other languages...</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://dzhigital.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/279435714709312740/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://dzhigital.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>xocoatl</name><uri>http://www.blogger.com/profile/10853781067421647746</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>4</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-279435714709312740.post-8917842122524538331</id><published>2009-03-28T04:47:00.000-07:00</published><updated>2009-04-04T03:23:47.029-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='null'/><category scheme='http://www.blogger.com/atom/ns#' term='implicit'/><category scheme='http://www.blogger.com/atom/ns#' term='AnyVal'/><category scheme='http://www.blogger.com/atom/ns#' term='scala'/><title type='text'>Nullable AnyVal in Scala</title><content type='html'>&lt;span style="font-style:italic;"&gt;DISCLAIMER: Please don't treat it too seriously. And never ever use the following code for anything serious. &lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Usually we want to get rid of potential nulls in the code.  Here I'll go the opposite way...&lt;br /&gt;&lt;br /&gt;All things in Scala are instances of classes. Full OO. The base type for everything is Any. On the other hand there are simple types (still objects!) that inherit from AnyVal and complex types that inherit from AnyRef.&lt;br /&gt;&lt;br /&gt;Classes in AnyVal family represent primitive types from underlying (JVM) platform. The compiler makes the decision when to do (un)boxing, so that everything works optimally and (what's even more important) according Scala semantics.&lt;br /&gt;&lt;br /&gt;The very important fact is that in Scala null has type Null. That special class is a subclass of every AnyRef derived class. In practice it means that &lt;span style="font-weight:bold;"&gt;every variable of type Any, AnyRef or AnyRef's subtypes may have a value null. We say that they are nullable.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://www.scala-lang.org/node/128"&gt;&lt;img style="float:center; margin:0 0 10px 10px;cursor:pointer; cursor:hand" src="http://1.bp.blogspot.com/_R5ot2jAIEgQ/Sdc0JLR_-rI/AAAAAAAAAOM/lg6Yx4_RH9I/s320/classhierarchy.png" border="0" alt="" id="BLOGGER_PHOTO_ID_5320778817013283506" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;This is a cause of many bugs... many programming languages aficionados wish that nulls never existed.&lt;br /&gt;&lt;br /&gt;Back to the topic. &lt;span style="font-weight:bold;"&gt;Variables of type AnyVal (or subtypes) normally can not be nulled&lt;/span&gt;.&lt;br /&gt;&lt;br /&gt;&lt;pre class="brush: scala"&gt;&lt;br /&gt; val anyval1: AnyVal = null&lt;br /&gt;/* error: type mismatch;&lt;br /&gt;found   : Null(null)&lt;br /&gt;required: AnyVal&lt;br /&gt;*/&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;But there is a unique Scala functionality (implicit defs aka. views) and a bug in the compiler that allows for that.&lt;br /&gt;&lt;br /&gt;&lt;pre class="brush: scala"&gt;&lt;br /&gt;implicit def theOtherNull(n: Null): AnyVal = { object o { var nn: AnyVal = _ }; o.nn }&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;After defining this view the following code will magically work.&lt;br /&gt;&lt;br /&gt;&lt;pre class="brush: scala"&gt;&lt;br /&gt;val anyval2: AnyVal = null&lt;br /&gt;&lt;br /&gt;println(x == null)  // "true" !&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Uninitialized variable in classes have to have a value. Even if they are AnyVals there needs to be a special default/uninitialized value. Unfortunately as of now that value is a null from underlying JVM platform. Of course it's a bug, becouse it breaks the type system... even worse thing is that this weird null value can be propagated to other vals/vars of type AnyVal.&lt;br /&gt;In case of the view above it's returned, the type checking part of the compiler assumes that the returned value will be a healthy AnyVal, but it's not so anyval2 get's that forbidden/improperly typed value.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/279435714709312740-8917842122524538331?l=dzhigital.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dzhigital.blogspot.com/feeds/8917842122524538331/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://dzhigital.blogspot.com/2009/03/nullable-anyval-in-scala.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/279435714709312740/posts/default/8917842122524538331'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/279435714709312740/posts/default/8917842122524538331'/><link rel='alternate' type='text/html' href='http://dzhigital.blogspot.com/2009/03/nullable-anyval-in-scala.html' title='Nullable AnyVal in Scala'/><author><name>xocoatl</name><uri>http://www.blogger.com/profile/10853781067421647746</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://1.bp.blogspot.com/_R5ot2jAIEgQ/Sdc0JLR_-rI/AAAAAAAAAOM/lg6Yx4_RH9I/s72-c/classhierarchy.png' height='72' width='72'/><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-279435714709312740.post-6683384866386430570</id><published>2009-03-20T15:12:00.000-07:00</published><updated>2009-03-21T07:31:52.419-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='actors'/><category scheme='http://www.blogger.com/atom/ns#' term='clone'/><category scheme='http://www.blogger.com/atom/ns#' term='scala'/><title type='text'>Cloning Scala actors</title><content type='html'>&lt;div&gt;I thought I'd base one of my projects on Scala actors. On the other hand seeing how actors are dynamically typed reminded me of my experiments with dynamic, clone based languages (Io, Ioke, Self, JavaScript).&lt;br /&gt;&lt;/div&gt;&lt;div&gt;The preferred way to create an actor in Scala is something like this:&lt;/div&gt;&lt;br /&gt;&lt;pre class="brush: scala"&gt;&lt;br /&gt;val anActor = actor {&lt;br /&gt; // here goes the body of an actor.: a loop, call to receive/react, message patterns...&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;div&gt;The problem is I want to have a function cloneActor() that would return a clone of a scala actor:&lt;/div&gt;&lt;br /&gt;&lt;pre class="brush: scala"&gt;val anotherActor = cloneActor(anActor)&lt;/pre&gt;&lt;br /&gt;&lt;div&gt;Note that (for now) I don't want this actor's state to be cloned, just the body.&lt;/div&gt;&lt;div&gt;Just like if you would clone a human being: genetically identical body, but a fresh, clear memory...&lt;/div&gt;&lt;div&gt;At first I thought about using reflection in order to create new instance of the existing actor. &lt;/div&gt;&lt;br /&gt;&lt;pre class="brush: scala"&gt;&lt;br /&gt;  &lt;br /&gt;val reflectivelyClonedActor = anActor.getClass.newInstance // this won't work&lt;br /&gt;  &lt;br /&gt;&lt;/pre&gt;&lt;div&gt;Even if it didn't throw a run-time exception it wouldn't clone the body of the actor. :-(&lt;/div&gt;&lt;div&gt;The body is a closure passed to the scala.actor.Actor.actor() method. &lt;/div&gt;&lt;div&gt;Second idea was to just create a new actor in a standard way, but reflectively copy the body... Unfortunately the body isn't kept in a explicitly declared field. &lt;/div&gt;&lt;div&gt;Althought there is a implicitly created synthetic field  body$1, it's marked private final by the compiler.&lt;/div&gt;&lt;br /&gt;&lt;pre class="brush: text"&gt;scala&gt; a1.getClass.getDeclaredField("body$1").get(a1)&lt;br /&gt;java.lang.IllegalAccessException: Class  can not access a member of class scala.actors.Actor$$anon$1 with modifiers "private final"&lt;br /&gt;      at sun.reflect.Reflection.ensureMemberAccess(Reflection.java:65)&lt;br /&gt;      at .....&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Well it was too tricky in the first place... OK. Out of ideas. Let's look at the source of scala.actor.Actor:&lt;br /&gt;&lt;pre class="brush: scala"&gt;&lt;br /&gt;def actor(body: =&gt; Unit): Actor = {&lt;br /&gt; val actor = new Actor {&lt;br /&gt;   def act() = body&lt;br /&gt; }&lt;br /&gt; actor.start()&lt;br /&gt; actor&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;div&gt;Interesting thing is that act() has type () =&gt; Unit while body has type =&gt; Unit.&lt;/div&gt;&lt;div&gt;So we can freely use existing actor's  act as a body of new actor. Voila! &lt;/div&gt;&lt;pre class="brush: scala"&gt;&lt;br /&gt;  &lt;br /&gt;def cloneActor(a: Actor) = actor(a.act)&lt;br /&gt;  &lt;br /&gt;&lt;/pre&gt;&lt;div&gt;Sure it has it's downsides...&lt;/div&gt;What's more interesting if you want to treat actor as wrappers for threads to use all 8 cores of your shiny new machine:&lt;br /&gt;&lt;pre class="brush: scala"&gt;&lt;br /&gt;implicit def intmulact(i: Int) = new {                     &lt;br /&gt; def *(a: scala.actors.Actor) = (1 to i) map { _ =&gt; cloneActor(a) } toList&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;val executors = 8 * actor {&lt;br /&gt;// body of the actor&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/279435714709312740-6683384866386430570?l=dzhigital.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dzhigital.blogspot.com/feeds/6683384866386430570/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://dzhigital.blogspot.com/2009/03/cloning-scala-actors.html#comment-form' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/279435714709312740/posts/default/6683384866386430570'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/279435714709312740/posts/default/6683384866386430570'/><link rel='alternate' type='text/html' href='http://dzhigital.blogspot.com/2009/03/cloning-scala-actors.html' title='Cloning Scala actors'/><author><name>xocoatl</name><uri>http://www.blogger.com/profile/10853781067421647746</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-279435714709312740.post-6913124762224802098</id><published>2009-03-11T02:17:00.000-07:00</published><updated>2009-03-21T07:35:14.952-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='dynamic'/><category scheme='http://www.blogger.com/atom/ns#' term='call'/><category scheme='http://www.blogger.com/atom/ns#' term='reflection'/><category scheme='http://www.blogger.com/atom/ns#' term='java'/><category scheme='http://www.blogger.com/atom/ns#' term='asinstanceof'/><category scheme='http://www.blogger.com/atom/ns#' term='method'/><category scheme='http://www.blogger.com/atom/ns#' term='scala'/><title type='text'>Dynamic method calls in Scala</title><content type='html'>&lt;div&gt;This is a trick that I showed to quite experienced Scala coders and some were surprised by it... Let's give it some persistence in the blogosphere.&lt;/div&gt;&lt;div&gt;If you spent some time hacking Scala you probably heard of "asInstanceOf[T]". Which is a Scala's notation for casting. You probably used it in the way similar to this&lt;/div&gt;&lt;br /&gt;&lt;pre class="brush: scala"&gt;&lt;br /&gt;serviceRegister.get('serviceIdentifier).asInstanceOf[SomeServiceClass].method()&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;div&gt;serviceRegister.get() finds a service implementor in the register and returns base, abstract thing... So to use the service you  need to cast it.&lt;/div&gt;&lt;div&gt;Second thing you probably know as a Scala hacker is structural typing - kind of explicit, type safe duck typing. For example method's parameter may have it's type specified as "an object that has a method that is named "add" and it has two Int parameters and results in an Int".&lt;/div&gt;&lt;br /&gt;&lt;pre class="brush: scala"&gt;&lt;br /&gt;def callsAdd(obj: {def add(a: Int, b: Int): Int}) { &lt;br /&gt;    /* does something and calls obj.add() */ &lt;br /&gt;  }&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;div&gt;What happens when you mix both asInstanceOf and structural types?&lt;/div&gt;&lt;br /&gt;&lt;pre class="brush: scala"&gt;&lt;br /&gt;println( anObject.asInstanceOf[{def add(a: Int, b: Int): Int}].add(1, 2) )&lt;/pre&gt;&lt;br /&gt;&lt;div&gt;Voila! An easiest way to reflectively call a Java/Scala method in a compiled language.&lt;/div&gt;&lt;div&gt;Compare this to Java's way of doing reflective call:&lt;/div&gt;&lt;br /&gt;&lt;pre class="brush: java"&gt;&lt;br /&gt;import java.lang.reflect.*;&lt;br /&gt;    &lt;br /&gt;public class method2 {&lt;br /&gt;   public int add(int a, int b)&lt;br /&gt;   {&lt;br /&gt;      return a + b;&lt;br /&gt;   }&lt;br /&gt;    &lt;br /&gt;   public static void main(String args[])&lt;br /&gt;   {&lt;br /&gt;      try {&lt;br /&gt;        Class cls = Class.forName("method2");&lt;br /&gt;        Class partypes[] = new Class[2];&lt;br /&gt;         partypes[0] = Integer.TYPE;&lt;br /&gt;         partypes[1] = Integer.TYPE;&lt;br /&gt;         Method meth = cls.getMethod(&lt;br /&gt;           "add", partypes);&lt;br /&gt;         method2 methobj = new method2();&lt;br /&gt;         Object arglist[] = new Object[2];&lt;br /&gt;         arglist[0] = new Integer(37);&lt;br /&gt;         arglist[1] = new Integer(47);&lt;br /&gt;         Object retobj &lt;br /&gt;           = meth.invoke(methobj, arglist);&lt;br /&gt;         Integer retval = (Integer)retobj;&lt;br /&gt;         System.out.println(retval.intValue());&lt;br /&gt;      }&lt;br /&gt;      catch (Throwable e) {&lt;br /&gt;         System.err.println(e);&lt;br /&gt;      }&lt;br /&gt;   }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/279435714709312740-6913124762224802098?l=dzhigital.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dzhigital.blogspot.com/feeds/6913124762224802098/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://dzhigital.blogspot.com/2009/03/dynamic-method-calls-in-scala.html#comment-form' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/279435714709312740/posts/default/6913124762224802098'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/279435714709312740/posts/default/6913124762224802098'/><link rel='alternate' type='text/html' href='http://dzhigital.blogspot.com/2009/03/dynamic-method-calls-in-scala.html' title='Dynamic method calls in Scala'/><author><name>xocoatl</name><uri>http://www.blogger.com/profile/10853781067421647746</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-279435714709312740.post-2486604275205589699</id><published>2009-03-11T01:52:00.000-07:00</published><updated>2009-03-20T15:01:43.246-07:00</updated><title type='text'>About the weird title of this blog</title><content type='html'>&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://upload.wikimedia.org/wikipedia/commons/7/71/Dzhigitovka.jpg"&gt;&lt;img style="float:left; margin:0 10px 10px 0;cursor:pointer; cursor:hand;width: 400px; height: 620px;" src="http://upload.wikimedia.org/wikipedia/commons/7/71/Dzhigitovka.jpg" border="0" alt="" /&gt;&lt;/a&gt;&lt;div&gt;Some of you may wonder why I named my blog in such a hard to remember, user unfriendly way. (Hint: Use RSS or bookmark it. Don't try to remember it...)&lt;/div&gt;&lt;div&gt; &lt;br /&gt;&lt;/div&gt;&lt;div&gt;"Dzhigital" is a neologism I just came up with after reading on &lt;a href="http://en.wikipedia.org/wiki/Dzhigit"&gt;wikipedia about "dzhigit"&lt;/a&gt; and seeing &lt;a href="http://en.wikipedia.org/wiki/File:Dzhigitovka.jpg"&gt;this&lt;/a&gt; picture. (compare to &lt;a href="http://en.wikipedia.org/wiki/File:Policja_konna_Pozna%C5%84.jpg"&gt;this&lt;/a&gt;)&lt;br /&gt;&lt;/div&gt;&lt;div&gt; &lt;/div&gt;&lt;div&gt;This reflects what I like the most in coding and what I'll cover here in this blog... Dangerous, experimental, tricky stuff on powerful horses... err... languages (Scala, Haskell or dynamic ones).&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;The stuff that usually nobody wants to pay you for... &lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;If you're a bit like me you won't care. You love coding (programming, developing or playing with abstractions and languages) and you'd keep on doing it even if (especially) it was forbidden. :-)&lt;br /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/279435714709312740-2486604275205589699?l=dzhigital.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dzhigital.blogspot.com/feeds/2486604275205589699/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://dzhigital.blogspot.com/2009/03/about-freaking-weird-title-of-this-blog.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/279435714709312740/posts/default/2486604275205589699'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/279435714709312740/posts/default/2486604275205589699'/><link rel='alternate' type='text/html' href='http://dzhigital.blogspot.com/2009/03/about-freaking-weird-title-of-this-blog.html' title='About the weird title of this blog'/><author><name>xocoatl</name><uri>http://www.blogger.com/profile/10853781067421647746</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry></feed>
