Re: [akka-user] Creating Actor w/ Props having Value Class Argument(s)

2016-07-23 Thread Johan Andrén
Hi Kevin,

If you scroll down a bit from where you linked into the docs you can see 
the variations of prop factory methods and how to make it work with a value 
class 
(http://doc.akka.io/docs/akka/current/scala/actors.html#Value_classes_as_constructor_arguments)

Reflection is used when you do not provide a factory function to props but 
rather a class name and a list of constructor parameters. If all you have 
is a class name and a bunch of objects, the only way you can invoke a 
constructor is through reflection (or possibly a macro)

Both variations have their shortcomings, factory functions are hard to 
serialize and can easily close over surrounding scope by mistake, class + 
objects serializes fine but looses a bit of type safety - you won't know 
until runtime if you put the wrong number or wrong types of arguments in 
the list.

--
Johan

On Saturday, July 23, 2016 at 4:35:30 AM UTC+2, Kevin Meredith wrote:
>
> Thanks for the minimal example, Konrad. 
>
> On a side note, why is it necessary to use reflection (or possibly a 
> macro) in order to construct an Actor? 
>
> On Wednesday, July 20, 2016 at 11:48:15 AM UTC-4, Konrad Malawski wrote:
>>
>> This basically demonstrates the core of the problem:
>>
>> scala> class Meter(val m: Int) extends AnyVal
>> defined class Meter  
>> // in runtime you have *no way* to check if it's an AnyVal extending class 
>> or not. We would have to use macros. (scala reflection is a bit iffy...)
>>
>> scala> val m = new Meter(12)
>> m: Meter = Meter@c
>>
>> scala> class Act(m: Meter)
>> defined class Act
>>
>> scala> classOf[Act].getDeclaredConstructors.head
>> res23: java.lang.reflect.Constructor[_] = public Act(int)
>> // constructor takes int as you can see
>> // it is the ONLY constructor this class has - you can't pass in a Meter 
>> instance.
>>
>> scala> classOf[Act].getDeclaredConstructors.head.newInstance(new Integer(12))
>> res24: Any = Act@572db5ee
>>
>> scala> classOf[Act].getDeclaredConstructors.head.newInstance(new 
>> Meter(12).asInstanceOf[Object])
>> java.lang.IllegalArgumentException: argument type mismatch
>>   at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
>>   at 
>> sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
>>   at 
>> sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
>>   at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
>>   ... 32 elided
>> // NOPE.
>>
>>
>> // because if effectively is exactly the same as:
>> scala> class Act2(m: Int)
>> defined class Act2
>>
>> scala> classOf[Act2].getDeclaredConstructors.head
>> res27: java.lang.reflect.Constructor[_] = public Act2(int)
>>
>> scala>  classOf[Act].getDeclaredConstructors.head.newInstance(new 
>> Integer(12))
>> res28: Any = Act@50f65fe0
>>
>> scala>  classOf[Act].getDeclaredConstructors.head.newInstance(new 
>> Meter(12).asInstanceOf[Object])
>> java.lang.IllegalArgumentException: argument type mismatch
>>   at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
>>   at 
>> sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
>>   at 
>> sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
>>   at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
>>   ... 32 elided
>>
>>
>> So we’d be forced into guesswork about “hmmm! it has one field, maybe if 
>> we take the field out of that instance it will work!”. Which is weird, so 
>> we choose not to do this.
>>
>> If you have a solution to this problem I’d love to hear it :-)
>>
>>
>>
>> -- 
>> Konrad `ktoso` Malawski
>> Akka  @ Lightbend 
>>
>> On 20 July 2016 at 17:29:15, Kevin Meredith (kevin.m@gmail.com) 
>> wrote:
>>
>> Hi Konrad - 
>>
>> Could you please give me an example that demonstrates the "so we can't 
>> ..."?
>>
>> >AnyVal is a Scala compiler optimisation, so we can't figure out the 
>> right constructor (always / safely) in runtime.
>>
>> Thanks
>>
>> On Wednesday, July 20, 2016 at 9:44:33 AM UTC-4, Konrad Malawski wrote: 
>>>
>>> Because the type (wrapper) is not there at runtime.
>>> AnyVal is a Scala compiler optimisation, so we can't figure out the 
>>> right constructor (always / safely) in runtime.
>>>
>>> A resolution would be to "if thing has one field, and that value matches 
>>> we use that field" which could lead to very weird behaviour sometimes.
>>>
>>> -- 
>>> Konrad `ktoso` Malawski
>>> Akka  @ Lightbend 
>>>
>>> On 20 July 2016 at 15:40:08, Kevin Meredith (kevin.m@gmail.com) 
>>> wrote:
>>>
>>> Looking at the Akka docs 
>>>  for creating 
>>> an Actor:
>>>
>>> The recommended approach to create the actor Props is not supported for 
>>> cases when the actor constructor takes value classes as arguments.
>>>
>>> Please 

Re: [akka-user] Creating Actor w/ Props having Value Class Argument(s)

2016-07-22 Thread Kevin Meredith
Thanks for the minimal example, Konrad. 

On a side note, why is it necessary to use reflection (or possibly a macro) 
in order to construct an Actor? 

On Wednesday, July 20, 2016 at 11:48:15 AM UTC-4, Konrad Malawski wrote:
>
> This basically demonstrates the core of the problem:
>
> scala> class Meter(val m: Int) extends AnyVal
> defined class Meter  
> // in runtime you have *no way* to check if it's an AnyVal extending class or 
> not. We would have to use macros. (scala reflection is a bit iffy...)
>
> scala> val m = new Meter(12)
> m: Meter = Meter@c
>
> scala> class Act(m: Meter)
> defined class Act
>
> scala> classOf[Act].getDeclaredConstructors.head
> res23: java.lang.reflect.Constructor[_] = public Act(int)
> // constructor takes int as you can see
> // it is the ONLY constructor this class has - you can't pass in a Meter 
> instance.
>
> scala> classOf[Act].getDeclaredConstructors.head.newInstance(new Integer(12))
> res24: Any = Act@572db5ee
>
> scala> classOf[Act].getDeclaredConstructors.head.newInstance(new 
> Meter(12).asInstanceOf[Object])
> java.lang.IllegalArgumentException: argument type mismatch
>   at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
>   at 
> sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
>   at 
> sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
>   at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
>   ... 32 elided
> // NOPE.
>
>
> // because if effectively is exactly the same as:
> scala> class Act2(m: Int)
> defined class Act2
>
> scala> classOf[Act2].getDeclaredConstructors.head
> res27: java.lang.reflect.Constructor[_] = public Act2(int)
>
> scala>  classOf[Act].getDeclaredConstructors.head.newInstance(new Integer(12))
> res28: Any = Act@50f65fe0
>
> scala>  classOf[Act].getDeclaredConstructors.head.newInstance(new 
> Meter(12).asInstanceOf[Object])
> java.lang.IllegalArgumentException: argument type mismatch
>   at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
>   at 
> sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
>   at 
> sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
>   at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
>   ... 32 elided
>
>
> So we’d be forced into guesswork about “hmmm! it has one field, maybe if 
> we take the field out of that instance it will work!”. Which is weird, so 
> we choose not to do this.
>
> If you have a solution to this problem I’d love to hear it :-)
>
>
>
> -- 
> Konrad `ktoso` Malawski
> Akka  @ Lightbend 
>
> On 20 July 2016 at 17:29:15, Kevin Meredith (kevin.m@gmail.com 
> ) wrote:
>
> Hi Konrad - 
>
> Could you please give me an example that demonstrates the "so we can't 
> ..."?
>
> >AnyVal is a Scala compiler optimisation, so we can't figure out the right 
> constructor (always / safely) in runtime.
>
> Thanks
>
> On Wednesday, July 20, 2016 at 9:44:33 AM UTC-4, Konrad Malawski wrote: 
>>
>> Because the type (wrapper) is not there at runtime.
>> AnyVal is a Scala compiler optimisation, so we can't figure out the right 
>> constructor (always / safely) in runtime.
>>
>> A resolution would be to "if thing has one field, and that value matches 
>> we use that field" which could lead to very weird behaviour sometimes.
>>
>> -- 
>> Konrad `ktoso` Malawski
>> Akka  @ Lightbend 
>>
>> On 20 July 2016 at 15:40:08, Kevin Meredith (kevin.m@gmail.com) 
>> wrote:
>>
>> Looking at the Akka docs 
>>  for creating an 
>> Actor:
>>
>> The recommended approach to create the actor Props is not supported for 
>> cases when the actor constructor takes value classes as arguments.
>>
>> Please explain the "is not supported ..." reason.
>>
>> Per 
>> http://stackoverflow.com/questions/38400433/function-to-accept-any-type-but-anyval,
>>  
>> I'm curious.
>>
>> Thanks.
>> --
>> >> Read the docs: http://akka.io/docs/
>> >> Check the FAQ: 
>> http://doc.akka.io/docs/akka/current/additional/faq.html
>> >> Search the archives: https://groups.google.com/group/akka-user
>> ---
>> You received this message because you are subscribed to the Google Groups 
>> "Akka User List" group.
>> To unsubscribe from this group and stop receiving emails from it, send an 
>> email to akka-user+...@googlegroups.com.
>> To post to this group, send email to akka...@googlegroups.com.
>> Visit this group at https://groups.google.com/group/akka-user.
>> For more options, visit https://groups.google.com/d/optout.
>>
>> --
> >> Read the docs: http://akka.io/docs/
> >> Check the FAQ: 
> http://doc.akka.io/docs/akka/current/additional/faq.html
> >> Search the archives: https://groups.google.com/group/akka-user
> 

