Daniel Kröni | 1 Oct 08:32 2008
Picon

type depending on constructor argument without cast

Hi all,

I have a class C whose type should depend on the type of its
constructor argument. In my example: If I create a new C with an attr
where (attr.isInstanceOf[T] == true) then the new instance of C
should also be mixed with T, otherwise not. The solution I found
utilizes a factory method and a cast. I want to avoid the cast and if
possible also the factory method.

Maybe dependent types solve my problem?

Thanks
daniel

abstract class Super {
 def m() : Unit = {}
}

trait T {
 this : Super => //always mixed with Super
 def n() : Unit = { m() }
}

class A extends Super {}

class B extends Super with T {}

// this class should be mixed with T if attr is mixed with T
class C(attr : Super) extends Super {}

(Continue reading)

Geoffrey Alan Washburn | 1 Oct 09:21 2008
Picon
Picon

Re: type depending on constructor argument without cast

Daniel Kröni wrote:

> Maybe dependent types solve my problem?

Yes, but it would require a much more expressive dependent type system 
than Scala currently provides.

David MacIver | 1 Oct 14:50 2008
Picon

Re: Re: Unreachable code in match

On Tue, Sep 30, 2008 at 10:59 PM, Geoffrey Alan Washburn
<geoffrey.washburn@...> wrote:
> David MacIver wrote:
>>
>> On Tue, Sep 30, 2008 at 5:37 PM, Geoffrey Alan Washburn
>> <geoffrey.washburn@...> wrote:
>>>
>>> Alex Boisvert wrote:
>>>>
>>>> Is there a plan to enhance/integrate pattern matching with the new
>>>> Manifest[T] so it's possible to match against type parameters?
>>>
>>> No, at least not a fashion that is transparent.  Certainly if you have a
>>> manifest for the type parameter you wish to match upon, you can match
>>> upon
>>> the manifest to obtain the desired behavior.
>>
>> Hm. How would one go about matching on manifests? They don't appear to
>> offer any mechanism for doing so.
>
> Yes, at the moment they do not really have much structure.  But they are
> still very much a work in progress.  Eventually there will be a number of
> case objects and case classes representing the different sorts of types
> found in Scala.  Unfortunately, Scala's type system is not expressive enough
> to make pattern matching on these types the yield type refinements as
> precise as you might get for GADTs representing types in a language with
> purely structural typing.
>
> In the meantime, matching on simple types can be done matching on a
> manifest's erasure.
(Continue reading)

James Iry | 1 Oct 16:27 2008
Picon

Re: type depending on constructor argument without cast

Here's one approach to the problem using implicits and type parameters to roll your own "dependent typing."

abstract class Super {
 def m() : Unit = {}
}

trait T {
 this : Super => //always mixed with Super
 def n() : Unit = { m() }
}

class A extends Super {}

class B extends Super with T {}

class C[+A <: Super](attr : A) extends Super {}

object C {
  implicit def cT2T(c:C[Super with T]) = new Super with T {
    override def m() = c.m 
  }
}

object Run extends Application {
  import C._
 
 val a = new A
 val b = new B

 val cWithoutT = new C(a)

//  cWithoutT.n  // won't compile

 
 val cWithT = new C(b)

 cWithT.n // works just fine
}

On Tue, Sep 30, 2008 at 11:32 PM, Daniel Kröni <daniel.kroeni-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
Hi all,

I have a class C whose type should depend on the type of its
constructor argument. In my example: If I create a new C with an attr
where (attr.isInstanceOf[T] == true) then the new instance of C
should also be mixed with T, otherwise not. The solution I found
utilizes a factory method and a cast. I want to avoid the cast and if
possible also the factory method.

Maybe dependent types solve my problem?

Thanks
daniel

abstract class Super {
 def m() : Unit = {}
}

trait T {
 this : Super => //always mixed with Super
 def n() : Unit = { m() }
}

class A extends Super {}

class B extends Super with T {}

// this class should be mixed with T if attr is mixed with T
class C(attr : Super) extends Super {}

object C {
 //Factory method
 def create(attr : Super) : C = {
  if(attr.isInstanceOf[T]){ return new C(attr) with T }
  else { return new C(attr) }
 }
}

object Run extends Application {
 val a = new A
 val b = new B

 val cWithoutT = C.create(a);
 cWithoutT.m

 val cWithT = C.create(b);
 // this works
 cWithT.asInstanceOf[Super with T].n;

 // but this is what I want
 cWithT.n
}

Daniel Kröni | 1 Oct 19:10 2008
Picon

Re: type depending on constructor argument without cast

Thanks for your replies. I first had to read chapter 21 to understand
how the code using the implicits is working. On a first glance it
looks like a solution for my problem, but the following does not
compile:

object Run extends Application {
  import C._

 val a = new A
 val b = new B

 val cWithoutT = new C(a)
//  cWithoutT.n  // won't compile

 val cWithT = new C(b)
 cWithT.n // works just fine

// !! here comes the problem !! //

 val otherCWithT = new C(cWithT)
 otherCWithT.n // won't compile
}

