Received: by 10.68.130.35 with SMTP id ob3mr7892797pbb.8.1332420970849; Thu, 22 Mar 2012 05:56:10 -0700 (PDT) X-BeenThere: scala-debate@googlegroups.com Received: by 10.68.54.170 with SMTP id k10ls4927930pbp.0.gmail; Thu, 22 Mar 2012 05:56:09 -0700 (PDT) Received: by 10.68.228.229 with SMTP id sl5mr1768979pbc.12.1332420969643; Thu, 22 Mar 2012 05:56:09 -0700 (PDT) Date: Thu, 22 Mar 2012 05:56:08 -0700 (PDT) From: Denis Nevmerzhitsky To: scala-debate@googlegroups.com Message-ID: <30778106.79.1332420968952.JavaMail.geo-discussion-forums@ynjk1> In-Reply-To: <24181042.488.1332274921998.JavaMail.geo-discussion-forums@ynes7> References: <4F51DB74.6060508@gmx.de> <24181042.488.1332274921998.JavaMail.geo-discussion-forums@ynes7> Subject: Re: pattern matching in method/function parameters MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----=_Part_77_11308581.1332420968946" ------=_Part_77_11308581.1332420968946 Content-Type: multipart/alternative; boundary="----=_Part_78_2817895.1332420968946" ------=_Part_78_2817895.1332420968946 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Hi! Was thinking about this. According to reference we have: Param ::=3D {Annotation} id [=E2=80=98:=E2=80=99 ParamType] [=E2=80=98=3D= =E2=80=99 Expr] It can be changed to another defenition: Param ::=3D {Annotation} ParamDcl [=E2=80=98:=E2=80=99 ParamType] [=E2=80= =98=3D=E2=80=99 Expr] ParamDcl ::=3D id | Pattern2 And such pattern definition will be replaced this way: def foo(: Type) =3D ...body... into:=20 def foo($x: Type) =3D { val =3D $x ... body... } So it can looks like this: def foo((head :: tail): List[String]) def foo(a: String, (top :: next :: rest): List[]) def foo(a: String, state @ State(b, c): State) def foo((a, b): (String, Int)) def foo((Email(user, domain), b): (String, Int)) =D0=B2=D1=82=D0=BE=D1=80=D0=BD=D0=B8=D0=BA, 20 =D0=BC=D0=B0=D1=80=D1=82=D0= =B0 2012 =D0=B3. 22:22:01 UTC+2 =D0=BF=D0=BE=D0=BB=D1=8C=D0=B7=D0=BE=D0=B2= =D0=B0=D1=82=D0=B5=D0=BB=D1=8C Denis Nevmerzhitsky=20 =D0=BD=D0=B0=D0=BF=D0=B8=D1=81=D0=B0=D0=BB: > > Hi! > > I think it would be useful tho have smth like this: > > case class State(a: Int, b: String) > > def someFunction(State(a, b), c: String) =3D "%s: [%d] %s" format (c, a, = b) > > val state =3D State(1, "a") > > val res =3D someFunction(state, "str") // will be equal to "str: [1] a"= =20 > > > So argument can be not only "*identifier: Type"* but "*extractor expr*"= =20 > too. Here are more examples: > > foo(head :: tail) > > foo(a: String, top :: next :: rest) > > foo(a: String, state @ State(b, c)) > > > Our someFunction will be translated to: > > def someFunction(state: State, c: String) =3D { > > val State(a, b) =3D state > > "%s: [%d] %s" format (c, a, b) > > } > > > What do you think? > > =D1=81=D1=83=D0=B1=D0=B1=D0=BE=D1=82=D0=B0, 3 =D0=BC=D0=B0=D1=80=D1=82=D0= =B0 2012 =D0=B3. 10:51:00 UTC+2 =D0=BF=D0=BE=D0=BB=D1=8C=D0=B7=D0=BE=D0=B2= =D0=B0=D1=82=D0=B5=D0=BB=D1=8C HamsterofDeath=20 > =D0=BD=D0=B0=D0=BF=D0=B8=D1=81=D0=B0=D0=BB: >> >> this is what i can do: >> >> tupleList.collect({case (a,b,c) =3D> ...} >> >> this is what i want to do: >> >> tupleList.forEach((a,b,c)) =3D> .... >> >> is there a reason for this restriction, is it planned, is it not, can i >> do it later with macros? >> >> ------=_Part_78_2817895.1332420968946 Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: quoted-printable Hi!

Was thinking about this.

Ac= cording to reference we have:
Param ::=3D {Annotation} id [= =E2=80=98:=E2=80=99 ParamType] [=E2=80=98=3D=E2=80=99 Expr]

It can be changed to another defenition:
<= div>
Param ::=3D {Annotation} ParamDcl [=E2=80=98:=E2=80=99 ParamType] = [=E2=80=98=3D=E2=80=99 Expr]
ParamDcl ::=3D id | Pa= ttern2

And such pattern definition wi= ll be replaced this way:
def foo(<Pattern>: Type) =3D .= ..body...
into: 
def foo($x: Type) =3D = {
  val <Pattern> =3D $x
  ..= . body...
}

So it can looks like this:
de= f foo((head :: tail): List[String])
def foo(a: String, (top :: next :: rest): List[= ])
def f= oo(a: String, state @ State(b, c): State)
def foo((a, b): (String, Int))
def foo((Email(user, domain), b): (String, Int))<= br>

=D0=B2=D1=82=D0=BE=D1=80=D0=BD=D0=B8=D0=BA, 2= 0 =D0=BC=D0=B0=D1=80=D1=82=D0=B0 2012 =D0=B3. 22:22:01 UTC+2 =D0=BF=D0= =BE=D0=BB=D1=8C=D0=B7=D0=BE=D0=B2=D0=B0=D1=82=D0=B5=D0=BB=D1=8C Denis Nevme= rzhitsky =D0=BD=D0=B0=D0=BF=D0=B8=D1=81=D0=B0=D0=BB:
Hi!

I think it would be useful tho= have smth like this:
case class State(a: Int, b: String)
=
def so= meFunction(State(a, b), c: String) =3D "%s: [%d] %s" format (c, a, b)
=
          val state =3D Sta= te(1, "a")
val res =3D someFunction(state, "str") // will be equal to "str: = [1] a" 

So argument can be not o= nly "identifier: Type" but "extractor expr" too. Here ar= e more examples:
foo(head :: tail)
foo(a: String, top :: next ::= rest)
foo(a: String, state @ State(b, c))

Our someFunction will be translated to:
def someFunction(s= tate: State, c: String) =3D {
  val State(a, b) =3D st= ate
  "%s: [%d] %s" format (c, a, b)
}

What do you think?

=D1=81= =D1=83=D0=B1=D0=B1=D0=BE=D1=82=D0=B0, 3 =D0=BC=D0=B0=D1=80=D1=82=D0=B0 2012=  =D0=B3. 10:51:00 UTC+2 =D0=BF=D0=BE=D0=BB=D1=8C=D0=B7=D0=BE=D0=B2=D0= =B0=D1=82=D0=B5=D0=BB=D1=8C HamsterofDeath =D0=BD=D0=B0=D0=BF=D0=B8=D1=81= =D0=B0=D0=BB:
this is what i can do:=

tupleList.collect({case (a,b,c) =3D> ...}

this is what i want t= o do:

tupleList.forEach((a,b,c)) =3D> ....

is there a reason= for this restriction, is it planned, is it not, can i
do it later with = macros?

------=_Part_78_2817895.1332420968946-- ------=_Part_77_11308581.1332420968946--