Re: [akka-user] Creating Actor w/ Props having Value Class Argument(s)

2016-07-20 Thread Konrad Malawski
This basically demonstrates the core of the problem:

scala> class Meter(val m: Int) extends AnyVal
defined class Meter
// in runtime you have *no way* to check if it's an AnyVal extending
class or not. We would have to use macros. (scala reflection is a bit
iffy...)

scala> val m = new Meter(12)
m: Meter = Meter@c

scala> class Act(m: Meter)
defined class Act

scala> classOf[Act].getDeclaredConstructors.head
res23: java.lang.reflect.Constructor[_] = public Act(int)
// constructor takes int as you can see
// it is the ONLY constructor this class has - you can't pass in a
Meter instance.

scala> classOf[Act].getDeclaredConstructors.head.newInstance(new Integer(12))
res24: Any = Act@572db5ee

scala> classOf[Act].getDeclaredConstructors.head.newInstance(new
Meter(12).asInstanceOf[Object])
java.lang.IllegalArgumentException: argument type mismatch
  at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
  at 
sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
  at 
sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
  at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
  ... 32 elided
// NOPE.


// because if effectively is exactly the same as:
scala> class Act2(m: Int)
defined class Act2

scala> classOf[Act2].getDeclaredConstructors.head
res27: java.lang.reflect.Constructor[_] = public Act2(int)

scala>  classOf[Act].getDeclaredConstructors.head.newInstance(new Integer(12))
res28: Any = Act@50f65fe0

scala>  classOf[Act].getDeclaredConstructors.head.newInstance(new
Meter(12).asInstanceOf[Object])
java.lang.IllegalArgumentException: argument type mismatch
  at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
  at 
sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
  at 
sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
  at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
  ... 32 elided


So we’d be forced into guesswork about “hmmm! it has one field, maybe if we
take the field out of that instance it will work!”. Which is weird, so we
choose not to do this.

If you have a solution to this problem I’d love to hear it :-)



-- 
Konrad `ktoso` Malawski
Akka  @ Lightbend 

On 20 July 2016 at 17:29:15, Kevin Meredith (kevin.m.mered...@gmail.com)
wrote:

Hi Konrad -

Could you please give me an example that demonstrates the "so we can't ..."?

>AnyVal is a Scala compiler optimisation, so we can't figure out the right
constructor (always / safely) in runtime.

Thanks

On Wednesday, July 20, 2016 at 9:44:33 AM UTC-4, Konrad Malawski wrote:
>
> Because the type (wrapper) is not there at runtime.
> AnyVal is a Scala compiler optimisation, so we can't figure out the right
> constructor (always / safely) in runtime.
>
> A resolution would be to "if thing has one field, and that value matches
> we use that field" which could lead to very weird behaviour sometimes.
>
> --
> Konrad `ktoso` Malawski
> Akka  @ Lightbend 
>
> On 20 July 2016 at 15:40:08, Kevin Meredith (kevin.m@gmail.com
> ) wrote:
>
> Looking at the Akka docs
>  for creating an
> Actor:
>
> The recommended approach to create the actor Props is not supported for
> cases when the actor constructor takes value classes as arguments.
>
> Please explain the "is not supported ..." reason.
>
> Per
> http://stackoverflow.com/questions/38400433/function-to-accept-any-type-but-anyval,
> I'm curious.
>
> Thanks.
> --
> >> Read the docs: http://akka.io/docs/
> >> Check the FAQ:
> http://doc.akka.io/docs/akka/current/additional/faq.html
> >> Search the archives: https://groups.google.com/group/akka-user
> ---
> You received this message because you are subscribed to the Google Groups
> "Akka User List" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to akka-user+...@googlegroups.com .
> To post to this group, send email to akka...@googlegroups.com
> .
> Visit this group at https://groups.google.com/group/akka-user.
> For more options, visit https://groups.google.com/d/optout.
>
> --
>> Read the docs: http://akka.io/docs/
>> Check the FAQ:
http://doc.akka.io/docs/akka/current/additional/faq.html
>> Search the archives: https://groups.google.com/group/akka-user
---
You received this message because you are subscribed to the Google Groups
"Akka User List" group.
To unsubscribe from this group and stop receiving emails from it, send an
email to akka-user+unsubscr...@googlegroups.com.
To post to this group, send email to akka-user@googlegroups.com.
Visit this group at https://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.

-- 
>>  Read the docs: 

Re: [akka-user] Creating Actor w/ Props having Value Class Argument(s)

2016-07-20 Thread Kevin Meredith
Hi Konrad -

Could you please give me an example that demonstrates the "so we can't ..."?

>AnyVal is a Scala compiler optimisation, so we can't figure out the right 
constructor (always / safely) in runtime.

