Naftoli Gugenheim | 1 Nov 2009 03:38
Picon
Gravatar

Re: Question about Nil and pattern matching

On Fri, Oct 30, 2009 at 11:56 PM, jlist9 <jlist9-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
Hi, I'm having a question with Nil and pattern matching.
The following code works:

val list = List()
list match {
 case Nil => "empty list"
 case x :: xs => "head:" + x + ", tail:" + xs
}

But if I replace List() with Nil, I get error:
constructor cannot be instantiated to expected type; found : ::[B]
required: object Nil

It looks like Nil is not equivalent to List(). If not, how come Nil
matches List()?
 
If by the last line you were referring to
Nil match {
  case List() => ...
}
then it may be worth mentioning that "List()" can mean one of two things. Usually it is syntactic sugar for List.apply() where List is the List singleton object (in this case not inherently related to the List class). List.apply() returns Nil, so List() eq Nil.
On the other hand when the same syntax -- List() -- appears in a case pattern match statement: an extractor, it is syntactic sugar for List.unapply, or in this case List.unapplySeq (if I'm not mistaken), which is a comparison function. List() will pattern match Nil; List(x) will pattern match a single-element List and bind its element to x; List(X) or List(`x`) will patter match on a list containing exactly X or x respectively. Similar for more elements. List(x <at> _*) will bind x to all of the list's 0 or more elements.


Ray Racine | 1 Nov 2009 04:31
Picon
Gravatar

Scala Software Contract Position

Apologies for off topic abusing of the Scala mailing list, but a Scala employment opportunity is probably still of general interest to the community.

Office Depot, in Boca Raton FL, has a need for an experienced software designer/developer with strong Scala skills to join a small 3-5 person team.  Candidate must be able to commit to 20-40 hours per week as a contractor.  Minimum 8-12 weeks engagement, which may extend up to a year.  Remote working (telecommute) is acceptable, or you could spend the winter in South Florida.

The application is a stateless, shared nothing architecture, high transaction (millions of calls a day), scalable, concurrent, highly available system.  Significant work has already been done and candidate will join the project deep in the development phase.

Ideal candidate:
 - Strong Scala language knowledge, actors and concurrency. We are developing against Scala 2.8, Java 1.6.  Yea, we fly pretty close to the sun.
 - CS, Math, Science degree or equivalent work experience.
 - Strong development / coding skills.
 - General experience in software development methodology and delivering a software project.
 - Use of version control, GIT.
 - Appropriate use of unit and regression testing.
 - Good knowledge of the HTTP protocol, JSON, REST.
 - Bonus experience, front end experience with Yahoo YUI, Javascript, AJAX or Lift
 - No SQL, RDB or ORM frameworks involved.

Sure the usual dull web stuff, forms and CRUD functionality, but there are also interesting project aspects such as:
 - Emphasized use of Algorithms, with research, reading, comprehending and implementing algorithms from papers.  Google Scholar is your friend.
 - Multi-Paxos algorithm for distributed consensus also Paxos-Commit, and Paxos-Lease.
 - UDP multicast (IGMP)
 - KV Storage via Cassandra
 - Emphasized use of open source solutions, with aspects of the project itself eventually open sourced.

Interested?  Please contact me with a light weight resume or CV.  Please highlight any direct Scala experience with proprietary or open source software.

Ray Racine

--
The object of life is not to be on the side of the majority, but to escape finding oneself in the ranks of the insane. - Marcus Aurelius
Lachlan Cotter | 1 Nov 2009 04:46
Picon
Gravatar

How to find a type parameter at runtime? (Generics / Manifest)


Hi,

I have a generic wrapper type Foo[X].

For a given instance of Foo[X] I want get the class of X. I have to be able to do this without actually examining the wrapped value because it might be null.

For example:

val x = new Foo[String]()
x.wrappedType // Class[String]

From what I’ve read, I understand that this is what Manifests might be good for, but after re-reading all the info I can find on them several times, I’m still hitting walls.

Is what I’m trying to do even possible?



Cheers,
Lach
Paul Phillips | 1 Nov 2009 05:30

Re: How to find a type parameter at runtime? (Generics / Manifest)

On Sun, Nov 01, 2009 at 02:46:39PM +1100, Lachlan Cotter wrote:
> I have a generic wrapper type Foo[X].
> 
> For a given instance of Foo[X] I want get the class of X.

We really need a decent faq going somewhere.  (Not a criticism of you, a 
general plea for it to exist.) This is 2.8:

scala> class Foo[X](implicit val m: Manifest[X])     
defined class Foo

scala> new Foo[String]() m  
res0: Manifest[String] = java.lang.String

scala> res0.erasure
res1: java.lang.Class[_] = class java.lang.String

--

-- 
Paul Phillips      | Those who can make you believe absurdities
Stickler           | can make you commit atrocities.
Empiricist         |     -- Voltaire
slap pi uphill!    |----------* http://www.improving.org/paulp/ *----------

Naftoli Gugenheim | 1 Nov 2009 05:41
Picon
Gravatar

Re: How to find a type parameter at runtime? (Generics / Manifest)

I think he wants res0.typeArguments.

On Sun, Nov 1, 2009 at 12:30 AM, Paul Phillips <paulp-v5eHc9rg9U0h9ZMKESR00Q@public.gmane.org> wrote:
On Sun, Nov 01, 2009 at 02:46:39PM +1100, Lachlan Cotter wrote:
> I have a generic wrapper type Foo[X].
>
> For a given instance of Foo[X] I want get the class of X.

We really need a decent faq going somewhere.  (Not a criticism of you, a
general plea for it to exist.) This is 2.8:

scala> class Foo[X](implicit val m: Manifest[X])
defined class Foo

scala> new Foo[String]() m
res0: Manifest[String] = java.lang.String

scala> res0.erasure
res1: java.lang.Class[_] = class java.lang.String

--
Paul Phillips      | Those who can make you believe absurdities
Stickler           | can make you commit atrocities.
Empiricist         |     -- Voltaire
slap pi uphill!    |----------* http://www.improving.org/paulp/ *----------

Paul Phillips | 1 Nov 2009 05:50

Re: How to find a type parameter at runtime? (Generics / Manifest)

On Sun, Nov 01, 2009 at 12:41:34AM -0400, Naftoli Gugenheim wrote:
> I think he wants res0.typeArguments.

Really? He wanted Nil? I could have saved him some trouble then.

--

-- 
Paul Phillips      | Before a man speaks it is always safe to assume
Future Perfect     | that he is a fool.  After he speaks, it is seldom
Empiricist         | necessary to assume it. 
pull his pi pal!   |     -- H. L. Mencken

Naftoli Gugenheim | 1 Nov 2009 05:58
Picon
Gravatar

Re: How to find a type parameter at runtime? (Generics / Manifest)

Never mind, I reread his post. Don't ask why I thought Foo[String] was the type parameter to something else.

On Sun, Nov 1, 2009 at 12:50 AM, Paul Phillips <paulp-v5eHc9rg9U0h9ZMKESR00Q@public.gmane.org> wrote:
On Sun, Nov 01, 2009 at 12:41:34AM -0400, Naftoli Gugenheim wrote:
> I think he wants res0.typeArguments.

Really? He wanted Nil? I could have saved him some trouble then.

--
Paul Phillips      | Before a man speaks it is always safe to assume
Future Perfect     | that he is a fool.  After he speaks, it is seldom
Empiricist         | necessary to assume it.
pull his pi pal!   |     -- H. L. Mencken

Andrew Gaydenko | 1 Nov 2009 11:52

Re: Re: Question about overloaded methods

On Friday 30 October 2009 11:47:49 Jesper Nordenberg wrote:
> Or you can use variants of the visitor pattern, see:
> 
> http://www.scala-lang.org/docu/files/IC_TECH_REPORT_200433.pdf

Jesper,

Have you tried to translate a code from this article to current Scala?
Extensions like this one

class Num(v: Int) extends super[ShowPlusNeg].Num(v) with 
super[DblePlusNeg].Num(v) with Exp

don't work, of course.

Lachlan Cotter | 1 Nov 2009 12:46
Picon
Gravatar

Re: How to find a type parameter at runtime? (Generics / Manifest)

Thanks for your speedy response Paul,

This looks encouraging.

I had actually tried using that syntax for the Manifest with 2.7.7 but the compiler complained that there was no implicit argument in scope:

no implicit argument matching parameter type scala.reflect.Manifest[T] was found

Does it work with the current stable release or do I need 2.8?



Cheers,
Lach




On 01/11/2009, at 3:30 PM, Paul Phillips wrote:

On Sun, Nov 01, 2009 at 02:46:39PM +1100, Lachlan Cotter wrote:
I have a generic wrapper type Foo[X].

For a given instance of Foo[X] I want get the class of X.

We really need a decent faq going somewhere.  (Not a criticism of you, a
general plea for it to exist.) This is 2.8:

scala> class Foo[X](implicit val m: Manifest[X])     
defined class Foo

scala> new Foo[String]() m  
res0: Manifest[String] = java.lang.String

scala> res0.erasure
res1: java.lang.Class[_] = class java.lang.String

--
Paul Phillips      | Those who can make you believe absurdities
Stickler           | can make you commit atrocities.
Empiricist         |     -- Voltaire
slap pi uphill!    |----------* http://www.improving.org/paulp/ *----------

Germán Ferrari | 1 Nov 2009 16:11
Picon
Gravatar

Re: How to find a type parameter at runtime? (Generics / Manifest)

Hi, 


As far I know, this is only available in scala 2.8 

Regards, 
Germán.


On Sun, Nov 1, 2009 at 9:46 AM, Lachlan Cotter <lach-K78Hl1KGfC8gBc27wqDAHg@public.gmane.org> wrote:
Thanks for your speedy response Paul,

This looks encouraging.

I had actually tried using that syntax for the Manifest with 2.7.7 but the compiler complained that there was no implicit argument in scope:

no implicit argument matching parameter type scala.reflect.Manifest[T] was found

Does it work with the current stable release or do I need 2.8?



Cheers,
Lach




On 01/11/2009, at 3:30 PM, Paul Phillips wrote:

On Sun, Nov 01, 2009 at 02:46:39PM +1100, Lachlan Cotter wrote:
I have a generic wrapper type Foo[X].

For a given instance of Foo[X] I want get the class of X.

We really need a decent faq going somewhere.  (Not a criticism of you, a
general plea for it to exist.) This is 2.8:

scala> class Foo[X](implicit val m: Manifest[X])     
defined class Foo

scala> new Foo[String]() m  
res0: Manifest[String] = java.lang.String

scala> res0.erasure
res1: java.lang.Class[_] = class java.lang.String

--
Paul Phillips      | Those who can make you believe absurdities
Stickler           | can make you commit atrocities.
Empiricist         |     -- Voltaire
slap pi uphill!    |----------* http://www.improving.org/paulp/ *----------



Gmane