- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Anonymous constructors ...
Wed, 2009-03-18, 17:26
Hi,
I'd like to be able to write a class which constructs an object based on a parameterised type. At its crudest:
class A[T] { val t = new T } // this doesn't compile because T isn't a type
I can do it in C++ using
template< class T> class A { const T t = Make<T >::any(); };
where the default for Make<T> is
template< class T > struct Make { T any() { return T(); } };
Is there a Scala equivalent (or better)? I guess if the worst comes to the worst I could use java.lang.reflect?
Thanks in advance.
Simon
View this message in context: Anonymous constructors ...
Sent from the Scala - User mailing list archive at Nabble.com.
Wed, 2009-03-18, 18:07
#2
Re: Anonymous constructors ...
On Wed, Mar 18, 2009 at 9:25 AM, Simon Langley <simon@ouryard.net> wrote:
Hi,Scala's Manifest is undocumented and experimental... but:I'd like to be able to write a class which constructs an object based on a parameterised type. At its crudest:
class A[T] { val t = new T } // this doesn't compile because T isn't a type
I can do it in C++ using
template< class T> class A { const T t = Make<T >::any(); };
where the default for Make<T> is
template< class T > struct Make { T any() { return T(); } };
Is there a Scala equivalent (or better)? I guess if the worst comes to the worst I could use java.lang.reflect?
import scala.reflect.Manifest
def build[T](implicit m: Manifest[T]): T =
m.erasure.newInstance.asInstanceOf[T]
println(build[java.util.Date])
Thanks in advance.
Simon
View this message in context: Anonymous constructors ...
Sent from the Scala - User mailing list archive at Nabble.com.
--
Lift, the simply functional web framework http://liftweb.net
Beginning Scala http://www.apress.com/book/view/1430219890
Follow me: http://twitter.com/dpp
Git some: http://github.com/dpp
Wed, 2009-03-18, 18:07
#3
Re: Anonymous constructors ...
Good point. However, beware that C++ statically guarantees that new T is valid whereas using manifests you won't know that there's no default constructor until runtime. On the plus side, you don't have to write factories.
On Wed, Mar 18, 2009 at 9:57 AM, David Pollak <feeder.of.the.bears@gmail.com> wrote:
On Wed, Mar 18, 2009 at 9:57 AM, David Pollak <feeder.of.the.bears@gmail.com> wrote:
On Wed, Mar 18, 2009 at 9:25 AM, Simon Langley <simon@ouryard.net> wrote:
Hi,Scala's Manifest is undocumented and experimental... but:I'd like to be able to write a class which constructs an object based on a parameterised type. At its crudest:
class A[T] { val t = new T } // this doesn't compile because T isn't a type
I can do it in C++ using
template< class T> class A { const T t = Make<T >::any(); };
where the default for Make<T> is
template< class T > struct Make { T any() { return T(); } };
Is there a Scala equivalent (or better)? I guess if the worst comes to the worst I could use java.lang.reflect?
import scala.reflect.Manifest
def build[T](implicit m: Manifest[T]): T =
m.erasure.newInstance.asInstanceOf[T]
println(build[java.util.Date])
Thanks in advance.
Simon
View this message in context: Anonymous constructors ...
Sent from the Scala - User mailing list archive at Nabble.com.
--
Lift, the simply functional web framework http://liftweb.net
Beginning Scala http://www.apress.com/book/view/1430219890
Follow me: http://twitter.com/dpp
Git some: http://github.com/dpp
Wed, 2009-03-18, 18:17
#4
Re: Anonymous constructors ...
class A[T] says "for all types T, there is a class A[T]. template<class T> class A says "you can have a class<T> provided it compiles." In other words, C++ hides requirements on its template parameters in the code.
There isn't a direct equivalent in Scala, but you can get reasonably close using implicits to state the requirement.
class A[T](implicit factory : () => T) {
val t = factory
}
Disadvantage: you have to write a factory function for every T you intend to use
class Foo
implicit def factory() = new Foo
val AFoo = new A[Foo]
Advantage1: you can use things that don't have a default constructor
class Bar(x : Int)
implicit def factory() = new Bar(0)
val ABar = new A[Bar]
Advantage2: you can explicitly decide on something else when you need to or when you don't want to bother with a factory
val ABar2 = new A()(() => new Bar(2))
Although, if that's a common use case, it's worthwhile adding an more syntactically pleasant mechanism using call-by-name
object A {
def apply[T](arg : => T) = new A()(() => arg)
}
val ABar3 = A{new Bar(3)}
On Wed, Mar 18, 2009 at 9:25 AM, Simon Langley <simon@ouryard.net> wrote:
There isn't a direct equivalent in Scala, but you can get reasonably close using implicits to state the requirement.
class A[T](implicit factory : () => T) {
val t = factory
}
Disadvantage: you have to write a factory function for every T you intend to use
class Foo
implicit def factory() = new Foo
val AFoo = new A[Foo]
Advantage1: you can use things that don't have a default constructor
class Bar(x : Int)
implicit def factory() = new Bar(0)
val ABar = new A[Bar]
Advantage2: you can explicitly decide on something else when you need to or when you don't want to bother with a factory
val ABar2 = new A()(() => new Bar(2))
Although, if that's a common use case, it's worthwhile adding an more syntactically pleasant mechanism using call-by-name
object A {
def apply[T](arg : => T) = new A()(() => arg)
}
val ABar3 = A{new Bar(3)}
On Wed, Mar 18, 2009 at 9:25 AM, Simon Langley <simon@ouryard.net> wrote:
Hi,I'd like to be able to write a class which constructs an object based on a parameterised type. At its crudest:
class A[T] { val t = new T } // this doesn't compile because T isn't a type
I can do it in C++ using
template< class T> class A { const T t = Make<T >::any(); };
where the default for Make<T> is
template< class T > struct Make { T any() { return T(); } };
Is there a Scala equivalent (or better)? I guess if the worst comes to the worst I could use java.lang.reflect?
Thanks in advance.
Simon
View this message in context: Anonymous constructors ...
Sent from the Scala - User mailing list archive at Nabble.com.
Wed, 2009-03-18, 18:17
#5
Re: Anonymous constructors ...
> class A[T] says "for all types T, there is a class A[T]. template T> class A says "you can have a class provided it compiles."
James, thanks for that. You have great clarity, for a noob.
Wed, 2009-03-18, 18:27
#6
Re: Anonymous constructors ...
Eastsun wrote:
>
> You can't do it even if you use java.lang.reflect ...
>
I see what you mean - I was thinking I could use the class to get an
instance ... but T isn't a class. Ah well, so it goes.
Thanks,
Simon
Wed, 2009-03-18, 18:37
#7
Re: Anonymous constructors ...
On Wed, Mar 18, 2009 at 10:02 AM, Ricky Clarkson <ricky.clarkson@gmail.com> wrote:
> class A[T] says "for all types T, there is a class A[T]. template<class
> T> class A says "you can have a class<T> provided it compiles."
James, thanks for that. You have great clarity,
Geez. Let me write that whole bit again
Scala's class A[T] says "for all types T, there is a class A[T]." C++'s template<class T> class A says "for all T there is a class A<T> provided the template expansion compiles. Good luck."
for a noob.
Some times words hurt. :'(
Thu, 2009-03-19, 00:47
#8
Re: Anonymous constructors ...
For what it's worth, C# gets around this by having a type constraint -
where T : new() means that new T() is guaranteed to succeed. It's not
very general, but that doesn't mean it couldn't be.
2009/3/18 James Iry :
> Good point. However, beware that C++ statically guarantees that new T is
> valid whereas using manifests you won't know that there's no default
> constructor until runtime. On the plus side, you don't have to write
> factories.
>
> On Wed, Mar 18, 2009 at 9:57 AM, David Pollak
> wrote:
>>
>>
>> On Wed, Mar 18, 2009 at 9:25 AM, Simon Langley wrote:
>>>
>>> Hi,
>>>
>>> I'd like to be able to write a class which constructs an object based on
>>> a parameterised type. At its crudest:
>>> class A[T] { val t = new T } // this doesn't compile because T isn't a
>>> type
>>>
>>> I can do it in C++ using
>>> template< class T> class A { const T t = Make::any(); };
>>>
>>> where the default for Make is
>>> template< class T > struct Make { T any() { return T(); } };
>>>
>>> Is there a Scala equivalent (or better)? I guess if the worst comes to
>>> the worst I could use java.lang.reflect?
>>
>> Scala's Manifest is undocumented and experimental... but:
>>
>> import scala.reflect.Manifest
>>
>> def build[T](implicit m: Manifest[T]): T =
>> m.erasure.newInstance.asInstanceOf[T]
>>
>> println(build[java.util.Date])
>>
>>
>>
>>>
>>> Thanks in advance.
>>>
>>> Simon
>>>
>>> ________________________________
>>> View this message in context: Anonymous constructors ...
>>> Sent from the Scala - User mailing list archive at Nabble.com.
>>
>>
>>
>> --
>> Lift, the simply functional web framework http://liftweb.net
>> Beginning Scala http://www.apress.com/book/view/1430219890
>> Follow me: http://twitter.com/dpp
>> Git some: http://github.com/dpp
>
>
You can't do it even if you use java.lang.reflect ...
-----
My scala solutions for http://projecteuler.net/ Project Euler problems:
http://eastsun.javaeye.com/category/34059 Click here