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

Type aliases in arbitrary blocks.

5 replies
Kris Nuttycombe
Joined: 2009-01-16,
User offline. Last seen 42 years 45 weeks ago.
Hi, all,
As my types become more sophisticated, I find myself really wishing that type aliases could be defined in arbitrary blocks, not just as members of traits, classes, or objects.
As a workaround for this lack, I find myself often writing something like this:
trait Value[A] {  def value: A}
def foo: A = new Value[A] {   type ER[R] = Either[String, R]  type F[A] = SomeOtherTypeThatTakesATypeConstructorAsItsFirstArg[ER, A]    val value = { /* use F as a type constructor somewhere in here */ } }.value
Why is it that local type aliases are not supported, and is there any possibility of adding this feature to the language? I know about the inline type lambda trick, of course, but when you start encountering nested lambdas things get a bit hairy to read. Of course, a better syntax for writing type lambdas would also be desirable, but even in the presence of such a syntax enhancement nested type lambdas would still be less readable than if type aliases could be defined in method bodies (or arbitrary blocks):
def foo: A = {  type ER[R] = Either[String, R]  type F[A] = SomeOtherTypeThatTakesATypeConstructorAsItsFirstArg[ER, A]
  // use F as a type constructor }
Lex
Joined: 2010-02-28,
User offline. Last seen 42 years 45 weeks ago.
Re: Type aliases in arbitrary blocks.
Local type aliases are supported. Double check your signatures.


On Sat, Jan 14, 2012 at 6:44 PM, Kris Nuttycombe <kris [dot] nuttycombe [at] gmail [dot] com> wrote:
Hi, all,
As my types become more sophisticated, I find myself really wishing that type aliases could be defined in arbitrary blocks, not just as members of traits, classes, or objects.
As a workaround for this lack, I find myself often writing something like this:
trait Value[A] {  def value: A}
def foo: A = new Value[A] {   type ER[R] = Either[String, R]  type F[A] = SomeOtherTypeThatTakesATypeConstructorAsItsFirstArg[ER, A]    val value = { /* use F as a type constructor somewhere in here */ } }.value
Why is it that local type aliases are not supported, and is there any possibility of adding this feature to the language? I know about the inline type lambda trick, of course, but when you start encountering nested lambdas things get a bit hairy to read. Of course, a better syntax for writing type lambdas would also be desirable, but even in the presence of such a syntax enhancement nested type lambdas would still be less readable than if type aliases could be defined in method bodies (or arbitrary blocks):
def foo: A = {  type ER[R] = Either[String, R]  type F[A] = SomeOtherTypeThatTakesATypeConstructorAsItsFirstArg[ER, A]
  // use F as a type constructor }

Kris Nuttycombe
Joined: 2009-01-16,
User offline. Last seen 42 years 45 weeks ago.
Re: Type aliases in arbitrary blocks.
How odd. I was sure that I'd tried that. Thanks.

On Sat, Jan 14, 2012 at 6:23 PM, Aleksey Nikiforov <lexn82 [at] gmail [dot] com> wrote:
Local type aliases are supported. Double check your signatures.


On Sat, Jan 14, 2012 at 6:44 PM, Kris Nuttycombe <kris [dot] nuttycombe [at] gmail [dot] com> wrote:
Hi, all,
As my types become more sophisticated, I find myself really wishing that type aliases could be defined in arbitrary blocks, not just as members of traits, classes, or objects.
As a workaround for this lack, I find myself often writing something like this:
trait Value[A] {  def value: A}
def foo: A = new Value[A] {   type ER[R] = Either[String, R]  type F[A] = SomeOtherTypeThatTakesATypeConstructorAsItsFirstArg[ER, A]    val value = { /* use F as a type constructor somewhere in here */ } }.value
Why is it that local type aliases are not supported, and is there any possibility of adding this feature to the language? I know about the inline type lambda trick, of course, but when you start encountering nested lambdas things get a bit hairy to read. Of course, a better syntax for writing type lambdas would also be desirable, but even in the presence of such a syntax enhancement nested type lambdas would still be less readable than if type aliases could be defined in method bodies (or arbitrary blocks):
def foo: A = {  type ER[R] = Either[String, R]  type F[A] = SomeOtherTypeThatTakesATypeConstructorAsItsFirstArg[ER, A]
  // use F as a type constructor }


Sophie
Joined: 2011-11-10,
User offline. Last seen 42 years 45 weeks ago.
Re: Type aliases in arbitrary blocks.
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; line-height: 15.0px; font: 14.0px Helvetica} p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; line-height: 15.0px; font: 14.0px Helvetica; min-height: 17.0px} p.p3 {margin: 0.0px 0.0px 0.0px 12.0px; font: 16.0px Times; color: #161616} p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; line-height: 17.0px; font: 14.0px Helvetica; min-height: 17.0px} p.p5 {margin: 0.0px 0.0px 0.0px 0.0px; line-height: 17.0px; font: 14.0px Helvetica} p.p6 {margin: 0.0px 0.0px 0.0px 12.0px; font: 16.0px Times; color: #161616; min-height: 19.0px} span.s1 {direction: ltr; unicode-bidi: embed} span.s2 {text-decoration: underline ; direction: ltr; unicode-bidi: embed}

On 2012-01-14 19:23:24 -0600, Aleksey Nikiforov said:


Local type aliases are supported. Double check your signatures.


Any idea why they are not supported at the top level of a package (i.e. without a package object)?




On Sat, Jan 14, 2012 at 6:44 PM, Kris Nuttycombe <kris [dot] nuttycombe [at] gmail [dot] com> wrote:

Hi, all,


As my types become more sophisticated, I find myself really wishing that type aliases could be defined in arbitrary blocks, not just as members of traits, classes, or objects.


As a workaround for this lack, I find myself often writing something like this:


trait Value[A] {

  def value: A

}


def foo: A = new Value[A] {

  type ER[R] = Either[String, R]

  type F[A] = SomeOtherTypeThatTakesATypeConstructorAsItsFirstArg[ER, A]

  

  val value = { /* use F as a type constructor somewhere in here */ }

}.value


Why is it that local type aliases are not supported, and is there any possibility of adding this feature to the language? I know about the inline type lambda trick, of course, but when you start encountering nested lambdas things get a bit hairy to read. Of course, a better syntax for writing type lambdas would also be desirable, but even in the presence of such a syntax enhancement nested type lambdas would still be less readable than if type aliases could be defined in method bodies (or arbitrary blocks):


def foo: A = {

  type ER[R] = Either[String, R]

  type F[A] = SomeOtherTypeThatTakesATypeConstructorAsItsFirstArg[ER, A]


  // use F as a type constructor

}


Lex
Joined: 2010-02-28,
User offline. Last seen 42 years 45 weeks ago.
Re: Re: Type aliases in arbitrary blocks.
Probably due to JVM compatibility baggage.

On Sun, Jan 15, 2012 at 8:44 PM, Sophie <itsme213 [at] hotmail [dot] com> wrote:

Any idea why they are not supported at the top level of a package (i.e. without a package object)?

 
Ken Scambler
Joined: 2009-11-07,
User offline. Last seen 42 years 45 weeks ago.
Re: Re: Type aliases in arbitrary blocks.
On 16 January 2012 15:57, Aleksey Nikiforov <lexn82 [at] gmail [dot] com> wrote:
Probably due to JVM compatibility baggage.

I had always assumed this, but I've always wondered why the package object couldn't just be generated in that case.   IMO it seems inelegant having three separate concepts, with slightly different rules -- packages, objects and package objects -- for essentially the same thing, barring JVM representational issues. 

Ken

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