This page is no longer maintained — Please continue to the home page at www.scala-lang.org

Re: Scala vs Objective-C

5 replies
Rob Dickens
Joined: 2008-12-20,
User offline. Last seen 42 years 45 weeks ago.
Hi. Thanks - I didn't think to look this up there. However, I'm afraid it didn't clear very much up for me.
The blog entry talks about things being defined at compile time, in languages (like Java) that do method-dispatch using virtual method tables. However, Java (and Scala) has something called 'late binding', which maybe the writer overlooked.
Anyway, the claim is that Objective-C  has more runtime flexibilty, that makes it better when working with frameworks (like Cocoa).

2009/10/13 Goddard Jiri <g0dd4rd [at] googlemail [dot] com>
hello,   i'm scala newbie but i'd say it's almost the same as for java. http://www.scala-lang.org/docu/files/ScalaReference.pdf - function declarations ande defintions (page 52) and class definitions (page 68)
jiri
On Tue, Oct 13, 2009 at 10:47 AM, Rob Dickens <arctic [dot] bob [at] googlemail [dot] com> wrote:
I've just been reading a (very well written) blog entry[1] on the way the latter does method dispatch, and wondered how Scala fits in.
Java is mentioned, and apparently uses virtual method tables (as opposed to Object-C's message passing). So I just wondered how Scala differs from Java in this respect.
Thanks,
Rob
1 http://cocoawithlove.com/2009/10/objective-c-niche-why-it-survives-in.html



--
web: http://dredwerkz.ic.cz
group: http://groups.google.com/group/dr3dwerkz
icq: 218 659 431



--
Rob, Lafros.com
Matthias Becker
Joined: 2009-09-27,
User offline. Last seen 42 years 45 weeks ago.
Re: Scala vs Objective-C

> The blog entry talks about things being defined at compile time, in
languages (like Java) that do method-dispatch using virtual method tables.
However, Java (and Scala) has something called 'late binding', which maybe
the writer overlooked.

Late binding is done using a virtual method table. Do you know what a
virtual method table is?

Rob Dickens
Joined: 2008-12-20,
User offline. Last seen 42 years 45 weeks ago.
Re: Scala vs Objective-C
yes. But maybe I've been labouring under a misapprehension as to what 'late binding' means. I thought this was one of the things that differentiates Java from C++, but it looks like[1] it's instead just another term for method dispatch.
Maybe the difference I'm thinking of is dynamic linking (Java) as opposed to static linking (C++). Please correct me if I'm wrong (again).
Anyway, I just wanted to know if Scala was doing anything different from Java, at the level of method dispatch, such that it compared more favourably with Objective-C.
http://www.sagecertification.org/events/javavm02/full_papers/zendra/zendra_html/index.html

2009/10/13 Matthias Becker <bekkah [at] web [dot] de>

> The blog entry talks about things being defined at compile time, in
languages (like Java) that do method-dispatch using virtual method tables.
However, Java (and Scala) has something called 'late binding', which maybe
the writer overlooked.



Late binding is done using a virtual method table. Do you know what a
virtual method table is?
--
View this message in context: http://www.nabble.com/Scala-vs-Objective-C-tp25869261p25874776.html
Sent from the Scala - Debate mailing list archive at Nabble.com.




--
Rob, Lafros.com
David Pollak
Joined: 2008-12-16,
User offline. Last seen 42 years 45 weeks ago.
Re: Scala vs Objective-C


On Tue, Oct 13, 2009 at 8:13 AM, Matthias Becker <bekkah [at] web [dot] de> wrote:

> The blog entry talks about things being defined at compile time, in
languages (like Java) that do method-dispatch using virtual method tables.
However, Java (and Scala) has something called 'late binding', which maybe
the writer overlooked.

Scala and Java are statically typed languages which means that, with the exception of reflection, the compiler knows the type of all objects and generates code that the runtime (the JVM) executes to invoke methods.  ObjC supports dynamic dispatch.  All objects can be viewed as an id (like Java Object or Scala Any) and messages are invoked on the instance.  The compiler does not have to know what the type of the object is because the runtime takes care of it.  ObjC also supports prototypes where you can tell the compiler that you really want a certain type to be enforced, but they were added to the language after I stopped coding in ObjC (circa 1995).
Under the covers, however, methods invoked on interfaces/traits use the same dispatch mechanism as ObjC.  A virtual table is used when dispatching against a class (e.g., String), but dynamic method lookup is used when dispatching against an interface (e.g., CharSequence).  This means that even on the JVM, dispatching to interfaces is materially slower than dispatching to classes.  For example:
val s: String = "12345" val cs: CharSequence = s
s.length() // faster cs.length() // slower

In JVM 7, there will be method handles which allow for dynamic dispatch without using reflection.

If you need to change the behavior of a library (as the post discusses), the JVM supports a number of Aspect packages that support changing/intercepting calls.

Now, the practical difference is one of great debate.  After having written a number of commercial products in C (pre-ANSI, so no compiler enforced types), Objective-C, C++, Java, and Scala, I come down on statically typed languages.  While Objective-C's mechanism for dispatching methods leads to very, very nice UI tools (AppKit/Interface Builder is still the best UI library around), it means that you have to write a lot more tests and the complexity for managing complex projects is non-trivial.

Scala's Partial Functions actually give you the same "can you respond to this message" semantics that Objective-C and other dynamic languages give you.

So, if you like statically typed languages where the compiler does compile-time testing of parameters, then Scala is the place to be.  If you like dynamic dispatch, then Objective-C (now with garbage collection) is a good choice, although I think Clojure is a much better choice.

In terms of the technical aspects of the blog post, the poster is generally wrong about everything at the technical level.  Sure, he likes Objective-C and there are good reasons to do so, but he's pretty clueless about the technology underlying the languages/run-times or the ability to do things in other languages/runtimes.

Thanks,

David

 



Late binding is done using a virtual method table. Do you know what a
virtual method table is?
--
View this message in context: http://www.nabble.com/Scala-vs-Objective-C-tp25869261p25874776.html
Sent from the Scala - Debate mailing list archive at Nabble.com.




--
Lift, the simply functional web framework http://liftweb.net
Beginning Scala http://www.apress.com/book/view/1430219890
Follow me: http://twitter.com/dpp
Surf the harmonics
Johannes Rudolph
Joined: 2008-12-17,
User offline. Last seen 29 weeks 20 hours ago.
Re: Scala vs Objective-C

On Tue, Oct 13, 2009 at 6:34 PM, David Pollak
wrote:
> [...]
> Under the covers, however, methods invoked on interfaces/traits use the same
> dispatch mechanism as ObjC.  A virtual table is used when dispatching
> against a class (e.g., String), but dynamic method lookup is used when
> dispatching against an interface (e.g., CharSequence).  This means that even
> on the JVM, dispatching to interfaces is materially slower than dispatching
> to classes.

Implementation details of the HotSpot VM can be found at:

http://wikis.sun.com/display/HotSpotInternals/InterfaceCalls

Rob Dickens
Joined: 2008-12-20,
User offline. Last seen 42 years 45 weeks ago.
Re: Scala vs Objective-C
Many thanks for the responses, particularly David's. So,
* it wasn't a misapprehension after all (see thread)
* the blog entry in question neglected to mention the way that Java's interfaces are treated differently from classes, using the same method dispatch mechanism as in Objective-C
* Scala does not compare more favourably than Java wrt Objective-C, from the point of view of method-dispatch
* Scala does however have things like partial functions (scala.PartialFunction), which do afford more runtime flexibility than Java has
* Objective-C probably does give superior runtime flexibility, of most benefit when dealing with frameworks (like Cocoa), but at the cost of being type-unsafe.
Thanks again for the input.

2009/10/13 Johannes Rudolph <johannes [dot] rudolph [at] googlemail [dot] com>
On Tue, Oct 13, 2009 at 6:34 PM, David Pollak
<feeder [dot] of [dot] the [dot] bears [at] gmail [dot] com> wrote:
> [...]
> Under the covers, however, methods invoked on interfaces/traits use the same
> dispatch mechanism as ObjC.  A virtual table is used when dispatching
> against a class (e.g., String), but dynamic method lookup is used when
> dispatching against an interface (e.g., CharSequence).  This means that even
> on the JVM, dispatching to interfaces is materially slower than dispatching
> to classes.

Implementation details of the HotSpot VM can be found at:

http://wikis.sun.com/display/HotSpotInternals/InterfaceCalls

--
Johannes

-----------------------------------------------
Johannes Rudolph
http://virtual-void.net



--
Rob, Lafros.com

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland