This page is no longer maintained — Please continue to the home page at www.scala-lang.org

Re: R: Re: Type parameters as member methods.

No replies
H-star Development
Joined: 2010-04-14,
User offline. Last seen 2 years 26 weeks ago.

Am 06.01.2012 19:08, schrieb edmondo [dot] porcu [at] gmail [dot] com:
> Yes that works... But I need to pass a dummy value while I just need the type...
> Inviato da BlackBerry(R) Wireless Handheld

no you don't :)
scala = magic
->

object TypedBuilder {
abstract class Foo {
type bar
val foobar: bar
}

abstract class Foo2 {
type bar
var foobar = null.asInstanceOf[bar]
}

def build[X](x:X) = new Foo {
type bar = X
val foobar = x
}

def build[X] = new Foo2 {
type bar = X
}

def main(args: Array[String]) {
val foo = build("yes it works")
val bar = build(12345)
val withoutDummyValue = build[BigInt]
withoutDummyValue.foobar = 12345
println(foo + bar.toString + withoutDummyValue.toString)
}
}

>
> -----Original Message-----
> From: HamsterofDeath
> Sender: scala-user [at] googlegroups [dot] com
> Date: Fri, 06 Jan 2012 19:03:33
> To:
> Subject: Re: [scala-user] Type parameters as member methods.
>
> Am 06.01.2012 18:40, schrieb Edmondo Porcu:
>> Dear all,
>> assuming I have an abstract class with an abstract type T.
>>
>> Can I imagine to have a builder method that receives a concrete type
>> for T, and builder a concrete instance of the class?
>>
>> Best Regards
>>
>> Edmondo
> like that?
>
> object TypedBuilder {
> abstract class Foo {
> type bar
> val foobar: bar
> }
>
> def build[X](x:X) = new Foo {
> type bar = X
> val foobar = x
> }
>
> def main(args: Array[String]) {
> val foo = build("yes it works")
> val bar = build(12345)
> println(foo + bar.toString)
> }
> }
>
> afaik types exist only at compile time

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland