dynamic mixin?

View: New views
20 Messages — Rating Filter:   Alert me  
< Prev | 1 - 2 | Next >

dynamic mixin?

by Kubitz, Jörg :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Some parts of this message have been removed. Learn more about Nabble's security policy.

Hi,

 

i am new to scala and want to understand how I could use scalas traits.

I’d like to have something like a dynamic mixin i.e. I know wich which trait-hirarchie I want to mix in  at compile time but don’t know which actual Trait I want to use at runtime.

I know that I could do it in java with the Strategy-Pattern, but I thought traits could help me with it. But it doesn’t seem to work:

 

 

trait EntityDef { // Interface

  def x:Double

  def y :Double

}

 

trait Data extends EntityDef{ // Data Implementation

  var y:Double=4

  val x:Double=5

}

 

trait AspektMath extends EntityDef { // Functions without Data

  def doMath()

}

 

trait div extends AspektMath {

  def doMath(){println(x/y)} 

}

trait mul extends AspektMath {

  def doMath(){println(x*y)}

}

 

trait  Entity[T >: AspektMath] extends Data with T {

  //does not compile

}

 

 

Any suggestions?

 

 

Dr. Jörg Kubitz
________________________________________
zeb/information.technology gmbh & co. kg

Hammer Straße 165
48153 Münster

Phone 

+4925197128-567

Fax 

+4925197128-101

E-Mail 

jkubitz@...

WWW 

http://www.zeb.de



Amtsgericht Münster | HRA 5127
Geschäftsführer: Martin Danne | Jürgen Hofner | Andreas Schick

 

Diese E-Mail und alle angefügten Dateien sind vertraulich und ausschließlich für den Adressaten bestimmt. Sollten Sie nicht der bezeichnete Adressat sein, informieren Sie bitte umgehend den Absender. Die Inhalte dieser E-Mail dürfen in diesem Fall nicht an Dritte weitergegeben, für keine Zwecke genutzt und in keiner Form gespeichert oder kopiert werden. Im Fall technischer Probleme mit dieser E-Mail wenden Sie sich bitte an den Absender.


This e-mail and any attachments are confidential and may also be privileged. If you are not the named recipient, please notify the sender immediately and do not disclose the contents to another person, use it for any purpose, or store or copy the information in any medium. In the event of any technical difficulty with this e-mail, please contact the sender.

 

 


Re: dynamic mixin?

by Johannes Rudolph-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Perhaps this way:

trait EntityDef { // Interface
  def x:Double
  def y :Double
}

trait Data extends EntityDef{ // Data Implementation
  var y:Double=4
  val x:Double=5
}

trait AspektMath { // Functions without Data
  def doMath()
}

trait div extends AspektMath {self:EntityDef => // the self type
ensures that this trait can
                                                                   //
only be mixed if EntityDef is mixed in as well
  def doMath(){println(x/y)}
}

trait mul extends AspektMath {self:EntityDef =>
  def doMath(){println(x*y)}
}

trait Entity extends Data with mul
trait Entity2 extends Data with div

etc.

--
Johannes

-----------------------------------------------
Johannes Rudolph
http://virtual-void.net

AW: dynamic mixin?

by Kubitz, Jörg :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Thanks for the reply

1. unimportant: I don’t see a value to use that " self:EntityDef =>" stuff in each class. Extending AspektMath with EntityDef only one time works well:
trait EntityDef { // Interface
  def x:Double
  def y :Double
}

trait Data extends EntityDef{ // Data Implementation
  var y:Double=4
  val x:Double=5
}

trait AspektMath extends EntityDef{ // Functions without Data
  def doMath()
}

trait div extends AspektMath {
  def doMath(){println(x/y)}
}

trait mul extends AspektMath {
  def doMath(){println(x*y)}
}

class Entity extends Data with mul
class Entity2 extends Data with div

But since I don’t have any experience with scala there any benefits for "self:EntityDef" over an extention? Which?

2. unfortunately your suggestion ist not realy a runtime mixin. I could perfectly use that code for the single polymorphism as in my simple example. But in the end I want to have a much more complex Entity with polymorphisms on many aspect like

class Entity extends Data with AspektMath1 with AspektMath2 with AspektMath3 with AspektMath4

each AspektMath1.. AspektMath4 does have a polymorphism and I don’t want to declare the cross-Product of all possible combinations since that may be thousands and due to correlations only a few may be actually used.

Further ideas?

-----Ursprüngliche Nachricht-----
Von: Johannes Rudolph [mailto:johannes.rudolph@...]
Gesendet: Donnerstag, 5. November 2009 10:43
An: Kubitz, Jörg
Cc: scala-user@...
Betreff: Re: [scala-user] dynamic mixin?

Perhaps this way:

trait EntityDef { // Interface
  def x:Double
  def y :Double
}

trait Data extends EntityDef{ // Data Implementation
  var y:Double=4
  val x:Double=5
}

trait AspektMath { // Functions without Data
  def doMath()
}

trait div extends AspektMath {self:EntityDef => // the self type
ensures that this trait can
                                                                   //
only be mixed if EntityDef is mixed in as well
  def doMath(){println(x/y)}
}

trait mul extends AspektMath {self:EntityDef =>
  def doMath(){println(x*y)}
}

trait Entity extends Data with mul
trait Entity2 extends Data with div

etc.

--
Johannes

-----------------------------------------------
Johannes Rudolph
http://virtual-void.net

Re: dynamic mixin?

by Stefan Langer-4 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Danymic mixins are not supported directly.

You are basically trying to mixin a trait on an existing object? If you are not interested to mix into an existing object
you can use pattern matching on the type to get what you want

Soemthing along the lines
getMixin[T :< AspektMath](a: T) = {
      a: Multi => new Object with mul
      a: Div => new Object with div
}

or you use getMixin[ T <% AspektMath](a: T) = ...
then you need to provide an implict that is capable of turning T into a type of AspektMath.

This is just an idea that I haven't tested since I do not have access to a scala shell at this computer.

Regards
Stefan

2009/11/5 Kubitz, Jörg <jkubitz@...>
Thanks for the reply