Thanks

On Wednesday, July 20, 2016 at 9:44:33 AM UTC-4, Konrad Malawski wrote:
>
> Because the type (wrapper) is not there at runtime.
> AnyVal is a Scala compiler optimisation, so we can't figure out the right 
> constructor (always / safely) in runtime.
>
> A resolution would be to "if thing has one field, and that value matches 
> we use that field" which could lead to very weird behaviour sometimes.
>
> -- 
> Konrad `ktoso` Malawski
> Akka  @ Lightbend 
>
> On 20 July 2016 at 15:40:08, Kevin Meredith (kevin.m@gmail.com 
> ) wrote:
>
> Looking at the Akka docs 
>  for creating an 
> Actor:
>
> The recommended approach to create the actor Props is not supported for 
> cases when the actor constructor takes value classes as arguments.
>
> Please explain the "is not supported ..." reason.
>
> Per 
> http://stackoverflow.com/questions/38400433/function-to-accept-any-type-but-anyval,
>  
> I'm curious.
>
> Thanks.
> --
> >> Read the docs: http://akka.io/docs/
> >> Check the FAQ: 
> http://doc.akka.io/docs/akka/current/additional/faq.html
> >> Search the archives: https://groups.google.com/group/akka-user
> ---
> You received this message because you are subscribed to the Google Groups 
> "Akka User List" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to akka-user+...@googlegroups.com .
> To post to this group, send email to akka...@googlegroups.com 
> .
> Visit this group at https://groups.google.com/group/akka-user.
> For more options, visit https://groups.google.com/d/optout.
>
>

-- 
>>  Read the docs: http://akka.io/docs/
>>  Check the FAQ: 
>> http://doc.akka.io/docs/akka/current/additional/faq.html
>>  Search the archives: https://groups.google.com/group/akka-user
--- 
You received this message because you are subscribed to the Google Groups "Akka 
User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to akka-user+unsubscr...@googlegroups.com.
To post to this group, send email to akka-user@googlegroups.com.
Visit this group at https://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.


Re: [akka-user] Creating Actor w/ Props having Value Class Argument(s)

2016-07-20 Thread Konrad Malawski
Because the type (wrapper) is not there at runtime.
AnyVal is a Scala compiler optimisation, so we can't figure out the right
constructor (always / safely) in runtime.

A resolution would be to "if thing has one field, and that value matches we
use that field" which could lead to very weird behaviour sometimes.

-- 
Konrad `ktoso` Malawski
Akka  @ Lightbend 

On 20 July 2016 at 15:40:08, Kevin Meredith (kevin.m.mered...@gmail.com)
wrote:

Looking at the Akka docs
 for creating an
Actor:

The recommended approach to create the actor Props is not supported for
cases when the actor constructor takes value classes as arguments.

Please explain the "is not supported ..." reason.

Per
http://stackoverflow.com/questions/38400433/function-to-accept-any-type-but-anyval,
I'm curious.

Thanks.
--
>> Read the docs: http://akka.io/docs/
>> Check the FAQ:
http://doc.akka.io/docs/akka/current/additional/faq.html
>> Search the archives: https://groups.google.com/group/akka-user
---
You received this message because you are subscribed to the Google Groups
"Akka User List" group.
To unsubscribe from this group and stop receiving emails from it, send an
email to akka-user+unsubscr...@googlegroups.com.
To post to this group, send email to akka-user@googlegroups.com.
Visit this group at https://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.

-- 
>>  Read the docs: http://akka.io/docs/
>>  Check the FAQ: 
>> http://doc.akka.io/docs/akka/current/additional/faq.html
>>  Search the archives: https://groups.google.com/group/akka-user
--- 
You received this message because you are subscribed to the Google Groups "Akka 
User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to akka-user+unsubscr...@googlegroups.com.
To post to this group, send email to akka-user@googlegroups.com.
Visit this group at https://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.


[akka-user] Creating Actor w/ Props having Value Class Argument(s)

2016-07-20 Thread Kevin Meredith


Looking at the Akka docs 
 for creating an 
Actor:

The recommended approach to create the actor Props is not supported for 
cases when the actor constructor takes value classes as arguments.

Please explain the "is not supported ..." reason.

Per 
http://stackoverflow.com/questions/38400433/function-to-accept-any-type-but-anyval,
 
I'm curious.

Thanks.

-- 
>>  Read the docs: http://akka.io/docs/
>>  Check the FAQ: 
>> http://doc.akka.io/docs/akka/current/additional/faq.html
>>  Search the archives: https://groups.google.com/group/akka-user
--- 
You received this message because you are subscribed to the Google Groups "Akka 
User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to akka-user+unsubscr...@googlegroups.com.
To post to this group, send email to akka-user@googlegroups.com.
Visit this group at https://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.