scala - scalatest "A stack" should "do something" -- wtf? How is should a method of string? -


i'm starting out scala here , i'm finding of syntax confusing. example scalatest main page

  class examplespec extends flatspec matchers {      "a stack" should "pop values in last-in-first-out order" in {...}  } 

as read means "should" method of string "a stack"? if right, how happen? doesn't seem work scala prompt

 scala> class examplespec {  |    "a stack" should "pop values"  | }  <console>:9: error: value should not member of string       "a stack" should "pop values" 

if "should" not method of string "a stack" in above snippet how read snippet correctly? "should" , how relate string? clues?

this commonly known pimp library enrich library pattern can extend other libraries (in case scala strings) our own methods using implicit conversions.

the way works flatspec mixes in trait called shouldverb has following implicit conversion defined:

 implicit def converttostringshouldwrapper(o: string): stringshouldwrapperforverb =    new stringshouldwrapperforverb {      val leftsidestring = o.trim    } 

and stringshouldwrapperforverb has should method defined:

def should(right: string) .... 

the reason not work in repl you don't have flatspec , via shouldverb trait mixed in.

you can read more in documentation under implicit classes.


Comments

Popular posts from this blog

javascript - oscilloscope of speaker input stops rendering after a few seconds -

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -