main_templates3/re.scala
changeset 477 a4e1f63157d8
parent 428 cdfa6a293453
--- a/main_templates3/re.scala	Sat Nov 04 18:53:37 2023 +0000
+++ b/main_templates3/re.scala	Mon Nov 06 14:18:26 2023 +0000
@@ -17,39 +17,34 @@
 def ALT(r1: Rexp, r2: Rexp) = ALTs(List(r1, r2))
 def SEQ(r1: Rexp, r2: Rexp) = SEQs(List(r1, r2))
 
-
 // some convenience for typing regular expressions
-import scala.language.implicitConversions    
-import scala.language.reflectiveCalls 
 
 def charlist2rexp(s: List[Char]): Rexp = s match {
   case Nil => ONE
   case c::Nil => CHAR(c)
   case c::s => SEQ(CHAR(c), charlist2rexp(s))
 }
-implicit def string2rexp(s: String): Rexp = charlist2rexp(s.toList)
+
+import scala.language.implicitConversions
 
-implicit def RexpOps (r: Rexp) = new {
+given Conversion[String, Rexp] = (s => charlist2rexp(s.toList))
+
+extension (r: Rexp) {
   def | (s: Rexp) = ALT(r, s)
   def % = STAR(r)
   def ~ (s: Rexp) = SEQ(r, s)
 }
 
-implicit def stringOps (s: String) = new {
-  def | (r: Rexp) = ALT(s, r)
-  def | (r: String) = ALT(s, r)
-  def % = STAR(s)
-  def ~ (r: Rexp) = SEQ(s, r)
-  def ~ (r: String) = SEQ(s, r)
-}
+// some examples for the conversion and extension:
 
-// examples for the implicits:
-// ALT(CHAR('a'), CHAR('b'))
 // val areg : Rexp = "a" | "b"
-
-// SEQ(CHAR('a'), CHAR('b')) 
+//  => ALTs(List(CHAR('a'), CHAR('b')))
+//
 // val sreg : Rexp = "a" ~ "b"
-
+//  => SEQs(List(CHAR('a'), CHAR('b'))) 
+//
+// val star_reg : Rexp = ("a" ~ "b").%
+//  => STAR(SEQs(List(CHAR('a'), CHAR('b')))) 
 
 // ADD YOUR CODE BELOW
 //======================