Ismael Juma | 1 Dec 01:18 2008
Picon

Re: How to set the scale for scala.BigDecimal's / method?

Bill Venners <bill <at> ...> writes:
> I'm not sure how many APIs take java.lang.Iterable, or therefore how
> helpful being able to pass a Scala Iterable around as a
> java.lang.Iterable would be.

I think this is partly because java.lang.Iterable was only introduced in
Java 5. Google Collections use Iterables/Iterators quite a bit, for example
(they even have a FAQ about it[1]).

[...]
> I've never needed to use scala.collection.jcl, but my theory has
> always been that this was its intended purpose. It also has implicit
> conversions, but is too specific in the "unconvert" direction.

Agreed. Miles Sabin had created a nice set of classes that would work for
general scala collections too.

> If people actually need conversion between Scala and Java Iterator,
> and/or Scala and Java Iterable, then methods could be added here that
> do it, but as I mentioned I'm not sure how often this would be needed.

They are needed as far as I am concerned, but my preference is also for
conversions instead of having j.l.Iterable as a superclass. I understand that
others disagree with this though and I have seen it used as a reason why Scala
provides worse interoperability with Java than Clojure or Groovy.

Ismael

[1] http://code.google.com/p/google-collections/wiki/Faq (search for "Why so
much emphasis on Iterators and Iterables?").
(Continue reading)

Oscar Picasso | 1 Dec 03:35 2008
Picon

GridBagPanel

As anyone an example of how to use the scala.swing.GridBagPanel?

It's not obvious from the scaladocs.

vkelman | 1 Dec 04:20 2008
Picon

Re: Repeated parameters: why are they designated by `*` and where `: _*` came from?


BTW, David Flanagan in his "JavaScript: The definite guide" has a nice
example of using Function.apply method and closures to implement partially
applied functions in JavaScript:

// Return a function that invokes the function f with the
// specified arguments and also any additional arguments that are
// passed to the returned function. (This is sometimes called "currying".)
function bindArguments(/* function */ f /*, initial arguments... */) {
    var boundArgs = arguments;
    return function() {
        // Build up an array of arguments.  It starts with the previously
        // bound arguments and is extended with the arguments passed now
        var args = [];
        for(var i = 1; i < boundArgs.length; i++) args.push(boundArgs[i]);
        for(var i = 0; i < arguments.length; i++) args.push(arguments[i]);

        // Now invoke the function with these arguments
        return f.apply(this, args);
    }
}

Really, JavaScript is very powerful - even allowing to program in functional
style, - although sometimes hard to understand and to debug.

-----
Vladimir Kelman
http://pro-thoughts.blogspot.com/
--

-- 
View this message in context: http://www.nabble.com/Repeated-parameters%3A-why-are-they-designated-by-%60*%60-and-where-%60%3A-_*%60-came-from--tp20741822p20765489.html
Sent from the Scala - User mailing list archive at Nabble.com.

Charles F. Munat | 1 Dec 07:58 2008

Creating objects programmatically

If I have a method and I want to return a new object but I won't know 
until runtime what that object will be, how do I do that?

In other words, if I have

def getOne[A] = {
   new ???
}

how do I create a new instance of class A?

Is it possible?

Thanks,
Chas. Munat
Seattle

Bill Venners | 1 Dec 08:57 2008

Re: Creating objects programmatically

Hi Charles,

On Sun, Nov 30, 2008 at 10:58 PM, Charles F. Munat <chas@...> wrote:
> If I have a method and I want to return a new object but I won't know until
> runtime what that object will be, how do I do that?
>
> In other words, if I have
>
> def getOne[A] = {
>  new ???
> }
>
> how do I create a new instance of class A?
>
In 2.7.2 you can do it this by passing an implicit manifest, and use
that to get a Class instance, then call newInstance on that:

scala> import scala.reflect.Manifest
import scala.reflect.Manifest

scala> def getOne[A](implicit manifest: Manifest[A]): A = {
     |   val clazz = manifest.erasure.asInstanceOf[Class[A]]
     |   clazz.newInstance.asInstanceOf[A]
     | }
getOne: [A](implicit scala.reflect.Manifest[A])A

scala> getOne[String]
res0: String =

scala> class Bob
defined class Bob

scala> getOne[Bob]
res1: Bob = Bob <at> c246b0

It will only work if the specified class has a no-arg constructor, is
non-abstract, etc. It's a bit unusual, though. It makes me wonder if
there's a more type safe way to accomplish what you're actually trying
to accomplish.

Bill

Atsuhiko Yamanaka | 1 Dec 09:07 2008
Picon

Re: How to set the scale for scala.BigDecimal's / method?

Hi,

2008/11/29 martin odersky <martin.odersky@...>:
>> IMHO, current scala.BigDecimal is not functional for the practical usage due
>> to a lack of scale settings for "divide".
>>
> AFAIK BigDecimal has currently no active maintainer. We'll see whether
> somebody would like to do a new design based on MathContext.

If nobody would like to do, I can if required.