1. unimportant: I don’t see a value to use that " self:EntityDef =>" stuff in each class. Extending AspektMath with EntityDef only one time works well:
trait EntityDef { // Interface
 def x:Double
 def y :Double
}

trait Data extends EntityDef{ // Data Implementation
 var y:Double=4
 val x:Double=5
}

trait AspektMath extends EntityDef{ // Functions without Data
 def doMath()
}

trait div extends AspektMath {
 def doMath(){println(x/y)}
}

trait mul extends AspektMath {
 def doMath(){println(x*y)}
}

class Entity extends Data with mul
class Entity2 extends Data with div

But since I don’t have any experience with scala there any benefits for "self:EntityDef" over an extention? Which?

2. unfortunately your suggestion ist not realy a runtime mixin. I could perfectly use that code for the single polymorphism as in my simple example. But in the end I want to have a much more complex Entity with polymorphisms on many aspect like

class Entity extends Data with AspektMath1 with AspektMath2 with AspektMath3 with AspektMath4

each AspektMath1.. AspektMath4 does have a polymorphism and I don’t want to declare the cross-Product of all possible combinations since that may be thousands and due to correlations only a few may be actually used.

Further ideas?

-----Ursprüngliche Nachricht-----
Von: Johannes Rudolph [mailto:johannes.rudolph@...]
Gesendet: Donnerstag, 5. November 2009 10:43
An: Kubitz, Jörg
Cc: scala-user@...
Betreff: Re: [scala-user] dynamic mixin?

Perhaps this way:

trait EntityDef { // Interface
  def x:Double
  def y :Double
}

trait Data extends EntityDef{ // Data Implementation
  var y:Double=4
  val x:Double=5
}

trait AspektMath { // Functions without Data
  def doMath()
}

trait div extends AspektMath {self:EntityDef => // the self type
ensures that this trait can
                                                                   //
only be mixed if EntityDef is mixed in as well
  def doMath(){println(x/y)}
}

trait mul extends AspektMath {self:EntityDef =>
  def doMath(){println(x*y)}
}

trait Entity extends Data with mul
trait Entity2 extends Data with div

etc.

--
Johannes

-----------------------------------------------
Johannes Rudolph
http://virtual-void.net


Re: dynamic mixin?

by Johannes Rudolph-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi Jörg,

On Thu, Nov 5, 2009 at 11:05 AM, Kubitz, Jörg <jkubitz@...> wrote:
> 2. unfortunately your suggestion ist not realy a runtime mixin. I could perfectly use that code for the single polymorphism as in my simple example. But in the end I want to have a much more complex Entity with polymorphisms on many aspect like
>
> class Entity extends Data with AspektMath1 with AspektMath2 with AspektMath3 with AspektMath4
>
> each AspektMath1.. AspektMath4 does have a polymorphism and I don’t want to declare the cross-Product of all possible combinations since that may be thousands and due to correlations only a few may be actually used.
>
> Further ideas?

It's not clear to me what you want to do. What do you mean by
dynamic/runtime mixin and how that relates to the original code you
posted. Perhaps you can give us an example of how the class-hierarchy
should be used?

--
Johannes

-----------------------------------------------
Johannes Rudolph
http://virtual-void.net

Re: dynamic mixin?

by Kevin Wright-4 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Try using object composition.  You could always hold an AspektMath
instance in Entity, and then delegate the call to doMath.

trait AspektMath extends EntityDef { // Functions without Data
  def doMath()
}

object div extends AspektMath {
  def doMath(){println(x/y)}
}
object mul extends AspektMath {
  def doMath(){println(x*y)}
}

class Entity(theAspekt : AspektMath) extends Data with AspektMath {
  def doMath() = theAspekt.doMath()
}

class MulEntity extends Entity(mul)
class DivEntity extends Entity(div)

On Thu, Nov 5, 2009 at 10:05 AM, Kubitz, Jörg <jkubitz@...> wrote:

> Thanks for the reply
>
> 1. unimportant: I don’t see a value to use that " self:EntityDef =>" stuff in each class. Extending AspektMath with EntityDef only one time works well:
> trait EntityDef { // Interface
>  def x:Double
>  def y :Double
> }
>
> trait Data extends EntityDef{ // Data Implementation
>  var y:Double=4
>  val x:Double=5
> }
>
> trait AspektMath extends EntityDef{ // Functions without Data
>  def doMath()
> }
>
> trait div extends AspektMath {
>  def doMath(){println(x/y)}
> }
>
> trait mul extends AspektMath {
>  def doMath(){println(x*y)}
> }
>
> class Entity extends Data with mul
> class Entity2 extends Data with div
>
> But since I don’t have any experience with scala there any benefits for "self:EntityDef" over an extention? Which?
>
> 2. unfortunately your suggestion ist not realy a runtime mixin. I could perfectly use that code for the single polymorphism as in my simple example. But in the end I want to have a much more complex Entity with polymorphisms on many aspect like
>
> class Entity extends Data with AspektMath1 with AspektMath2 with AspektMath3 with AspektMath4
>
> each AspektMath1.. AspektMath4 does have a polymorphism and I don’t want to declare the cross-Product of all possible combinations since that may be thousands and due to correlations only a few may be actually used.
>
> Further ideas?
>
> -----Ursprüngliche Nachricht-----
> Von: Johannes Rudolph [mailto:johannes.rudolph@...]
> Gesendet: Donnerstag, 5. November 2009 10:43
> An: Kubitz, Jörg
> Cc: scala-user@...
> Betreff: Re: [scala-user] dynamic mixin?
>
> Perhaps this way:
>
> trait EntityDef { // Interface
>   def x:Double
>   def y :Double
> }
>
> trait Data extends EntityDef{ // Data Implementation
>   var y:Double=4
>   val x:Double=5
> }
>
> trait AspektMath { // Functions without Data
>   def doMath()
> }
>
> trait div extends AspektMath {self:EntityDef => // the self type
> ensures that this trait can
>                                                                    //
> only be mixed if EntityDef is mixed in as well
>   def doMath(){println(x/y)}
> }
>
> trait mul extends AspektMath {self:EntityDef =>
>   def doMath(){println(x*y)}
> }
>
> trait Entity extends Data with mul
> trait Entity2 extends Data with div
>
> etc.
>
> --
> Johannes
>
> -----------------------------------------------
> Johannes Rudolph
> http://virtual-void.net
>

Re: dynamic mixin?

by andrew cooke-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

My guess:  You are thinking about this wrong.  You need to explain what you are trying to do in more general terms and then people can explain how it can work within Scala.  I suggest you focus on how you will "dynamically" assemble these objects, because that must exist within Scala code and there, I think, you will find that this "dynamic" approach is in fact "static" (if I understand what you want correctly).  Andrew


2009/11/5 Kubitz, Jörg <jkubitz@...>

Hi,

 

i am new to scala and want to understand how I could use scalas traits.

I’d like to have something like a dynamic mixin i.e. I know wich which trait-hirarchie I want to mix in  at compile time but don’t know which actual Trait I want to use at runtime.

I know that I could do it in java with the Strategy-Pattern, but I thought traits could help me with it. But it doesn’t seem to work:

 

 

trait EntityDef { // Interface

  def x:Double

  def y :Double

}

 

trait Data extends EntityDef{ // Data Implementation

  var y:Double=4

  val x:Double=5

}

 

trait AspektMath extends EntityDef { // Functions without Data

  def doMath()

}

 

trait div extends AspektMath {

  def doMath(){println(x/y)} 

}

trait mul extends AspektMath {

  def doMath(){println(x*y)}

}

 

trait  Entity[T >: AspektMath] extends Data with T {

  //does not compile

}

 

 

Any suggestions?

 

 

Dr. Jörg Kubitz
________________________________________
zeb/information.technology gmbh & co. kg

Hammer Straße 165
48153 Münster

Phone 

+4925197128-567

Fax 

+4925197128-101

E-Mail 

jkubitz@...

WWW 

http://www.zeb.de



Amtsgericht Münster | HRA 5127
Geschäftsführer: Martin Danne | Jürgen Hofner | Andreas Schick

 

Diese E-Mail und alle angefügten Dateien sind vertraulich und ausschließlich für den Adressaten bestimmt. Sollten Sie nicht der bezeichnete Adressat sein, informieren Sie bitte umgehend den Absender. Die Inhalte dieser E-Mail dürfen in diesem Fall nicht an Dritte weitergegeben, für keine Zwecke genutzt und in keiner Form gespeichert oder kopiert werden. Im Fall technischer Probleme mit dieser E-Mail wenden Sie sich bitte an den Absender.


This e-mail and any attachments are confidential and may also be privileged. If you are not the named recipient, please notify the sender immediately and do not disclose the contents to another person, use it for any purpose, or store or copy the information in any medium. In the event of any technical difficulty with this e-mail, please contact the sender.

 

 



Re: dynamic mixin?

by Kevin Wright-4 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

It sounds like you want the ability to mix-in an object instance,
instead of a type.

I'm working on a plugin to do just this, but it's currently far from perfect.
I have to synthesise methods, which means that classes with the
dynamic mixins must be compiled in a separate pass from the classes
that use them.  This is obviously not a viable approach for real-world
problems, but I am vigorously seeking a solution to that :)

On Thu, Nov 5, 2009 at 10:17 AM, Johannes Rudolph
<johannes.rudolph@...> wrote:

> Hi Jörg,
>
> On Thu, Nov 5, 2009 at 11:05 AM, Kubitz, Jörg <jkubitz@...> wrote:
>> 2. unfortunately your suggestion ist not realy a runtime mixin. I could perfectly use that code for the single polymorphism as in my simple example. But in the end I want to have a much more complex Entity with polymorphisms on many aspect like
>>
>> class Entity extends Data with AspektMath1 with AspektMath2 with AspektMath3 with AspektMath4
>>
>> each AspektMath1.. AspektMath4 does have a polymorphism and I don’t want to declare the cross-Product of all possible combinations since that may be thousands and due to correlations only a few may be actually used.
>>
>> Further ideas?
>
> It's not clear to me what you want to do. What do you mean by
> dynamic/runtime mixin and how that relates to the original code you
> posted. Perhaps you can give us an example of how the class-hierarchy
> should be used?
>
> --
> Johannes
>
> -----------------------------------------------
> Johannes Rudolph
> http://virtual-void.net
>

AW: dynamic mixin?

by Kubitz, Jörg :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

What I want to do is the following:


trait EntityDef1 { // Interface1
  def x:Double
}

trait EntityDef2 extends EntityDef1 { // Interface2
  def y:Double
}

trait Data extends EntityDef2{ // Data Implementation
  var y:Double=4
  val x:Double=5
}

trait AspektMath1 extends EntityDef1 { // Functions with 1 parameter without data
  def doMath1()
}
trait log extends AspektMath1 {
  def doMath1(){println(Math.log(x))}  
}
trait exp extends AspektMath1 {
  def doMath1(){println(Math.exp(x))}
}  

trait AspektMath2 extends EntityDef2 { // Functions with 2 parameters without data
  def doMath2()
}
trait div extends AspektMath2 {
  def doMath2(){println(x/y)}  
}
trait mul extends AspektMath2 {
  def doMath2(){println(x*y)}
}

class  Entity(math1: AspektMath1,math2: AspektMath2) extends Data with AspektMath1 with AspektMath2{
    def doMath1=math1.doMath1
    def doMath2=math2.doMath2
}

class c1 {
  var m1:AspektMath1
  var m2:AspektMath2
  // which aspects should be used is a runtime decision
  m1=new exp() // does not compile since exp is not a class
  m2=new mul() // does not compile since exp is not a class
  var e=new Entity(m1, m2)
  e.doMath1
  e.doMath2
}

object t1 extends Application {
  new c1
}

@Kevin: I doesn’t compile with objects either
@Johannes: Even though above is a academic example we do have Real-World szenarios where the applied Function (which are much more complicated then the above) should be mixed in at runtime. We read Data from a database and the functions that should be called should be selected by the data.
We already have done it with java interfaces, but I don’t get if there is a nicer way with traits.

-----Ursprüngliche Nachricht-----
Von: Johannes Rudolph [mailto:johannes.rudolph@...]
Gesendet: Donnerstag, 5. November 2009 11:18
An: Kubitz, Jörg
Cc: scala-user@...
Betreff: Re: [scala-user] dynamic mixin?