And a few minutes later I found that I have to write it like this:

 val cWithT : Super with T = new C(b)  //explicit type declaration
 cWithT.n // works just fine

 val otherCWithT = new C(cWithT)
 otherCWithT.n // works as well !

For my purpose this is good enough. Thanks again!
Daniel

On Wed, Oct 1, 2008 at 4:27 PM, James Iry <jamesiry <at> gmail.com> wrote:
> Here's one approach to the problem using implicits and type parameters to
> roll your own "dependent typing."
>
> abstract class Super {
>  def m() : Unit = {}
> }
>
> trait T {
>  this : Super => //always mixed with Super
>  def n() : Unit = { m() }
> }
>
> class A extends Super {}
>
> class B extends Super with T {}
>
> class C[+A <: Super](attr : A) extends Super {}
>
> object C {
>   implicit def cT2T(c:C[Super with T]) = new Super with T {
>     override def m() = c.m
>   }
> }
>
> object Run extends Application {
>   import C._
>
>  val a = new A
>  val b = new B
>
>  val cWithoutT = new C(a)
>
> //  cWithoutT.n  // won't compile
>
>
>  val cWithT = new C(b)
>
>  cWithT.n // works just fine
> }
>
> On Tue, Sep 30, 2008 at 11:32 PM, Daniel Kröni <daniel.kroeni <at> gmail.com>
> wrote:
>>
>> Hi all,
>>
>> I have a class C whose type should depend on the type of its
>> constructor argument. In my example: If I create a new C with an attr
>> where (attr.isInstanceOf[T] == true) then the new instance of C
>> should also be mixed with T, otherwise not. The solution I found
>> utilizes a factory method and a cast. I want to avoid the cast and if
>> possible also the factory method.
>>
>> Maybe dependent types solve my problem?
>>
>> Thanks
>> daniel
>>
>> abstract class Super {
>>  def m() : Unit = {}
>> }
>>
>> trait T {
>>  this : Super => //always mixed with Super
>>  def n() : Unit = { m() }
>> }
>>
>> class A extends Super {}
>>
>> class B extends Super with T {}
>>
>> // this class should be mixed with T if attr is mixed with T
>> class C(attr : Super) extends Super {}
>>
>> object C {
>>  //Factory method
>>  def create(attr : Super) : C = {
>>   if(attr.isInstanceOf[T]){ return new C(attr) with T }
>>   else { return new C(attr) }
>>  }
>> }
>>
>> object Run extends Application {
>>  val a = new A
>>  val b = new B
>>
>>  val cWithoutT = C.create(a);
>>  cWithoutT.m
>>
>>  val cWithT = C.create(b);
>>  // this works
>>  cWithT.asInstanceOf[Super with T].n;
>>
>>  // but this is what I want
>>  cWithT.n
>> }
>
>
Harshad | 1 Oct 19:11 2008
Picon

Building scala projects

I feel pretty dumb asking this, but a lot of web-searching reveals nothing.

How exactly do we build a scala project efficiently, without using maven,
ant or an IDE?

ant and maven are XML mine fields. I don't feel like looking at them (and I
am usually game for learning something new!)

IDEs are too heavy-weight for me. In all my development work, I have only
needed vim + make. It's fast and it works.

Assuming "make" is the build tool, how would I go about writing a Makefile
for building a scala project?

Scalac generates a whole lot of class files whose names are not
(necessarily) related to the original source file name. How would I write
dependencies? Is there a way to auto-generate the dependencies
like "gcc -M"?

One way I can think of, is to do it with a little script (instead of make):
1. t = timestamp of output jar
2. "*.scala" foreach (f => if (t < f.timestamp) scalac f)
3. Build output jar

Is that a good approach?

The maven invocation to build (for example) a /lift/ project seems to
automatically detect the source files which have been modified. It prints
something like:
Compiling 3 source files...

How does it manage that?

Thanks,
Harshad

Mark Harrah | 1 Oct 20:42 2008

Re: Building scala projects

Hi Harshad,

I have written a simple build tool in Scala that may interest you, but it is 
brand new (no project page yet) and only tested on a couple of my own 
projects.  If that hasn't scared you away, here is more about it:

First, the types of projects it is useful for right now:
* Scala-only source files
* Projects with dependencies that are manually manageable (i.e., drop the jars 
in the lib directory)
* Build process that goes something like: compile, test, doc, create jars

Second, some features:
* Fairly fast, unintrusive, and easy to set up for simple projects
* Configuration is just the project name + version in a two-line text file
* The default source directory layout is the same as maven's so you can always 
switch to maven should you need/want to
* Regardless of what sources you have added, changed, or removed, it should 
(in theory) recompile the right sources (using information extracted from 
compilation with a plugin)
* Can run tests on all objects that subclass org.scalacheck.Properties
* Can generates documentation with scaladoc
* Packages jars (classes, sources, or api docs)
* Can start the Scala interpreter with the right classpath (dependencies + 
compiled classes)

