Backquote Used in Scala Swing Event -
i new scala , following 1 of examples swing working in scala , hava question. based on,
listento(celsius, fahrenheit) reactions += { case editdone(`fahrenheit`) => val f = integer.parseint(fahrenheit.text) celsius.text = ((f - 32) * 5 / 9).tostring case editdone(`celsius`) => val c = integer.parseint(celsius.text) fahrenheit.text = ((c * 9) / 5 + 32).tostring } why have use backquote (`) in editdone(`fahrenheit`) , editdone(`celsius`) identify textfield components e.g. fahrenheit , celsius? why can't use editdone(fahrenheit) instead?
thanks
this has pattern matching. if use lower-case name within pattern match:
reactions += { case editdone(fahrenheit) => // ... } then object being matched (the event in case) matched against editdone event on widget. bind reference widget name fahrenheit. fahrenheit becomes new value in scope of case.
however, if use backticks:
val fahrenheit = new textfield ... reactions += { case editdone(`fahrenheit`) => // ... } then pattern match succeed if editdone event refers existing object referenced value fahrenheit, defined previously.
note if name of value fahrenheit uppercase, fahrenheit, wouldn't have use backticks - if you've put them. useful if have constants or objects in scope want match against - these have uppercase names.
Comments
Post a Comment