Hi Jörg,

On Thu, Nov 5, 2009 at 11:05 AM, Kubitz, Jörg <jkubitz@...> wrote:
> 2. unfortunately your suggestion ist not realy a runtime mixin. I could perfectly use that code for the single polymorphism as in my simple example. But in the end I want to have a much more complex Entity with polymorphisms on many aspect like
>
> class Entity extends Data with AspektMath1 with AspektMath2 with AspektMath3 with AspektMath4
>
> each AspektMath1.. AspektMath4 does have a polymorphism and I don’t want to declare the cross-Product of all possible combinations since that may be thousands and due to correlations only a few may be actually used.
>
> Further ideas?

It's not clear to me what you want to do. What do you mean by
dynamic/runtime mixin and how that relates to the original code you
posted. Perhaps you can give us an example of how the class-hierarchy
should be used?

--
Johannes

-----------------------------------------------
Johannes Rudolph
http://virtual-void.net

AW: dynamic mixin?

by Kubitz, Jörg :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Some parts of this message have been removed. Learn more about Nabble's security policy.

Hi Stefan,

 

“Object with mul” does not compile since x,y are not defined.

But “Data with mul” does compile J

 – but it does not calculate the desired result L since the wrong (default) datas are mixed in.

I would need to mix in an existing object like “=d with mul”

 

trait EntityDef1 { // Interface1

  def x:Double

}

 

trait EntityDef2 extends EntityDef1 { // Interface2

  def y:Double

}

 

class Data extends EntityDef2{ // Data Implementation

  var y:Double=4

  var x:Double=5

}

 

trait AspektMath1 extends EntityDef1 { // Functions with 1 parameter without data

  def doMath1()

}

trait log extends AspektMath1 {

  def doMath1(){println(Math.log(x))} 

}

trait exp extends AspektMath1 {

  def doMath1(){println(Math.exp(x))}

 

trait AspektMath2 extends EntityDef2 { // Functions with 2 parameters without data

  def doMath2()

}

trait div extends AspektMath2 {

  def doMath2(){println(x/y)} 

}

trait mul extends AspektMath2 {

  def doMath2(){println(x*y)}

}

 

class  Entity(math1: AspektMath1,math2: AspektMath2) extends Data with AspektMath1 with AspektMath2{

    def doMath1=math1.doMath1

    def doMath2=math2.doMath2

}

 

class c1 {

//  var d:Data= new Data

//  d.x=10

//  d.y=20

  var m1:AspektMath1 =new Data with exp

  var m2:AspektMath2 =new Data with mul

  var e=new Entity(m1, m2)

  e.x=10

  e.y=20

  e.doMath1  // gives exp(5)=148.4131591025766 // should be exp(10) = 22026,4657948067

  e.doMath2  // gives 4*5=20 // should be 10*20 = 200

}

 

Von: Stefan Langer [mailto:mailtolanger@...]
Gesendet: Donnerstag, 5. November 2009 11:17
An: Kubitz, Jörg
Cc: scala-user@...
Betreff: Re: [scala-user] dynamic mixin?

 

Danymic mixins are not supported directly.

You are basically trying to mixin a trait on an existing object? If you are not interested to mix into an existing object
you can use pattern matching on the type to get what you want

Soemthing along the lines
getMixin[T :< AspektMath](a: T) = {
      a: Multi => new Object with mul
      a: Div => new Object with div
}

or you use getMixin[ T <% AspektMath](a: T) = ...
then you need to provide an implict that is capable of turning T into a type of AspektMath.

This is just an idea that I haven't tested since I do not have access to a scala shell at this computer.

Regards
Stefan

2009/11/5 Kubitz, Jörg <jkubitz@...>

Thanks for the reply

1. unimportant: I don’t see a value to use that " self:EntityDef =>" stuff in each class. Extending AspektMath with EntityDef only one time works well:

trait EntityDef { // Interface
 def x:Double
 def y :Double
}

trait Data extends EntityDef{ // Data Implementation
 var y:Double=4
 val x:Double=5
}

trait AspektMath extends EntityDef{ // Functions without Data

 def doMath()
}

trait div extends AspektMath {

 def doMath(){println(x/y)}
}

trait mul extends AspektMath {
 def doMath(){println(x*y)}
}

class Entity extends Data with mul
class Entity2 extends Data with div

But since I don’t have any experience with scala there any benefits for "self:EntityDef" over an extention? Which?

2. unfortunately your suggestion ist not realy a runtime mixin. I could perfectly use that code for the single polymorphism as in my simple example. But in the end I want to have a much more complex Entity with polymorphisms on many aspect like

class Entity extends Data with AspektMath1 with AspektMath2 with AspektMath3 with AspektMath4

each AspektMath1.. AspektMath4 does have a polymorphism and I don’t want to declare the cross-Product of all possible combinations since that may be thousands and due to correlations only a few may be actually used.

Further ideas?

-----Ursprüngliche Nachricht-----
Von: Johannes Rudolph [mailto:johannes.rudolph@...]
Gesendet: Donnerstag, 5. November 2009 10:43
An: Kubitz, Jörg
Cc: scala-user@...
Betreff: Re: [scala-user] dynamic mixin?


Perhaps this way:

trait EntityDef { // Interface
  def x:Double
  def y :Double
}

trait Data extends EntityDef{ // Data Implementation
  var y:Double=4
  val x:Double=5
}

trait AspektMath { // Functions without Data
  def doMath()
}

trait div extends AspektMath {self:EntityDef => // the self type
ensures that this trait can
                                                                   //
only be mixed if EntityDef is mixed in as well
  def doMath(){println(x/y)}
}

trait mul extends AspektMath {self:EntityDef =>
  def doMath(){println(x*y)}
}

trait Entity extends Data with mul
trait Entity2 extends Data with div

etc.

--
Johannes

-----------------------------------------------
Johannes Rudolph
http://virtual-void.net

 


Re: dynamic mixin?

by Stefan Langer-4 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Use methods to obtain x and y and make your traits expect a type that implement def x and def y  then you should be set.


2009/11/5 Kubitz, Jörg <jkubitz@...>

Hi Stefan,

 

“Object with mul” does not compile since x,y are not defined.

But “Data with mul” does compile J

