Fwd: Is under still upper when it comes to score?

144 views
Skip to first unread message

Som Snytt

unread,
Jul 27, 2013, 9:19:26 AM7/27/13
to scala-internals

Probably this is the same bucket of fish.

Maybe this behavior of underscore could find mention in the motivational section of the style guide, where it would say, "prefer myMsg".

scala> Character isUnicodeIdentifierStart '_'
res0: Boolean = false

scala> val _msg = "hi"
_msg: String = hi

scala> s"$_msg"
<console>:1: error: invalid string interpolation: `$$', `$'ident or `$'BlockExpr expected
       s"$_msg"
         ^
<console>:1: error: unclosed string literal
       s"$_msg"
               ^

scala> s"${_msg}"
res1: String = hi



---------- Forwarded message ----------
From: Som Snytt <som....@gmail.com>
Date: Fri, Jul 26, 2013 at 7:17 AM
Subject: Is under still upper when it comes to score?
To: scala-user <scala...@googlegroups.com>


Thinking about Travis Brown's issue with backticked underscore

https://issues.scala-lang.org/browse/SI-6426

I hope that resolves as import something.{ `_` => ubar } at least.

But is it a problem that underscore doesn't count as upper case in patterns?

The spec as such says both $ and _ count as upper. Maybe that changed.

Maybe somebody knows off the cuff.  Like maybe somebody from _.underscore consulting, who presumably are specialists in that token.

I guess that would entail that case _ is also not a special case of a variable pattern, but of a stable id.


scala> val $foo = 4
$foo: Int = 4

scala> 4 match { case $foo => "yes" case _ => "no" }
res0: String = yes

scala> val _foo = 4
_foo: Int = 4

scala> 4 match { case _foo => "yes" case _ => "no" }
<console>:8: warning: patterns after a variable pattern cannot match (SLS 8.1.1)
              4 match { case _foo => "yes" case _ => "no" }
                             ^
<console>:8: warning: unreachable code due to variable pattern '_foo' on line 8
              4 match { case _foo => "yes" case _ => "no" }
                                                     ^
res1: String = yes

scala> 3 match { case _foo => "yes" case _ => "no" }
<console>:8: warning: patterns after a variable pattern cannot match (SLS 8.1.1)
              3 match { case _foo => "yes" case _ => "no" }
                             ^
<console>:8: warning: unreachable code due to variable pattern '_foo' on line 8
              3 match { case _foo => "yes" case _ => "no" }
                                                     ^
res2: String = yes

scala> val Foo = 4
Foo: Int = 4

scala> 3 match { case Foo => "yes" case _ => "no" }
res3: String = no

scala> 3 match { case $foo => "yes" case _ => "no" }
res4: String = no


Eugene Burmako

unread,
Jul 27, 2013, 9:24:06 AM7/27/13
to <scala-internals@googlegroups.com>
Would also be great to be able to use unadorned underscore in interpolated pattern matching, as in val q"$foo(..$_)". Currently we have to write q"$foo(..${_})".


--
You received this message because you are subscribed to the Google Groups "scala-internals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-interna...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Som Snytt

unread,
Jul 27, 2013, 10:26:31 AM7/27/13
to scala-internals
On the apply side, I was thinking

Seq("milk", "eggs") map s"got $_?"

Som Snytt

unread,
Aug 2, 2013, 1:42:37 AM8/2/13
to scala-internals
I haven't tried the pattern matching side yet, but at the moment, I am disliking the extra parens required:

scala> val vs = 1 to 10
vs: scala.collection.immutable.Range.Inclusive = Range(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)

scala> val xs = vs zip vs
xs: scala.collection.immutable.IndexedSeq[(Int, Int)] = Vector((1,1), (2,2), (3,3), (4,4), (5,5), (6,6), (7,7), (8,8), (9,9), (10,10))

scala> vs map (s"How many? $_")
res9: scala.collection.immutable.IndexedSeq[String] = Vector(How many? 1, How many? 2, How many? 3, How many? 4, How many? 5, How many? 6, How many? 7, How many? 8, How many? 9, How many? 10)

scala> xs map (s"How many? $_ by $_").tupled
res10: scala.collection.immutable.IndexedSeq[String] = Vector(How many? 1 by 1, How many? 2 by 2, How many? 3 by 3, How many? 4 by 4, How many? 5 by 5, How many? 6 by 6, How many? 7 by 7, How many? 8 by 8, How many? 9 by 9, How many? 10 by 10)



By contrast:

scala> vs map s"How many? $_"
res15: Any => scala.collection.immutable.IndexedSeq[Char] = <function1>

scala> xs map s"How many? $_ by $_".tupled
<console>:10: error: value tupled is not a member of String
              xs map s"How many? $_ by $_".tupled
                                           ^

and while this is great

scala> val foo = "hi"
foo: String = hi

scala> s"($foo)+".r
res13: scala.util.matching.Regex = (hi)+

this is less so

scala> s"How many? $_ by $_ by $_".r
res14: (Any, Any, Any) => scala.util.matching.Regex = <function3>


That is, I'd prefer s"$_".tupled worked and s"$_".r did not.

Similarly,

scala> s"How many? $_ by $_ by $_"("one","two","three")
<console>:8: error: too many arguments for method apply: (index: Int)Char in class StringOps
              s"How many? $_ by $_ by $_"("one","two","three")
                                         ^

scala> (s"How many? $_ by $_ by $_")("one","two","three")
res20: String = How many? one by two by three




Som Snytt

unread,
Aug 2, 2013, 1:54:26 AM8/2/13
to scala-internals
Actually, I want it to just work, where "it" is whatever I typed.

scala> def f(i: Int, s: String) = {
     | val r = s"foo $_".r
     | val m = r(i)
     | s match { case m() => true case _ => false }
     | }
f: (i: Int, s: String)Boolean

scala> f(3, "foo 3")
res2: Boolean = true

scala> f(3, "foo 4")
res3: Boolean = false


Som Snytt

unread,
Aug 2, 2013, 4:13:59 AM8/2/13
to scala-internals
quasiquotes is a great name for a great feature. But I'm just learning.

Does this mean it does what you wanted:

scala> val q"$a + $b" = q"1 + 2"
a: reflect.runtime.universe.Tree = 1
b: reflect.runtime.universe.Tree = 2

scala> val q"$_ + $b" = q"1 + 2"
b: reflect.runtime.universe.Tree = 2




On Sat, Jul 27, 2013 at 6:24 AM, Eugene Burmako <eugene....@epfl.ch> wrote:

Eugene Burmako

unread,
Aug 2, 2013, 4:17:54 AM8/2/13
to scala-i...@googlegroups.com
Yes, if that'd word, it'd be marvelous. Currently it fails with:


scala> val q"$_ + $b" = q"1 + 2"
<console>:1: error: invalid string interpolation: `$$', `$'ident or `$'BlockExpr expected
      val q"$_ + $b" = q"1 + 2"
            ^
<console>:1: error: unclosed string literal
      val q"$_ + $b" = q"1 + 2"
                              ^
Reply all
Reply to author
Forward
0 new messages