{"id":4380,"date":"2016-06-08T02:29:34","date_gmt":"2016-06-08T02:29:34","guid":{"rendered":"http:\/\/www.garysieling.com\/blog\/?p=4380"},"modified":"2016-06-08T02:29:34","modified_gmt":"2016-06-08T02:29:34","slug":"scala-multi-line-strings","status":"publish","type":"post","link":"https:\/\/www.garysieling.com\/blog\/scala-multi-line-strings\/","title":{"rendered":"Scala &#8211; multi-line strings"},"content":{"rendered":"<p>Scala has a really awesome feature, that allows you to use multi-line strings in the language. I find this particularly helpful for SQL, or inline documentation. <\/p>\n<pre lang=\"scala\">\nval str = \"\"\"\n   test\n\"\"\"\n<\/pre>\n<p>If you want to deal with tabbing issues, you can put a pipe at the beginning of each line. When you do this, the string out of this is stripped of the extraneous whitespace at the beginning of each line.<\/p>\n<pre lang=\"scala\">\nval str = \"\"\"\n  |abc\n  |def\n  |ghi\n\"\"\"\n<\/pre>\n<p>If for some reason you dislike using a pipe for this, you can pick another character and than call stripMargin on the output, to apply the same change that the pipe would have done:<\/p>\n<pre lang=\"scala\">\nval str = \"\"\"\n   #test\n\"\"\".stripMargin('#')\n<\/pre>\n<p>This returns the following result:<\/p>\n<pre>\n\"\ntest\n\"\n<\/pre>\n<p>This is really compelling if you combine it with string interpolation, as this allows you to pull in any constants you have, or do computations within the string, for instance:<\/p>\n<pre lang=\"scala\">\nval word = \"test\"\nval str = s\"\"\"\n  ${word}\n\"\"\"\n<\/pre>\n<p>And this will swap in the external variable:<\/p>\n<pre>\n\"\n  test\n\"\n<\/pre>\n<p>Like I said in the beginning &#8211; this is really useful for SQL. You might be asking how you escape values, as this is clearly necessary. While not well documented, you can put whatever you want in the ${} block, which allows you to call external functions, do computations, etc, like so:<\/p>\n<pre lang=\"scala\">\nval str = s\"\"\"\n  ${word.length()}\n\"\"\"\n<\/pre>\n<p>In this case it returns a value like this:<\/p>\n<pre>\n\"\n  4\n\"\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Some examples that show how multiline strings work in scala<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[4],"tags":[480],"aioseo_notices":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/4380"}],"collection":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/comments?post=4380"}],"version-history":[{"count":0,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/4380\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/media?parent=4380"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/categories?post=4380"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/tags?post=4380"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}