 – but it does not calculate the desired result L since the wrong (default) datas are mixed in.

I would need to mix in an existing object like “=d with mul”

 

trait EntityDef1 { // Interface1

  def x:Double

}

 

trait EntityDef2 extends EntityDef1 { // Interface2

  def y:Double

}

 

class Data extends EntityDef2{ // Data Implementation

  var y:Double=4

  var x:Double=5

}

 

trait AspektMath1 extends EntityDef1 { // Functions with 1 parameter without data

  def doMath1()

}

trait log extends AspektMath1 {

  def doMath1(){println(Math.log(x))} 

}

trait exp extends AspektMath1 {

  def doMath1(){println(Math.exp(x))}

 

trait AspektMath2 extends EntityDef2 { // Functions with 2 parameters without data

  def doMath2()

}

trait div extends AspektMath2 {

  def doMath2(){println(x/y)} 

}

trait mul extends AspektMath2 {

  def doMath2(){println(x*y)}

}

 

class  Entity(math1: AspektMath1,math2: AspektMath2) extends Data with AspektMath1 with AspektMath2{

    def doMath1=math1.doMath1

    def doMath2=math2.doMath2

}

 

class c1 {

//  var d:Data= new Data

//  d.x=10

//  d.y=20

  var m1:AspektMath1 =new Data with exp

  var m2:AspektMath2 =new Data with mul

  var e=new Entity(m1, m2)

  e.x=10

  e.y=20

  e.doMath1  // gives exp(5)=148.4131591025766 // should be exp(10) = 22026,4657948067

  e.doMath2  // gives 4*5=20 // should be 10*20 = 200

}

 

Von: Stefan Langer [mailto:mailtolanger@...]
Gesendet: Donnerstag, 5. November 2009 11:17


An: Kubitz, Jörg
Cc: scala-user@...
Betreff: Re: [scala-user] dynamic mixin?

 

Danymic mixins are not supported directly.

You are basically trying to mixin a trait on an existing object? If you are not interested to mix into an existing object
you can use pattern matching on the type to get what you want

Soemthing along the lines
getMixin[T :< AspektMath](a: T) = {
      a: Multi => new Object with mul
      a: Div => new Object with div
}

or you use getMixin[ T <% AspektMath](a: T) = ...
then you need to provide an implict that is capable of turning T into a type of AspektMath.

This is just an idea that I haven't tested since I do not have access to a scala shell at this computer.

Regards
Stefan

2009/11/5 Kubitz, Jörg <jkubitz@...>

Thanks for the reply

1. unimportant: I don’t see a value to use that " self:EntityDef =>" stuff in each class. Extending AspektMath with EntityDef only one time works well:

trait EntityDef { // Interface
 def x:Double
 def y :Double
}

trait Data extends EntityDef{ // Data Implementation
 var y:Double=4
 val x:Double=5
}

trait AspektMath extends EntityDef{ // Functions without Data

 def doMath()
}

trait div extends AspektMath {

 def doMath(){println(x/y)}
}

trait mul extends AspektMath {
 def doMath(){println(x*y)}
}

class Entity extends Data with mul
class Entity2 extends Data with div

But since I don’t have any experience with scala there any benefits for "self:EntityDef" over an extention? Which?

2. unfortunately your suggestion ist not realy a runtime mixin. I could perfectly use that code for the single polymorphism as in my simple example. But in the end I want to have a much more complex Entity with polymorphisms on many aspect like

class Entity extends Data with AspektMath1 with AspektMath2 with AspektMath3 with AspektMath4

each AspektMath1.. AspektMath4 does have a polymorphism and I don’t want to declare the cross-Product of all possible combinations since that may be thousands and due to correlations only a few may be actually used.

Further ideas?

-----Ursprüngliche Nachricht-----
Von: Johannes Rudolph [mailto:johannes.rudolph@...]
Gesendet: Donnerstag, 5. November 2009 10:43
An: Kubitz, Jörg
Cc: scala-user@...
Betreff: Re: [scala-user] dynamic mixin?


Perhaps this way:

trait EntityDef { // Interface
  def x:Double
  def y :Double
}

trait Data extends EntityDef{ // Data Implementation
  var y:Double=4
  val x:Double=5
}

trait AspektMath { // Functions without Data
  def doMath()
}

trait div extends AspektMath {self:EntityDef => // the self type
ensures that this trait can
                                                                   //
only be mixed if EntityDef is mixed in as well
  def doMath(){println(x/y)}
}

trait mul extends AspektMath {self:EntityDef =>
  def doMath(){println(x*y)}
}

trait Entity extends Data with mul
trait Entity2 extends Data with div

etc.

--
Johannes

-----------------------------------------------
Johannes Rudolph
http://virtual-void.net

 



Re: dynamic mixin?

by Johannes Rudolph-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Thu, Nov 5, 2009 at 12:03 PM, Kubitz, Jörg <jkubitz@...> wrote:

> class c1 {
>
> //  var d:Data= new Data
>
> //  d.x=10
>
> //  d.y=20
>
>   var m1:AspektMath1 =new Data with exp
>
>   var m2:AspektMath2 =new Data with mul
>
>   var e=new Entity(m1, m2)
>
>   e.x=10
>
>   e.y=20
>
>   e.doMath1  // gives exp(5)=148.4131591025766 // should be exp(10) =
> 22026,4657948067
>
>   e.doMath2  // gives 4*5=20 // should be 10*20 = 200
>
> }
Can you explain what this is expected to do in words? Why have
AspektMathX to be instances of Data?

Why don't you pass a Data instance to doMathX explicitly? You could
then define the operations as simple function objects:

val exp: Data => Unit = d => println(Math.exp(d.x))
etc.

--
Johannes

-----------------------------------------------
Johannes Rudolph
http://virtual-void.net

Re: dynamic mixin?

by Kevin Wright-4 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Would something like this scratch your itch?
(okay, so it's a trivial and useless example, but should demo the
concept well enough)



trait SomeTrait {
  val HelloString = "Hello World"
}

class SomeClass(@With theTrait: SomeTrait)

val traitInstance = new SomeTrait
val classInstance = new SomeClass(traitInstance)

println(classInstance.helloString) // This works!



The idea is that the @With annotation will synthesise delegates in
SomeClass for all methods from SomeTrait, SomeClass also gains the
interface of SomeTrait.  SomeTrait doesn't even have to be a trait,
though there are certain advantages if it is.

Parameterised types are handled in a sane & obvious manner, though
I've not yet explored possible interactions with type members or
higher-kinded types...

It can be applied to vals, vars, defs and constructor params.  Of
course, vals and vars become defs anyway (unless private), so handling
defs essentially gets these for free :)



On Thu, Nov 5, 2009 at 11:20 AM, Johannes Rudolph
<johannes.rudolph@...> wrote:

> On Thu, Nov 5, 2009 at 12:03 PM, Kubitz, Jörg <jkubitz@...> wrote:
>> class c1 {
>>
>> //  var d:Data= new Data
>>
>> //  d.x=10
>>
>> //  d.y=20
>>
>>   var m1:AspektMath1 =new Data with exp
>>
>>   var m2:AspektMath2 =new Data with mul
>>
>>   var e=new Entity(m1, m2)
>>
>>   e.x=10
>>
>>   e.y=20
>>
>>   e.doMath1  // gives exp(5)=148.4131591025766 // should be exp(10) =
>> 22026,4657948067
>>
>>   e.doMath2  // gives 4*5=20 // should be 10*20 = 200
>>
>> }
> Can you explain what this is expected to do in words? Why have
> AspektMathX to be instances of Data?
>
> Why don't you pass a Data instance to doMathX explicitly? You could
> then define the operations as simple function objects:
>
> val exp: Data => Unit = d => println(Math.exp(d.x))
> etc.
>
> --
> Johannes
>
> -----------------------------------------------
> Johannes Rudolph
> http://virtual-void.net
>

AW: dynamic mixin?

by Kubitz, Jörg :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Sounds good. But I am not sure if you want to support polymorphism.  I actually would need to use an extension of your example like this:

Trait Sometrait1 extends SomeTrait{
  override val HelloString = "Hello World1"
}
val traitInstance: SomeTrait = new SomeTrait1
val classInstance = new SomeClass(traitInstance)

Are you working on a plugin that would allow this?

-----Ursprüngliche Nachricht-----
Von: Kevin Wright [mailto:kev.lee.wright@...]
Gesendet: Donnerstag, 5. November 2009 12:38
An: Johannes Rudolph
Cc: Kubitz, Jörg; Stefan Langer; scala-user@...
Betreff: Re: [scala-user] dynamic mixin?

Would something like this scratch your itch?
(okay, so it's a trivial and useless example, but should demo the
concept well enough)



trait SomeTrait {
  val HelloString = "Hello World"
}

class SomeClass(@With theTrait: SomeTrait)

val traitInstance = new SomeTrait
val classInstance = new SomeClass(traitInstance)

println(classInstance.helloString) // This works!



The idea is that the @With annotation will synthesise delegates in
SomeClass for all methods from SomeTrait, SomeClass also gains the
interface of SomeTrait.  SomeTrait doesn't even have to be a trait,
though there are certain advantages if it is.

Parameterised types are handled in a sane & obvious manner, though
I've not yet explored possible interactions with type members or
higher-kinded types...

It can be applied to vals, vars, defs and constructor params.  Of
course, vals and vars become defs anyway (unless private), so handling
defs essentially gets these for free :)



On Thu, Nov 5, 2009 at 11:20 AM, Johannes Rudolph
<johannes.rudolph@...> wrote:

> On Thu, Nov 5, 2009 at 12:03 PM, Kubitz, Jörg <jkubitz@...> wrote:
>> class c1 {
>>
>> //  var d:Data= new Data
>>
>> //  d.x=10
>>
>> //  d.y=20
>>
>>   var m1:AspektMath1 =new Data with exp
>>
>>   var m2:AspektMath2 =new Data with mul
>>
>>   var e=new Entity(m1, m2)
>>
>>   e.x=10
>>
>>   e.y=20
>>
>>   e.doMath1  // gives exp(5)=148.4131591025766 // should be exp(10) =
>> 22026,4657948067
>>
>>   e.doMath2  // gives 4*5=20 // should be 10*20 = 200
>>
>> }
> Can you explain what this is expected to do in words? Why have
> AspektMathX to be instances of Data?
>
> Why don't you pass a Data instance to doMathX explicitly? You could
> then define the operations as simple function objects:
>
> val exp: Data => Unit = d => println(Math.exp(d.x))
> etc.
>
> --
> Johannes
>
> -----------------------------------------------
> Johannes Rudolph
> http://virtual-void.net
>

AW: dynamic mixin? working but ugly example

by Kubitz, Jörg :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Explanation:
I want to have a object that calculates me values.
Where the Data comes wrong and the actual math should be separated
The algorithms that are used should be selected at runtime
The examples should do at runtime:
1.Select two algorithms
2.fill in the data
3.evaluate the functions
Sounds like I want to "mix" datas and algorithms. So "mixin" should be usefull but I don’t get how.

Your workaround with the functions would imply that I need to prefix the "x" with the parameter "d" (d.x). Unfortunatly this would make the functionbodys ugly. Plus: actually I even can not(!)insert that prefix (since the function body is created by a blackbox - I may only define the box around the body)