If this seems like something you'd be interested in, I'll write some 
documentation and post the code somewhere.

-Mark

On Wednesday 01 October 2008 13:11, Harshad wrote:
> I feel pretty dumb asking this, but a lot of web-searching reveals nothing.
> 
> How exactly do we build a scala project efficiently, without using maven,
> ant or an IDE?
> 
> ant and maven are XML mine fields. I don't feel like looking at them (and I
> am usually game for learning something new!)
> 
> IDEs are too heavy-weight for me. In all my development work, I have only
> needed vim + make. It's fast and it works.
> 
> Assuming "make" is the build tool, how would I go about writing a Makefile
> for building a scala project?
> 
> Scalac generates a whole lot of class files whose names are not
> (necessarily) related to the original source file name. How would I write
> dependencies? Is there a way to auto-generate the dependencies
> like "gcc -M"?
> 
> One way I can think of, is to do it with a little script (instead of make):
> 1. t = timestamp of output jar
> 2. "*.scala" foreach (f => if (t < f.timestamp) scalac f)
> 3. Build output jar
> 
> Is that a good approach?
> 
> The maven invocation to build (for example) a /lift/ project seems to
> automatically detect the source files which have been modified. It prints
> something like:
> Compiling 3 source files...
> 
> How does it manage that?
> 
> Thanks,
> Harshad
> 
> 
> 

Alex Boisvert | 1 Oct 21:05 2008

Re: Building scala projects

If you don't feel like reinventing the wheel, take a look at Buildr:
http://incubator.apache.org/buildr/

It's a Ruby-based build system based on Rake (the Ruby Make) and has good Scala support:
http://incubator.apache.org/buildr/languages.html#scala

A Buildfile may be as simple as:

repositories.remote << 'http://repo1.maven.org/maven2'

define "MyProject", :group => "com.example", :version => "1.0" do
  # customize here if necessary, e.g.
  # compile.with SLF4J, SCALAX, ...
end

This gets you compilation with scalac, test with ScalaTest/Specs/ScalaCheck and plenty of room to grow towards multi-language projects or more complex builds.

alex

On Wed, Oct 1, 2008 at 10:11 AM, Harshad <harshad.rj-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
I feel pretty dumb asking this, but a lot of web-searching reveals nothing.

How exactly do we build a scala project efficiently, without using maven,
ant or an IDE?

ant and maven are XML mine fields. I don't feel like looking at them (and I
am usually game for learning something new!)

IDEs are too heavy-weight for me. In all my development work, I have only
needed vim + make. It's fast and it works.

Assuming "make" is the build tool, how would I go about writing a Makefile
for building a scala project?

Scalac generates a whole lot of class files whose names are not
(necessarily) related to the original source file name. How would I write
dependencies? Is there a way to auto-generate the dependencies
like "gcc -M"?

One way I can think of, is to do it with a little script (instead of make):
1. t = timestamp of output jar
2. "*.scala" foreach (f => if (t < f.timestamp) scalac f)
3. Build output jar

Is that a good approach?

The maven invocation to build (for example) a /lift/ project seems to
automatically detect the source files which have been modified. It prints
something like:
Compiling 3 source files...

How does it manage that?

Thanks,
Harshad


Sciss | 2 Oct 00:17 2008
Picon

Reassignment to val

hello,

i have a problem with the below code. the line program.value = ...  
brings up a "reassignment to val" compilation error. i don't  
understand why? value_= is a method, and valu is a variable, so there  
are no val statements here?

thanks, -sciss-


abstract class ServerOption[T]( val switch: String, val default: T ) {
   protected var valu = default
   def value() : T = valu
   def value_=( newValue: T ) : ServerOption[T] = {
     valu = newValue
     this
   }
}

class ServerStringOption( override val switch: String,
                           override val default: String )
extends ServerOption[String]( switch, default ) {
}

class ServerOptions {
   val program	 = new ServerStringOption( "", "scsynth" )

   // ---- constructor ----
   program.value = "/My/Path/scsynth"  // "reassignment to val"
}

James Iry | 2 Oct 01:39 2008
Picon

Re: Reassignment to val

Missing parenthesis. Should be

program.value() = "/My/Path/scsynth"



On Wed, Oct 1, 2008 at 3:17 PM, Sciss <contact-ezOpgnVUB8c@public.gmane.org> wrote:
hello,

i have a problem with the below code. the line program.value = ... brings up a "reassignment to val" compilation error. i don't understand why? value_= is a method, and valu is a variable, so there are no val statements here?

thanks, -sciss-


abstract class ServerOption[T]( val switch: String, val default: T ) {
 protected var valu = default
 def value() : T = valu
 def value_=( newValue: T ) : ServerOption[T] = {
   valu = newValue
   this
 }
}

class ServerStringOption( override val switch: String,
                         override val default: String )
extends ServerOption[String]( switch, default ) {
}

class ServerOptions {
 val program    = new ServerStringOption( "", "scsynth" )

 // ---- constructor ----
 program.value = "/My/Path/scsynth"  // "reassignment to val"
}



Gmane