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

Scala vs Objective-C

3 replies
Rob Dickens
Joined: 2008-12-20,
User offline. Last seen 42 years 45 weeks ago.
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
Joined: 2008-12-19,
User offline. Last seen 3 years 2 weeks ago.
Re: Scala vs Objective-C

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), 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 :
> 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
>

extempore
Joined: 2008-12-17,
User offline. Last seen 35 weeks 3 days ago.
Re: Scala vs Objective-C

On 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

Viktor Klang
Joined: 2008-12-17,
User offline. Last seen 1 year 27 weeks ago.
Re: Scala vs Objective-C


On Tue, Oct 13, 2009 at 10:38 PM, Paul Phillips <paulp [at] improving [dot] org> wrote:
On 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

can o 'worms
 

--
Paul Phillips      | These are the climbs that apply men's soles.
Stickler           |
Empiricist         |
up hill, pi pals!  |----------* http://www.improving.org/paulp/ *----------



--
Viktor Klang

Blog: klangism.blogspot.com
Twttr: viktorklang
Wave: viktor [dot] klang [at] googlewave [dot] com
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

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