Charles F. Munat | 1 Sep 01:01 2009

Re: dynamically creating instances

So can Maker now be used with something other than a String? Do you have 
an example?

This is really fascinating, but I need to get much more familiar with 
the type system before it really makes sense to me.

Thanks!

Chas.

David Pollak wrote:
> import scala.reflect.Manifest
> 
> object Maker {
>   def apply[T](params: Any*)(implicit m: Manifest[T]): T = {
>     val p2: Seq[Object] = params.flatMap {
>       case o: Object => Some(o)
>       case _ => None
>     }
> 
>     val paramClasses: Seq[Class[_]] = p2.map(_.getClass)
> 
>     m.erasure.getConstructor(paramClasses :_*).
>     newInstance(p2 :_*).asInstanceOf[T]
>   }
> 
>   def apply(clz: String, params: Any*): AnyRef = {
>     val p2: Seq[Object] = params.flatMap {
>       case o: Object => Some(o)
>       case _ => None
(Continue reading)

Jorge Ortiz | 1 Sep 01:34 2009
Picon

Re: How to use synchronized?

Scala's synchronized is a "method" in that for all intents and purposes it looks and acts like a method, but it is special in that the sense that it exists as a special form in Java, and gets compiled down to the special form rather than a method call. A type signature of synchronized[T0](arg0: => T0): T0 would probably be less misleading, but the point is kind of moot as it's probably just getting translated to Java's synchronized(this) { ... }.

--j

On Mon, Aug 31, 2009 at 1:00 PM, Rodrigo Cano <ioniviil-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
Yes, I perfectly understand what synchronized is for, I had worked a lot with threads in java, and know how to use it, thank you. What puzzles me is that the signature for it according to the scaladoc is
final def synchronized[T0](arg0 : T0) : T0 So, what is T0, it doesnt look like a by-name paremeter. That is why I wrote a function with similar arguments (the debug function I showed you), and see that the code didn't got executed within debug.
Happily, I already understood by-name parameters and functions in scala, so that is not the problem. Thanks for the explanation anyway, but my doubt still remains. What is T0, is it a function?, a by-name?, there is no sourcecode for Any, so I cannot see for myself.

Anyway, according to the examples you showed me about how to use it, I suppose that it must be a by-name parameter, and it gets executed synchronizedly returning after that, as in the "val x = 2 * synchronized" that Naftoli Gugenheim kindly showed me.

Thanks.
Rodrigo



On Mon, Aug 31, 2009 at 3:48 PM, David Flemström <david.flemstrom-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:


synchronized is useful when you're dealing with threading; you basically want to make sure that a method (or section of code) is called only once at any point in time, so that two threads don't try to access the method simultaneously and thereby disrupt some kind of state that the method maintains.


So for example if I write:


import scala.actors.Actor._
def a() = {
val someInt = Random.nextInt(10) //random number between 0 and 10
for(i <- 0 to 2000)
println("hello " + someInt) //prints 2000 times
}


actor {
a()
}
actor {
a()
}


...then a will be called simultaneously (probably) by two threads. You might then get output like this:
hello 3
hello 3
hello 2
hello 3
hello 2
hello 3
hello 2
hello 3
hello 2
...continues mixed like this exactly 4000 times


However, if we set a to be synchronized, it can only be "visited" by one thread at once, so:
def a() = synchronized {
//...
}


will make the output look like this:
hello 9
hello 9
hello 9
hello 9
...continues exactly 2000 times
hello 4
hello 4
hello 4
hello 4
hello 4
hello 4
...continues exactly 2000 times


synchronized can be very useful in methods that change some shared variables, or needs to perform operations that cannot be interrupted by other threads.


On Monday 31 August 2009 20:34:56 Rodrigo Cano wrote:
> Exactly that, how to use the synchronized method that all objects inherit?
> I cannot understand from its signature. The method accepts just anything?
> and returns it!?
>
> I tried a method test method with a similar signature to find out
>
> def debug[T0](arg0: T0): T0 = {
> println("Arg is " + arg0.asInstanceOf[AnyRef].getClass)
> arg0
> }
>
> And tried calling it as:
>
> debug {
> println("What im doing?")
> }
>
> But the println("Waht im doing?") prints first, and the getClass returns
> BoxedUnit. So no idea of how synchronized actually works
>
> Thanks.
> Rodrigo

David Flemström
david.flemstrom-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org


Naftoli Gugenheim | 1 Sep 01:40 2009
Picon

Re: dynamically creating instances

Are you familar with how lift creates fetched entities? See MetaMapper.createOne IIRC.

-------------------------------------
Charles F. Munat<chas@...> wrote:

So can Maker now be used with something other than a String? Do you have 
an example?

This is really fascinating, but I need to get much more familiar with 
the type system before it really makes sense to me.

Thanks!

Chas.

David Pollak wrote:
> import scala.reflect.Manifest
> 
> object Maker {
>   def apply[T](params: Any*)(implicit m: Manifest[T]): T = {
>     val p2: Seq[Object] = params.flatMap {
>       case o: Object => Some(o)
>       case _ => None
>     }
> 
>     val paramClasses: Seq[Class[_]] = p2.map(_.getClass)
> 
>     m.erasure.getConstructor(paramClasses :_*).
>     newInstance(p2 :_*).asInstanceOf[T]
>   }
> 
>   def apply(clz: String, params: Any*): AnyRef = {
>     val p2: Seq[Object] = params.flatMap {
>       case o: Object => Some(o)
>       case _ => None
>     }
> 
>     val paramClasses: Seq[Class[_]] = p2.map(_.getClass)
> 
>     Class.forName(clz).getConstructor(paramClasses :_*).
>     newInstance(p2 :_*).asInstanceOf[AnyRef]
>   }
> }
> 
> println(Maker[String](Array('c','a', 't')))
> 
> println(Maker("java.lang.String", Array('d','o', 'g')))
> 
> 
> 
> On Mon, Aug 31, 2009 at 12:58 PM, Charles F. Munat <chas@... 
> <mailto:chas@...>> wrote:
> 
>     Meta model class hierarchy? (I love responses that use the word
>     "just" :-)
> 
>     Chas.
> 
> 
>     John Nilsson wrote:
> 
>         On Mon, Aug 31, 2009 at 7:05 PM, Erik
>         Engbrecht<erik.engbrecht@...
>         <mailto:erik.engbrecht@...>> wrote:
> 
>             I've been giving use cases like this some thought and I'd
>             like to toss some
>             ideas out for the community...
> 
>             In the simple case, I want to be able to write a generic
>             function (in
>             psuedo-scala):
>              (Class[T], Map[String, Sting]) => T
> 
> 
>         I fail to see what this would be good for.
>         Either you are going to write all those constructors and classes by
>         hand. In which case adding the additional code to map input to these
>         classes wouldn't be that hard.
> 
>         Or you really need dynamic objects, in which case there is no reason
>         to imagine a T just design a meta model class hierarchy.
> 
>         BR,
>         John
> 
> 
> 
> 
> 
> -- 
> Lift, the simply functional web framework http://liftweb.net
> Beginning Scala http://www.apress.com/book/view/1430219890
> Follow me: http://twitter.com/dpp
> Git some: http://github.com/dpp

Bill Venners | 1 Sep 01:53 2009

Re: How to use synchronized?

Hi Jorge,

Scala has a number of these conceptual constructs that get compiled
specially, and they are documented specially in the Scaladoc. I'd call
this a bug in that special treatment of synchronized in whatever
program generates Scaladoc for the Scala API. Conceptually,
synchronized is a method that takes a by-name parameter, so it should
really show up that way in the API docs for Scala.

Bill

On Mon, Aug 31, 2009 at 4:34 PM, Jorge Ortiz<jorge.ortiz@...> wrote:
> Scala's synchronized is a "method" in that for all intents and purposes it
> looks and acts like a method, but it is special in that the sense that it
> exists as a special form in Java, and gets compiled down to the special form
> rather than a method call. A type signature of synchronized[T0](arg0: =>
> T0): T0 would probably be less misleading, but the point is kind of moot as
> it's probably just getting translated to Java's synchronized(this) { ... }.
>
> --j
>
> On Mon, Aug 31, 2009 at 1:00 PM, Rodrigo Cano <ioniviil@...> wrote:
>>
>> Yes, I perfectly understand what synchronized is for, I had worked a lot
>> with threads in java, and know how to use it, thank you. What puzzles me is
>> that the signature for it according to the scaladoc is
>> final def synchronized[T0](arg0 : T0) : T0 So, what is T0, it doesnt look
>> like a by-name paremeter. That is why I wrote a function with similar
>> arguments (the debug function I showed you), and see that the code didn't
>> got executed within debug.
>> Happily, I already understood by-name parameters and functions in scala,
>> so that is not the problem. Thanks for the explanation anyway, but my doubt
>> still remains. What is T0, is it a function?, a by-name?, there is no
>> sourcecode for Any, so I cannot see for myself.
>>
>> Anyway, according to the examples you showed me about how to use it, I
>> suppose that it must be a by-name parameter, and it gets executed
>> synchronizedly returning after that, as in the "val x = 2 * synchronized"
>> that Naftoli Gugenheim kindly showed me.
>>
>> Thanks.
>> Rodrigo
>>
>>
>> On Mon, Aug 31, 2009 at 3:48 PM, David Flemström
>> <david.flemstrom@...> wrote:
>>>
>>> synchronized is useful when you're dealing with threading; you basically
>>> want to make sure that a method (or section of code) is called only once at
>>> any point in time, so that two threads don't try to access the method
>>> simultaneously and thereby disrupt some kind of state that the method
>>> maintains.
>>>
>>> So for example if I write:
>>>
>>> import scala.actors.Actor._
>>> def a() = {
>>> val someInt = Random.nextInt(10) //random number between 0 and 10
>>> for(i <- 0 to 2000)
>>> println("hello " + someInt) //prints 2000 times
>>> }
>>>
>>> actor {
>>> a()
>>> }
>>> actor {
>>> a()
>>> }
>>>
>>> ...then a will be called simultaneously (probably) by two threads. You
>>> might then get output like this:
>>> hello 3
>>> hello 3
>>> hello 2
>>> hello 3
>>> hello 2
>>> hello 3
>>> hello 2
>>> hello 3
>>> hello 2
>>> ...continues mixed like this exactly 4000 times
>>>
>>> However, if we set a to be synchronized, it can only be "visited" by one
>>> thread at once, so:
>>> def a() = synchronized {
>>> //...
>>> }
>>>
>>> will make the output look like this:
>>> hello 9
>>> hello 9
>>> hello 9
>>> hello 9
>>> ...continues exactly 2000 times
>>> hello 4
>>> hello 4
>>> hello 4
>>> hello 4
>>> hello 4
>>> hello 4
>>> ...continues exactly 2000 times
>>>
>>> synchronized can be very useful in methods that change some shared
>>> variables, or needs to perform operations that cannot be interrupted by
>>> other threads.
>>>
>>> On Monday 31 August 2009 20:34:56 Rodrigo Cano wrote:
>>> > Exactly that, how to use the synchronized method that all objects
>>> > inherit?
>>> > I cannot understand from its signature. The method accepts just
>>> > anything?
>>> > and returns it!?
>>> >
>>> > I tried a method test method with a similar signature to find out
>>> >
>>> > def debug[T0](arg0: T0): T0 = {
>>> > println("Arg is " + arg0.asInstanceOf[AnyRef].getClass)
>>> > arg0
>>> > }
>>> >
>>> > And tried calling it as:
>>> >
>>> > debug {
>>> > println("What im doing?")
>>> > }
>>> >
>>> > But the println("Waht im doing?") prints first, and the getClass
>>> > returns
>>> > BoxedUnit. So no idea of how synchronized actually works
>>> >
>>> > Thanks.
>>> > Rodrigo
>>>
>>> David Flemström
>>> david.flemstrom@...
>
>

--

-- 
Bill Venners
Artima, Inc.
http://www.artima.com

Dave Griffith | 1 Sep 03:07 2009
Picon

Re: How to use synchronized?


Rodrigo Cano wrote:
> 
> Yes, I perfectly understand what synchronized is for, I had worked a lot
> with threads in java, and know how to use it, thank you. 
> 

Well, no, evidently not.  

"synchronized" in Scala is purely more powerful that "synchronized" in Java. 
The T0 is a type parameter, not a by-name parameter.  It means that the
"synchronized" method in Scala can evaluate to a value (unlike the
"synchronized" statement in Java).  The value returned by the synchronized
method is whatever is returned by the argument to the synchronized method.  
In your case, that return is of type "Unit" (similar to "void" in Java)
since that's the type that the last expression in your synchronized
statement's argument has.   Most of the time, you don't have to explicitly
specify T0, since Scala's type inference will do that for you.  

Also, this would be a good place to say that you really don't want to mix
actor semantics with low-level thread-wrangling semantics like
"synchronized" (or "wait", or "notify").  Do that naively, and the best you
can hope for is an ugly deadlock.  After that things get more difficult to
diagnose.

In general, anything you see in Scala that looks similar to something in
Java will be just similar enough to tweak your intuition, but not similar
enough for you to actually understand it fully.  

--Dave Griffith

--

-- 
View this message in context: http://www.nabble.com/How-to-use-synchronized--tp25228291p25232855.html
Sent from the Scala - User mailing list archive at Nabble.com.

Bill Venners | 1 Sep 03:20 2009

Re: How to use synchronized?

Hi Dave,

I think arg0 is what we're saying should be a by-name parameter, not
T0. Instead of:

def synchronized[T0](arg0: T0): T0 = arg0

it should probably be:

def synchronized[T0](arg0: => T0): T0 = arg0

Because if it isn't a by-name, how could this conceptual synchronized
method actually acquire the lock before executing the code that needs
to be synchronized.

Bill

On Mon, Aug 31, 2009 at 6:07 PM, Dave
Griffith<dave.l.griffith@...> wrote:
>
>
>
> Rodrigo Cano wrote:
>>
>> Yes, I perfectly understand what synchronized is for, I had worked a lot
>> with threads in java, and know how to use it, thank you.
>>
>
> Well, no, evidently not.
>
> "synchronized" in Scala is purely more powerful that "synchronized" in Java.
> The T0 is a type parameter, not a by-name parameter.  It means that the
> "synchronized" method in Scala can evaluate to a value (unlike the
> "synchronized" statement in Java).  The value returned by the synchronized
> method is whatever is returned by the argument to the synchronized method.
> In your case, that return is of type "Unit" (similar to "void" in Java)
> since that's the type that the last expression in your synchronized
> statement's argument has.   Most of the time, you don't have to explicitly
> specify T0, since Scala's type inference will do that for you.
>
> Also, this would be a good place to say that you really don't want to mix
> actor semantics with low-level thread-wrangling semantics like
> "synchronized" (or "wait", or "notify").  Do that naively, and the best you
> can hope for is an ugly deadlock.  After that things get more difficult to
> diagnose.
>
> In general, anything you see in Scala that looks similar to something in
> Java will be just similar enough to tweak your intuition, but not similar
> enough for you to actually understand it fully.
>
> --Dave Griffith
>
> --
> View this message in context: http://www.nabble.com/How-to-use-synchronized--tp25228291p25232855.html
> Sent from the Scala - User mailing list archive at Nabble.com.
>
>

--

-- 
Bill Venners
Artima, Inc.
http://www.artima.com

Rex Kerr | 1 Sep 03:29 2009
Picon

Re: Type promotion for binary operators?

Incidentally, how do the scala collections do the type-preservation with map and so on without importing Map._ and such where the implicit BuilderFactories are declared?

Also, does anyone know of a minimal example of preserving types of containers when using non-overridden superclass methods?  The whole set of Companion, Builder, BuilderFactory, XTemplate, XClass, XFactory, and X is a lot of overhead, and I think I'm still missing something subtle about how it all works.

  --Rex

Rodrigo Cano | 1 Sep 03:35 2009
Picon

Re: How to use synchronized?

Exactly what Bill is pointing is the problem Dave. Everyone that wrote here was conscious about the fact that synchronized didn't just accept a by-name, that was implicit in T0 no beign a arg0: => T0.
Anyway, the real question is: if I for instance write

val myMethodResult = synchronized(myMethod())

What would happen?
would myMethod be call first, its result be passed to the synchronized and then, the synchronized would give me the result like any other method with the same signature, resulting in nothing useful?
or will it execute myMethod in a synchronized way?. If the second is to happend, how would the method know if it's not a function, nor partial, nor by-name how to "call" my method. We believe that it cannot (unless it breaks with scala syntax), and hence infer that you should pass a by-name as argument as Jorge Ortiz states. But all these is speculation.

On Mon, Aug 31, 2009 at 10:20 PM, Bill Venners <bill-7L19218AtABhl2p70BpVqQ@public.gmane.orgm> wrote:
Hi Dave,

I think arg0 is what we're saying should be a by-name parameter, not
T0. Instead of:

def synchronized[T0](arg0: T0): T0 = arg0

it should probably be:

def synchronized[T0](arg0: => T0): T0 = arg0

Because if it isn't a by-name, how could this conceptual synchronized
method actually acquire the lock before executing the code that needs
to be synchronized.

Bill

On Mon, Aug 31, 2009 at 6:07 PM, Dave
Griffith<dave.l.griffith <at> gmail.com> wrote:
>
>
>
> Rodrigo Cano wrote:
>>
>> Yes, I perfectly understand what synchronized is for, I had worked a lot
>> with threads in java, and know how to use it, thank you.
>>
>
> Well, no, evidently not.
>
> "synchronized" in Scala is purely more powerful that "synchronized" in Java.
> The T0 is a type parameter, not a by-name parameter.  It means that the
> "synchronized" method in Scala can evaluate to a value (unlike the
> "synchronized" statement in Java).  The value returned by the synchronized
> method is whatever is returned by the argument to the synchronized method.
> In your case, that return is of type "Unit" (similar to "void" in Java)
> since that's the type that the last expression in your synchronized
> statement's argument has.   Most of the time, you don't have to explicitly
> specify T0, since Scala's type inference will do that for you.
>
> Also, this would be a good place to say that you really don't want to mix
> actor semantics with low-level thread-wrangling semantics like
> "synchronized" (or "wait", or "notify").  Do that naively, and the best you
> can hope for is an ugly deadlock.  After that things get more difficult to
> diagnose.
>
> In general, anything you see in Scala that looks similar to something in
> Java will be just similar enough to tweak your intuition, but not similar
> enough for you to actually understand it fully.
>
> --Dave Griffith
>
> --
> View this message in context: http://www.nabble.com/How-to-use-synchronized--tp25228291p25232855.html
> Sent from the Scala - User mailing list archive at Nabble.com.
>
>



--
Bill Venners
Artima, Inc.
http://www.artima.com

Kevin Weil | 1 Sep 03:33 2009

Scala/Hadoop function override question

Hi all,

I'm very new to Scala, and have been learning it by implementing some simple Hadoop jobs with it.  Turns out it wasn't the simplest way to learn the language, since it involves Java/Scala interop with reflection and template class parameters, but oh well.  The following describes the compile error I'm seeing when compiling against the Hadoop (0.20) jar.

I'm overriding the Hadoop mapper, which is the following object in pure Java:

public class Mapper<KEYIN, VALUEIN, KEYOUT, VALUEOUT> {

  public class Context
    extends MapContext<KEYIN,VALUEIN,KEYOUT,VALUEOUT> {
    public Context(Configuration conf, TaskAttemptID taskid,
                   RecordReader<KEYIN,VALUEIN> reader,
                   RecordWriter<KEYOUT,VALUEOUT> writer,
                   OutputCommitter committer,
                   StatusReporter reporter,
                   InputSplit split) throws IOException, InterruptedException { ... }
  }

  <at> SuppressWarnings("unchecked")
  protected void map(KEYIN key, VALUEIN value,
                     Context context) throws IOException, InterruptedException { ... }
...
}

I'm attempting to override the map() method with my own tiny object:

class WordCountMapper extends Mapper[LongWritable, Text, Text, LongWritable] {   
    override def map(key: LongWritable, value: Text, context: Context): Unit = { ... }
}

If I leave off the override, everything compiles and it calls into the base Hadoop mapper class which I listed above.  When I add override in, which seems the right thing to do, I get the following compile error:

compile-scala:
   [scalac] Compiling 1 source file to /Users/kevin/Code/twitter/twadoop/target/classes
   [scalac] /Users/kevin/Code/twitter/twadoop/src/main/scala/com/twitter/twadoop/ScalaWordCount2.scala:50: error: method map overrides nothing
   [scalac]     protected override def map(key: LongWritable, value: Text, context: Context): Unit = {
   [scalac]                            ^
   [scalac] one error found

So something in the parameter list isn't matching up like it should.  But I have the argument list right as far as I can tell.  I polled a couple people much more knowledgeable about Scala than I, and we tried futzing with imports, removing the Unit return value, adding "protected", adding <at> throws(classOf[IOException]) declarations, etc.  Nothing could convince scalac that my map() method really was the same as the one I'm trying to override in Hadoop's Mapper class.  Any ideas?  I've pasted the equivalent Java code (which does work) at the bottom in case that helps.

Thanks in advance,
Kevin

public static class WordCountMapper extends Mapper<LongWritable, Text, Text, LongWritable> {
 
        <at> Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException { ... }
}



David Pollak | 1 Sep 04:11 2009
Picon

Re: How to use synchronized?

Or we can see what bytecode is generated:

class Sync {
  private[this] var x = 1
  def bar: Int = synchronized {
    x += 1
    x
    }
}

Becomes:

public class Sync extends java.lang.Object implements scala.ScalaObject{
public Sync();
  Code:
   0: aload_0
   1: iconst_1
   2: putfield #12; //Field x:I
   5: aload_0
   6: invokespecial #16; //Method java/lang/Object."<init>":()V
   9: return

public int bar();
  Code:
   0: aload_0
   1: dup
   2: astore_1
   3: monitorenter
   4: aload_0
   5: aload_0
   6: getfield #12; //Field x:I
   9: iconst_1
   10: iadd
   11: putfield #12; //Field x:I
   14: aload_0
   15: getfield #12; //Field x:I
   18: invokestatic #24; //Method scala/runtime/BoxesRunTime.boxToInteger:(I)Ljava/lang/Integer;
   21: aload_1
   22: monitorexit
   23: invokestatic #28; //Method scala/runtime/BoxesRunTime.unboxToInt:(Ljava/lang/Object;)I
   26: ireturn
   27: aload_1
   28: monitorexit
   29: athrow
  Exception table:
   from   to  target type
     4    23    27   any

public int $tag()   throws java.rmi.RemoteException;
  Code:
   0: aload_0
   1: invokestatic #34; //Method scala/ScalaObject$class.$tag:(Lscala/ScalaObject;)I
   4: ireturn

}

So, it looks like it should be documented as call-by-name.  It also looks like it does what one expects is to.

Thanks,

David

On Mon, Aug 31, 2009 at 6:35 PM, Rodrigo Cano <ioniviil-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
Exactly what Bill is pointing is the problem Dave. Everyone that wrote here was conscious about the fact that synchronized didn't just accept a by-name, that was implicit in T0 no beign a arg0: => T0.
Anyway, the real question is: if I for instance write

val myMethodResult = synchronized(myMethod())

What would happen?
would myMethod be call first, its result be passed to the synchronized and then, the synchronized would give me the result like any other method with the same signature, resulting in nothing useful?
or will it execute myMethod in a synchronized way?. If the second is to happend, how would the method know if it's not a function, nor partial, nor by-name how to "call" my method. We believe that it cannot (unless it breaks with scala syntax), and hence infer that you should pass a by-name as argument as Jorge Ortiz states. But all these is speculation.


On Mon, Aug 31, 2009 at 10:20 PM, Bill Venners <bill-7L19218AtADQT0dZR+AlfA@public.gmane.org> wrote:
Hi Dave,

I think arg0 is what we're saying should be a by-name parameter, not
T0. Instead of:

def synchronized[T0](arg0: T0): T0 = arg0

it should probably be:

def synchronized[T0](arg0: => T0): T0 = arg0

Because if it isn't a by-name, how could this conceptual synchronized
method actually acquire the lock before executing the code that needs
to be synchronized.

Bill

On Mon, Aug 31, 2009 at 6:07 PM, Dave
Griffith<dave.l.griffith-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
>
>
>
> Rodrigo Cano wrote:
>>
>> Yes, I perfectly understand what synchronized is for, I had worked a lot
>> with threads in java, and know how to use it, thank you.
>>
>
> Well, no, evidently not.
>
> "synchronized" in Scala is purely more powerful that "synchronized" in Java.
> The T0 is a type parameter, not a by-name parameter.  It means that the
> "synchronized" method in Scala can evaluate to a value (unlike the
> "synchronized" statement in Java).  The value returned by the synchronized
> method is whatever is returned by the argument to the synchronized method.
> In your case, that return is of type "Unit" (similar to "void" in Java)
> since that's the type that the last expression in your synchronized
> statement's argument has.   Most of the time, you don't have to explicitly
> specify T0, since Scala's type inference will do that for you.
>
> Also, this would be a good place to say that you really don't want to mix
> actor semantics with low-level thread-wrangling semantics like
> "synchronized" (or "wait", or "notify").  Do that naively, and the best you
> can hope for is an ugly deadlock.  After that things get more difficult to
> diagnose.
>
> In general, anything you see in Scala that looks similar to something in
> Java will be just similar enough to tweak your intuition, but not similar
> enough for you to actually understand it fully.
>
> --Dave Griffith
>
> --
> View this message in context: http://www.nabble.com/How-to-use-synchronized--tp25228291p25232855.html
> Sent from the Scala - User mailing list archive at Nabble.com.
>
>



--
Bill Venners
Artima, Inc.
http://www.artima.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
Git some: http://github.com/dpp

Gmane