What does Work is the same like we have it in java.
But
1. it looks as ugly as in Java since I have to forward x. And
2. even more ugly then in java: I can not pass a constructor variables to the aspects so the implementation has to start with NULLs :(
Suggestions?


Working example:

trait IData {// Interface
  def x:Double
  def y:Double
}
trait IEntity extends IData { // Interface
  def doMath()
}
trait DataAspekt { // Data Part
  var x:Double=5
  var y:Double=4
}

trait IMath {// Math Base
  var e:IEntity=null //UGLY
  def doMath()
  def x=e.x  //UGLY
  def y=e.y  //UGLY
}
trait MathAspekt extends IEntity { // Math Part  
  var m:IMath=null //UGLY
  def doMath()=m.doMath
}

class mul extends IMath { // sample Math Function  
  def doMath{println(x*y)}
}

class  Entity extends DataAspekt with MathAspekt {
}

class c1 {
  var e=new Entity
  e.m=new mul
  e.m.e=e
  e.x=10
  e.y=20
  e.doMath //200
}

object t1 extends Application {
  new c1
}



Re: dynamic mixin?

by Kevin Wright-4 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Thu, Nov 5, 2009 at 12:30 PM, Kubitz, Jörg <jkubitz@...> wrote:
> Sounds good. But I am not sure if you want to support polymorphism.  I actually would need to use an extension of your example like this:
>
> Trait Sometrait1 extends SomeTrait{
>  override val HelloString = "Hello World1"
> }
> val traitInstance: SomeTrait = new SomeTrait1
> val classInstance = new SomeClass(traitInstance)
>
> Are you working on a plugin that would allow this?


Oh yeah, that's exactly what I'm supporting :)
You can get even more dynamic that that if you want:


class SomeClass(@With var theTrait: SomeTrait)

val traitInstance = new SomeTrait
val classInstance = new SomeClass(traitInstance)

println(classInstance.helloString) // prints "Hello World"

Trait OtherTrait extends SomeTrait{
  override val HelloString = "Hello Other World"
}

classInstance.theTrait = new OtherTrait
println(classInstance.helloString) //  prints "Hello Other World"


> -----Ursprüngliche Nachricht-----
> Von: Kevin Wright [mailto:kev.lee.wright@...]
> Gesendet: Donnerstag, 5. November 2009 12:38
> An: Johannes Rudolph
> Cc: Kubitz, Jörg; Stefan Langer; scala-user@...
> Betreff: Re: [scala-user] dynamic mixin?
>
> Would something like this scratch your itch?
> (okay, so it's a trivial and useless example, but should demo the
> concept well enough)
>
>
>
> trait SomeTrait {
>  val HelloString = "Hello World"
> }
>
> class SomeClass(@With theTrait: SomeTrait)
>
> val traitInstance = new SomeTrait
> val classInstance = new SomeClass(traitInstance)
>
> println(classInstance.helloString) // This works!
>
>
>
> The idea is that the @With annotation will synthesise delegates in
> SomeClass for all methods from SomeTrait, SomeClass also gains the
> interface of SomeTrait.  SomeTrait doesn't even have to be a trait,
> though there are certain advantages if it is.
>
> Parameterised types are handled in a sane & obvious manner, though
> I've not yet explored possible interactions with type members or
> higher-kinded types...
>
> It can be applied to vals, vars, defs and constructor params.  Of
> course, vals and vars become defs anyway (unless private), so handling
> defs essentially gets these for free :)
>
>
>
> On Thu, Nov 5, 2009 at 11:20 AM, Johannes Rudolph
> <johannes.rudolph@...> wrote:
>> On Thu, Nov 5, 2009 at 12:03 PM, Kubitz, Jörg <jkubitz@...> wrote:
>>> class c1 {
>>>
>>> //  var d:Data= new Data
>>>
>>> //  d.x=10
>>>
>>> //  d.y=20
>>>
>>>   var m1:AspektMath1 =new Data with exp
>>>
>>>   var m2:AspektMath2 =new Data with mul
>>>
>>>   var e=new Entity(m1, m2)
>>>
>>>   e.x=10
>>>
>>>   e.y=20
>>>
>>>   e.doMath1  // gives exp(5)=148.4131591025766 // should be exp(10) =
>>> 22026,4657948067
>>>
>>>   e.doMath2  // gives 4*5=20 // should be 10*20 = 200
>>>
>>> }
>> Can you explain what this is expected to do in words? Why have
>> AspektMathX to be instances of Data?
>>
>> Why don't you pass a Data instance to doMathX explicitly? You could
>> then define the operations as simple function objects:
>>
>> val exp: Data => Unit = d => println(Math.exp(d.x))
>> etc.
>>
>> --
>> Johannes
>>
>> -----------------------------------------------
>> Johannes Rudolph
>> http://virtual-void.net
>>
>

Re: AW: dynamic mixin? working but ugly example

by Jesper Nordenberg :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Kubitz wrote:

> Explanation:
> I want to have a object that calculates me values.
> Where the Data comes wrong and the actual math should be separated
> The algorithms that are used should be selected at runtime
> The examples should do at runtime:
> 1.Select two algorithms
> 2.fill in the data
> 3.evaluate the functions
> Sounds like I want to "mix" datas and algorithms. So "mixin" should be usefull but I don’t get how.
>
> Your workaround with the functions would imply that I need to prefix the "x" with the parameter "d" (d.x). Unfortunatly this would make the functionbodys ugly. Plus: actually I even can not(!)insert that prefix (since the function body is created by a blackbox - I may only define the box around the body)

How about this then:

scala> class Data { val x = 1.0; val y = 2.0 }
defined class Data

scala> def fn(d : Data) = {import d._; x + y}
fn: (d: Data)Double

/Jesper Nordenberg


debugging call by names

by Kubitz, Jörg :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Some parts of this message have been removed. Learn more about Nabble's security policy.

I almost got what I wanted for my Aspekts – at least it compiled – but unfortunately it doesn’t return the right result.

So I wanted do debug it. But step by step debugging this example result in a “JDI thrad evaluation”-Error

I am using Eclipse 3.5.1 scala plugin 2.7.7.final, Windows XP

 

 

Question 1: is this “JDI thrad evaluation”-NullPointerExeption my Error or a scala BUG?

Question 2: does anybody have an idea whats wrong with my code?

Background: I wanted to use the “call by name” to surround a nullpointerexception that would acccour with an value call

  var e:Entity=new Entity (new mul(e))

Question 3: Any better Ideas to resolve that recursive definition of e?

Source:

 

trait IData {// Interface

  def x:Double

  def y:Double

}

trait IEntity extends IData { // Interface

  def doMath()

}

trait DataAspekt { // Data Part

  var x:Double=4

  var y:Double=5

  def load{

    x=10

    x=20

  }

}

 

abstract class Math(e: =>IEntity) extends IEntity{

  def doMath()

  lazy val e1=e //UGLY

  def x=e1.x  //UGLY

