- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
5 times println
Sat, 2009-08-29, 03:47
Hi,
I noticed a times method was added to RichInt today:
http://lampsvn.epfl.ch/trac/scala/changeset/18599
To achieve:
3 times { println("hi ") }
This is pretty syntax, but it would unfortunately clash with some DSL
sugar I was intending to put in ScalaTest 1.0. It would also clash
with a different DSL sugar that Eric Torreborre put in specs 1.4.4. We
could make changes, of course, but one other option is to stick a Do
at the end of times in RichInt:
3 timesDo { print("hi ") }
Uglier I think, but it does leave "times" available to be added to
Ints in DSLs created by other "non-standard" libraries. Or maybe
there's a better method name people can think of, or a different
approach.
Bill
----
Bill Venners
Artima, Inc.
http://www.artima.com
Sat, 2009-08-29, 04:37
#2
Re: 5 times println
Hi Paul,
I don't like timesDo either. "times" is used in Ruby and it is pretty,
although it does look a bit like multiplication. Here's another idea:
3 timesRepeat { println("hi ") }
Also not as pretty as times and even more verbose.
3 iterationsOf { println("hi ") }
3 iterations { println("hi ") }
That last one might work. In fact that might be clearer than times.
I have also never had luck turing off an implicit from PreDef, but I
only tried once for curiosity. I think it is a bad idea to try and
design something that would require hiding an implicit in Predef
anyway.
Bill
On Fri, Aug 28, 2009 at 8:08 PM, Paul Phillips wrote:
>
> On Fri, Aug 28, 2009 at 07:47:53PM -0700, Bill Venners wrote:
>> 3 timesDo { print("hi ") }
>
> I will go with any consensus, but I'm not too thrilled about timesDo.
> Some words I'd prefer are ditto and (thank you m-w.com) ingeminate,
> which let's face it would be hard to forget. To the extent that there
> is a preexisting expectation about what such a method would be called,
> "times" is the only name I've heard with any traction.
>
> Of significantly greater interest to me than the choice of name is the
> reality that it poses a conflict for you. It is too difficult to
> control the scoping of implicits and mask them when they're in the way.
> I've never had any luck warding off implicits from Predef, but even if
> that did work, you'd have to turn off the entire RichInt conversion to
> avoid the one ambiguity-creating method.
>
> --
> Paul Phillips | If this is raisin, make toast with it.
> Vivid |
> Empiricist |
> pp: i haul pills |----------* http://www.improving.org/paulp/ *----------
>
Sat, 2009-08-29, 17:07
#3
Re: 5 times println
Bill Venners wrote:
> Uglier I think, but it does leave "times" available to be added to
> Ints in DSLs created by other "non-standard" libraries. Or maybe
> there's a better method name people can think of, or a different
> approach.
I think that changing names in standard library to conform some
"non-standard" libraries as general practice is very bad idea. Standard
library should be as best as it could. "non-standard" means - extension
to standard. They should be extended to add values to standard. But the
standard should not care about non-standards. Standard lib suppose to
have much longer lifetime then any non standard and should not be
designed in awareness of something that some developers would not ever use.
Sat, 2009-08-29, 17:27
#4
Re: 5 times println
On Sat, Aug 29, 2009 at 5:06 PM, Vladimir
Kirichenko wrote:
> I think that changing names in standard library to conform some
> "non-standard" libraries as general practice is very bad idea.
Agreed, but the flip side of that is that additions to the standard
library which ... I'm trying to avoid saying "pollute" because I don't
want the pejorative connotations ... the global namespace need to
really pay their way.
Cheers,
Miles
On Fri, Aug 28, 2009 at 07:47:53PM -0700, Bill Venners wrote:
> 3 timesDo { print("hi ") }
I will go with any consensus, but I'm not too thrilled about timesDo.
Some words I'd prefer are ditto and (thank you m-w.com) ingeminate,
which let's face it would be hard to forget. To the extent that there
is a preexisting expectation about what such a method would be called,
"times" is the only name I've heard with any traction.
Of significantly greater interest to me than the choice of name is the
reality that it poses a conflict for you. It is too difficult to
control the scoping of implicits and mask them when they're in the way.
I've never had any luck warding off implicits from Predef, but even if
that did work, you'd have to turn off the entire RichInt conversion to
avoid the one ambiguity-creating method.