By the way, I have a question.
Is there a possibility to modify(or improve ?) the scalac compiler to
intelligently choose
one of following methods according to the context?

>>    def / (that: BigDecimal): BigDecimal
>>    def / (that: BigDecimal)(implicit mc:java.math.MathContext): BigDecimal

If above methods are added to scala.BigDecimal,
the current compiler will fail with following messages,

 error: ambiguous reference to overloaded definition,
 both method / in class BigDeicmal of type (BigDecimal)(implicit
java.math.MathContext)BigDecimal
 and  method / in class BigDecimal of type (BigDecimal)BigDecimal

for the following code,

 BigDecimal(6)/BigDecimal(3)

 {
   implict val mc=new MathContext(100)
   BigDecimal(1)/BigDecimal(3)
 }

As for the second case, both methods can be applicable, but user will
clealy expect the
divide with MathContext.

Sincerely,
--
Atsuhiko Yamanaka
JCraft,Inc.
1-14-20 HONCHO AOBA-KU,
SENDAI, MIYAGI 980-0014 Japan.
Tel +81-22-723-2150
    +1-415-578-3454
Fax +81-22-224-8773
Skype callto://jcraft/

Luc Duponcheel | 1 Dec 09:18 2008
Picon

Re: Creating objects programmatically

Hi Bill (and others),

a few questions:

(1)
I do not see scala.reflect.Manifest in the Scala 2.7.2.final API documentation.
Does this mean that Manifest more like a language thing than a library thing?
I would doubt ...

(2)
In which way would you like getOne to be more type safe?

(3)
Maybe it would be nice to have a more flexible (and still relatively type safe)
version of getOne that could be used as: getOne["String"] and getOne["Bob"]


... just some thoughts ...

Luc

On Mon, Dec 1, 2008 at 8:57 AM, Bill Venners <bill-7L19218AtADQT0dZR+AlfA@public.gmane.org> wrote:
Hi Charles,

On Sun, Nov 30, 2008 at 10:58 PM, Charles F. Munat <chas-QXwrqIsfxigAvxtiuMwx3w@public.gmane.org> wrote:
> If I have a method and I want to return a new object but I won't know until
> runtime what that object will be, how do I do that?
>
> In other words, if I have
>
> def getOne[A] = {
>  new ???
> }
>
> how do I create a new instance of class A?
>
In 2.7.2 you can do it this by passing an implicit manifest, and use
that to get a Class instance, then call newInstance on that:

scala> import scala.reflect.Manifest
import scala.reflect.Manifest

scala> def getOne[A](implicit manifest: Manifest[A]): A = {
    |   val clazz = manifest.erasure.asInstanceOf[Class[A]]
    |   clazz.newInstance.asInstanceOf[A]
    | }
getOne: [A](implicit scala.reflect.Manifest[A])A

scala> getOne[String]
res0: String =

scala> class Bob
defined class Bob

scala> getOne[Bob]
res1: Bob = Bob <at> c246b0

It will only work if the specified class has a no-arg constructor, is
non-abstract, etc. It's a bit unusual, though. It makes me wonder if
there's a more type safe way to accomplish what you're actually trying
to accomplish.

Bill



--
  __~O
 -\ <,
(*)/ (*)

reality goes far beyond imagination

     (\__/)
    (='.'=)
     (")_(")

Jorge Ortiz | 1 Dec 09:27 2008
Picon

Re: Creating objects programmatically


(1)
I do not see scala.reflect.Manifest in the Scala 2.7.2.final API documentation.
Does this mean that Manifest more like a language thing than a library thing?
I would doubt ...

As I understand it, it's experimental hence undocumented. Don't use it unless you're willing to live with possible bugginess and future backwards incompatibility.

--j
Ingo Maier | 1 Dec 09:36 2008
Picon
Picon

Re: GridBagPanel

See here:

https://lampsvn.epfl.ch/trac/scala/browser/scala/trunk/src/swing/scala/swing/test/GridBagDemo.scala

Oscar Picasso wrote:
> As anyone an example of how to use the scala.swing.GridBagPanel?
> 
> It's not obvious from the scaladocs.

Bill Venners | 1 Dec 09:40 2008

Re: Creating objects programmatically

Hi All,

I didn't realize it was experimental. Another way to do that may be
more future proof is:

scala> def getOne[A](clazz: Class[A]): A = clazz.newInstance.asInstanceOf[A]
getOne: [A](Class[A])A

scala> getOne(classOf[String])
res2: String =

scala> getOne(classOf[Bob])
res3: Bob = Bob <at> fa8cbc

Bill

On Mon, Dec 1, 2008 at 12:27 AM, Jorge Ortiz <jorge.ortiz@...> wrote:
>
>> (1)
>> I do not see scala.reflect.Manifest in the Scala 2.7.2.final API
>> documentation.
>> Does this mean that Manifest more like a language thing than a library
>> thing?
>> I would doubt ...
>
> As I understand it, it's experimental hence undocumented. Don't use it
> unless you're willing to live with possible bugginess and future backwards
> incompatibility.
>
> --j
>


Gmane