  def y=e1.y  //UGLY

}

trait MathAspekt extends IEntity { // Math Functions 

  def m:Math

  def doMath()=m.doMath

}

 

class mul(e: => IEntity) extends Math(e) { // Math Functions

  def doMath{println(x*y)}

}

 

class  Entity(m1: Math) extends DataAspekt with MathAspekt {

      val m=m1

}

 

class c1 {

  var e:Entity=new Entity (new mul(e))

  e.load

  e.doMath //gives 20 // should give 200

}

 

object t1 extends Application {

  println("hallo8")

  new c1

}

 

 

 

-----Ursprüngliche Nachricht-----
Von: Kubitz, Jörg [mailto:jkubitz@...]
Gesendet: Donnerstag, 5. November 2009 14:18
An: Johannes Rudolph
Cc: Stefan Langer; scala-user@...
Betreff: AW: [scala-user] dynamic mixin? working but ugly example

 

Explanation:

I want to have a object that calculates me values.

Where the Data comes wrong and the actual math should be separated

The algorithms that are used should be selected at runtime

The examples should do at runtime:

1.Select two algorithms

2.fill in the data

3.evaluate the functions

Sounds like I want to "mix" datas and algorithms. So "mixin" should be usefull but I don’t get how.

 

Your workaround with the functions would imply that I need to prefix the "x" with the parameter "d" (d.x). Unfortunatly this would make the functionbodys ugly. Plus: actually I even can not(!)insert that prefix (since the function body is created by a blackbox - I may only define the box around the body)

 

What does Work is the same like we have it in java.

But

1. it looks as ugly as in Java since I have to forward x. And

2. even more ugly then in java: I can not pass a constructor variables to the aspects so the implementation has to start with NULLs :(

Suggestions?

 

 

Working example:

 

trait IData {// Interface

  def x:Double

  def y:Double

}

trait IEntity extends IData { // Interface

  def doMath()

}

trait DataAspekt { // Data Part

  var x:Double=5

  var y:Double=4

}

 

trait IMath {// Math Base

  var e:IEntity=null //UGLY

  def doMath()

  def x=e.x  //UGLY

  def y=e.y  //UGLY

}

trait MathAspekt extends IEntity { // Math Part 

  var m:IMath=null //UGLY

  def doMath()=m.doMath

}

 

class mul extends IMath { // sample Math Function 

  def doMath{println(x*y)}

}

 

class  Entity extends DataAspekt with MathAspekt {

}

 

class c1 {

  var e=new Entity

  e.m=new mul

  e.m.e=e

  e.x=10

  e.y=20

  e.doMath //200

}

 

object t1 extends Application {

  new c1

}

 

 



AW: Re: AW: dynamic mixin? working but ugly example

by Kubitz, Jörg :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

That’s a cool feature, thanks for the tip!

But why doesn’t it work with a call by name?

class mul(e: => IEntity) extends Math(e) { // Math Functions
  lazy val e1=e
  import e1._  //compiles
  def doMath{  println(x*y)}
}


class mul(e: => IEntity) extends Math(e) { // Math Functions
  import e._  //does not compile
  def doMath{  println(x*y)}
}

-----Ursprüngliche Nachricht-----
Von: news [mailto:news@...] Im Auftrag von Jesper Nordenberg
Gesendet: Donnerstag, 5. November 2009 14:57
An: scala-user@...
Betreff: [scala-user] Re: AW: dynamic mixin? working but ugly example

Kubitz wrote:

> Explanation:
> I want to have a object that calculates me values.
> Where the Data comes wrong and the actual math should be separated
> The algorithms that are used should be selected at runtime
> The examples should do at runtime:
> 1.Select two algorithms
> 2.fill in the data
> 3.evaluate the functions
> Sounds like I want to "mix" datas and algorithms. So "mixin" should be usefull but I don’t get how.
>
> Your workaround with the functions would imply that I need to prefix the "x" with the parameter "d" (d.x). Unfortunatly this would make the functionbodys ugly. Plus: actually I even can not(!)insert that prefix (since the function body is created by a blackbox - I may only define the box around the body)

How about this then:

scala> class Data { val x = 1.0; val y = 2.0 }
defined class Data

scala> def fn(d : Data) = {import d._; x + y}
fn: (d: Data)Double

/Jesper Nordenberg


AW: dynamic mixin? solved

by Kubitz, Jörg :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Some parts of this message have been removed. Learn more about Nabble's security policy.

Thanks for all the Hints!

Finally I got an working Source and I have to say that it is smaller and looks much cleaner than the old Java code (However it was hard to get there).

 

Just if anybody is interested in the solution:

 

trait IData {// Interface

  def x:Double

  def y:Double

}

trait IMath {// Interface

  def math():Double 

}

trait IMath2 {// Interface

  def math2():Double 

}

trait IEntity extends IData with IMath with IMath2{ // Interface

}

trait DataAspekt { // Data Part

  var x:Double=4

  var y:Double=5

  def load{

    x=10

    y=20

  }

}

 

trait MathAspekt extends IEntity { // Math algorithms 1

  def m1:IMath

  def math()=m1.math

}

 

trait MathAspekt2 extends IEntity { // Math algorithms 2

  def m2:IMath2

  def math2()=m2.math2

}

 

class NullMath(e: IEntity) extends IMath { // Math 1 Default algorithm

  def math= Double.NaN

}

class mul(e: IEntity) extends IMath { // Math 1 algorithm

  import e._

  def math= x*y

}

 

class NullMath2(e: IEntity) extends IMath2 { // Math 2 Default algorithm

  def math2= Double.NaN

}

 

class funny(e: IEntity) extends IMath2 { // Math 2 algorithm

  import e._

  def math2= x*math

}

 

class  Entity() extends DataAspekt with MathAspekt with MathAspekt2 {

      var m1:IMath=new NullMath(this)

      var m2:IMath2=new NullMath2(this)

}

 

class c1 {

  var e:Entity=new Entity

  e.m1=new mul(e)

  e.m2=new funny(e)

  e.load

  println(e.math) // 200

  println(e.math2) // 2000

}

 

object t1 extends Application {

  println("hello")

  new c1

}

< Prev | 1 - 2 | Next >