|
View:
New views
10 Messages
—
Rating Filter:
Alert me
|
|
|
Scala vs Objective-CI'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 |
|
|
|
|
|
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? |
|
|
Re: Scala vs Objective-Cyes. 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.
1 http://www.sagecertification.org/events/javavm02/full_papers/zendra/zendra_html/index.html 2009/10/13 Matthias Becker <bekkah@...>
-- Rob, Lafros.com |
|
|
Re: Scala vs Objective-COn Tue, Oct 13, 2009 at 8:13 AM, Matthias Becker <bekkah@...> wrote:
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" 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
-- 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 |
|
|
Re: Scala vs Objective-COn Tue, Oct 13, 2009 at 6:34 PM, David Pollak
<feeder.of.the.bears@...> 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 |
|
|
Re: Scala vs Objective-CMany 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.rudolph@...> On Tue, Oct 13, 2009 at 6:34 PM, David Pollak -- Rob, Lafros.com |
|
|
Re: Scala vs Objective-CYou can implement Objective C's message passing via reflective calls
in Java or Scala. Paul Philips had a prototype for an 'o' method added to Any via an implicit (implicit-loathers: I don't think he intends to commit it to Scala), so that obj o methodName would reflectively invoke methodName on obj if it existed. Of course, then the niceties of Scala's type system like implicits and type parameters would be completely absent, but if that's what it takes to be able to boast how 'dynamic' you are, then so be it. For reference, C# 4 adds the 'dynamic' keyword, so that dynamic foo = bar; foo.Baz(spam) - Baz is not checked at compile time. And you lose extension methods and type parameters if you use dynamic. It's not really a feature to boast about; it just lets unsound code past the compiler. Scala has a more powerful type system, so there's less need to get stuff past the compiler. Scala's structural types help in those limited cases where you do just need to get some code past the compiler (e.g., you want to write a method that can take any object that has a def close(): Unit), though reflection is still needed occasionally. 2009/10/13 Rob Dickens <arctic.bob@...>: > 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 > -- Ricky Clarkson Java Programmer, AD Holdings +44 1565 770804 Skype: ricky_clarkson Google Talk: ricky.clarkson@... |
|
|
Re: Scala vs Objective-COn Tue, Oct 13, 2009 at 09:05:54PM +0100, Ricky Clarkson wrote:
> You can implement Objective C's message passing via reflective calls > in Java or Scala. Paul Philips had a prototype for an 'o' method > added to Any via an implicit (implicit-loathers: I don't think he > intends to commit it to Scala) It's been in trunk a long time. Ssshhhhhhh, it's @experimental. scala> import scala.reflect.Invocation._ import scala.reflect.Invocation._ scala> "abc" o 'length res0: Any = 3 -- Paul Phillips | These are the climbs that apply men's soles. Stickler | Empiricist | up hill, pi pals! |----------* http://www.improving.org/paulp/ *---------- |
|
|
Re: Scala vs Objective-COn Tue, Oct 13, 2009 at 10:38 PM, Paul Phillips <paulp@...> wrote:
can o 'worms
-- Viktor Klang Blog: klangism.blogspot.com Twttr: viktorklang Wave: viktor.klang@... Code: github.com/viktorklang AKKA Committer - akkasource.org Lift Committer - liftweb.com Atmosphere Committer - atmosphere.dev.java.net SoftPub founder: http://groups.google.com/group/softpub |
| Free embeddable forum powered by Nabble | Forum Help |