- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Generic default class
Tue, 2009-04-07, 18:24
Hi, in Java when you have a class with generic parameters and you create it
without parameters it fallsback to the default one
Considere this:
public class Help {
public Help(T s) {
System.out.println(s.endsWith("o"));//s can always be used as a
string
}
public static void main(String[] args) {
Help h=new Help("foo");
Help h2= new Help("bar");
}
}
Its just an example, if i didnt specify the extends String it would still
work without parameters falling back to Object
Cant find a way todo this in Scala it always give me error when i create a
lass with no type parameters
Tue, 2009-04-07, 19:57
#2
Re: Generic default class
2009/4/7 porfirio :
>
> Hi, in Java when you have a class with generic parameters and you create it
> without parameters it fallsback to the default one
> Considere this:
> public class Help {
> public Help(T s) {
> System.out.println(s.endsWith("o"));//s can always be used as a
> string
> }
> public static void main(String[] args) {
> Help h=new Help("foo");
> Help h2= new Help("bar");
> }
> }
>
> Its just an example, if i didnt specify the extends String it would still
> work without parameters falling back to Object
This is not an accurate description of what happens in Java. The truth
is much darker.
http://www.angelikalanger.com/GenericsFAQ/FAQSections/ParameterizedTypes...
Tue, 2009-04-07, 20:27
#3
Re: Generic default class
porfirio wrote:
>
> Hi, in Java when you have a class with generic parameters and you create
> it without parameters it fallsback to the default one
> Considere this:
> public class Help {
> public Help(T s) {
> System.out.println(s.endsWith("o"));//s can always be used as a
> string
> }
> public static void main(String[] args) {
> Help h=new Help("foo");
> Help h2= new Help("bar");
> }
> }
>
> Its just an example, if i didnt specify the extends String it would still
> work without parameters falling back to Object
>
> Cant find a way todo this in Scala it always give me error when i create a
> lass with no type parameters
>
This works fine:
class Help[T <: String](s:T) {
println(s.endsWith("o"))//s can always be used as a string
}
object Help {
def test {
val h=new Help[String]("foo");
val h2= new Help("bar");
}
}
>true
>false
Of course this works only if the right generic bound can be derived by type
inference. Or do you mean something else?
BTW: I would consider your Java example as bad style: It's not a "fall
back", it' only there and should be only used for backward compatibility
(e.g. when working with old libraries etc).
Daniel
Tue, 2009-04-07, 20:37
#4
Re: Generic default class
Thank you guys for the info :) i didnt knew that trick
Its not the same thing but it does the job ;)
I just wish that if we didnt pass any type we could use a predifined one,
but looks like in scala its Nothing
class MyList[T <: Any] extends ArrayList[T]{
def <<(t:T):MyList[T]={add(t);this}
}
i would like to do
var foo=new MyList
foo << 10 << "foo" << Color.red << etc
It can still be done by doing var foo=new MyList[Any], no big deal
Tue, 2009-04-07, 22:17
#5
Re: Generic default class
porfirio wrote:
>
> Thank you guys for the info :) i didnt knew that trick
> Its not the same thing but it does the job ;)
>
> I just wish that if we didnt pass any type we could use a predifined one,
> but looks like in scala its Nothing
>
> class MyList[T <: Any] extends ArrayList[T]{
> def <<(t:T):MyList[T]={add(t);this}
> }
>
> i would like to do
>
> var foo=new MyList
>
> foo << 10 << "foo" << Color.red << etc
>
> It can still be done by doing var foo=new MyList[Any], no big deal
>
If you want *always* MyList[Any], you don't need generics at all. But if you
have a MyList with a more specific type (say MyList[String]), your <<
operation won't work, because it has somehow to "widen" the type when you
add suddenly something else like a Date. The closest I could come up with is
this terrible wasteful and confusing implementation:
object MyList {
def apply[T](ts:T*) = {
val list = new MyList[T]
ts.foreach(list.add(_))
list
}
}
class MyList[T] extends ArrayList[T]{
def <<[U >: T](u:U):MyList[U] = {
val list = new MyList[U]
list.addAll(this)
list.add(u)
list
}
}
object Test{
def main(args:Array[String]){
val foo = MyList()
println(foo << 10 << "foo" << java.awt.Color.red) //OK
val bar = MyList("a","b","c")
println(bar << "x" << "y" << "z") //OK
println(bar) //note that this prints only ["a","b","c"]
//bar.add(27) won't work because bar is a MyList[String]
}
}
println(s endsWith "o")
}
object Test {
def main(args: Array[String]) {
val h = new Help("foo")
val h2 = new Help("bar")
()
}
}
Save, compile with scalac, and run "scala Test".
alex
On Tue, Apr 7, 2009 at 10:23 AM, porfirio <porfirio.ribeiro@sapo.pt> wrote: