- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
parameters without names
Wed, 2011-12-14, 13:05
what's the closest thing scala offers to achieve something like this:
def product(Int,Double) = {
"the product is "+$1*$2
}
if it's a function, i can use "_" in cases where every parameter is only used once and in the same order as they come in, but that's a special case. can i avoid names in a more general case as well? i'd like to skip them in cases where they don't add information
Wed, 2011-12-14, 13:51
#2
Re: parameters without names
keystrokes don't matter here, it's just that i don't want to write down anything that is not needed.
if i have a method min(a,b,c,d,e......z), the parameter names don't serve any purpose. their meaning becomes clear just by reading the method name.
i came up with a third option:
def x(p:(String, Label, JPanel, TypeWhatever)) = {
p._1, p._2 etc.
}
but that is cruelty to the caller.
there's nothing wrong with a and b as parameter names, they are as good as $n
-------- Original-Nachricht --------
> Datum: Wed, 14 Dec 2011 13:17:41 +0100
> Von: Andreas Flierl
> An: Dennis Haupt
> CC: scala-user@googlegroups.com
> Betreff: Re: [scala-user] parameters without names
> Hi,
>
> you're probably already aware of what is below, but anyway...
>
> Dennis Haupt asked:
>
> > what's the closest thing scala offers to achieve something like this:
> >
> > def product(Int,Double) = {
> > "the product is "+$1*$2
> > }
>
> probably
> val f: (Int, Double) => String = "the product is " + _ * _
>
> or
> def product($1: Int, $2: Double) = "the product is " + $1 * $2
> ;)
>
> > if it's a function, i can use "_" in cases where every parameter is only
> used once and in the same order as they come in, but that's a special
> case. can i avoid names in a more general case as well? i'd like to skip them
> in cases where they don't add information
>
>
> Whats worse about using "a" and "b" instead of some special notation like
> $parameterIndex?
> Why skip the names in the parameter list? To save a couple of keystrokes?
>
> Kind regards
> Andreas
Thu, 2011-12-15, 01:21
#3
Re: parameters without names
It would be great if this was possible!
That is (IIUC):
1) the names $1,$2,...,$n were implicitly defined in all methods as aliases for actual parameters,
$0 could refer to all parameters together (e.g. to pass them all to another method),
2) naming of method parameters was optional.
In the meantime, how about this:
val product = "the product is " + (_:Int)*(_:Double)
Tue, 2011-12-20, 22:31
#4
Re: parameters without names
On 2011-12-14 13:46, Dennis Haupt wrote:
> i came up with a third option:
>
> def x(p:(String, Label, JPanel, TypeWhatever)) = {
> p._1, p._2 etc.
> }
>
> but that is cruelty to the caller.
It is not!
(HamsterofDeath pointed this out http://groups.google.com/group/scala-user/msg/b969d4a6173f39dd)
scala> def x(p:(String, Int, List[Double])) = p.productIterator foreach println
x: (p: (String, Int, List[Double]))Unit
scala> x("w",3,List(2.1,1.5))
w
3
List(2.1, 1.5)
Tue, 2011-12-20, 22:41
#5
Re: parameters without names
Am 20.12.2011 22:24, schrieb Eugen Labun:
> On 2011-12-14 13:46, Dennis Haupt wrote:
>> i came up with a third option:
>>
>> def x(p:(String, Label, JPanel, TypeWhatever)) = {
>> p._1, p._2 etc.
>> }
>>
>> but that is cruelty to the caller.
>
> It is not!
> (HamsterofDeath pointed this out
> http://groups.google.com/group/scala-user/msg/b969d4a6173f39dd)
>
>
> scala> def x(p:(String, Int, List[Double])) = p.productIterator
> foreach println
> x: (p: (String, Int, List[Double]))Unit
>
> scala> x("w",3,List(2.1,1.5))
> w
> 3
> List(2.1, 1.5)
>
>
>
>
i AM hamsterofdeath/dennis ;) i just didn't know about the parameter
list -> tuple feature at that point in time.
when at home, i use thunderbird which tells everyone my nick. at work i
use gmx (which uses my actual name) directly. i'll have to change that
someday
Tue, 2011-12-20, 23:01
#6
Re: parameters without names
> i AM hamsterofdeath/dennis ;) i just didn't know about the parameter
> list -> tuple feature at that point in time.
>
> when at home, i use thunderbird which tells everyone my nick. at work i
> use gmx (which uses my actual name) directly. i'll have to change that
> someday
Oh, sorry, didn't noticed that.
Anyway, thank you for the tip :)
Hi,
you're probably already aware of what is below, but anyway...
Dennis Haupt asked:
> what's the closest thing scala offers to achieve something like this:
>
> def product(Int,Double) = {
> "the product is "+$1*$2
> }
probably
val f: (Int, Double) => String = "the product is " + _ * _
or
def product($1: Int, $2: Double) = "the product is " + $1 * $2
;)
> if it's a function, i can use "_" in cases where every parameter is only used once and in the same order as they come in, but that's a special case. can i avoid names in a more general case as well? i'd like to skip them in cases where they don't add information
Whats worse about using "a" and "b" instead of some special notation like $parameterIndex?
Why skip the names in the parameter list? To save a couple of keystrokes?
Kind regards
Andreas