Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
pp:2025:scala:l06 [2025/04/02 20:15] cata_chiru created |
pp:2025:scala:l06 [2025/04/09 22:22] (current) cata_chiru |
||
---|---|---|---|
Line 2: | Line 2: | ||
===== 6.1 A small string DSL ===== | ===== 6.1 A small string DSL ===== | ||
+ | |||
+ | <note warning>For this laboratory, you will need to have a scala version >= 3.0.0, only starting from version 3.0.0 is the keyword "extension" available.</note> | ||
In this section we will extend the scala ''String'' class with several operators that implement useful string functions, effectively obtaining a small string-specialized [[https://en.wikipedia.org/wiki/Domain-specific_language|DSL]]. | In this section we will extend the scala ''String'' class with several operators that implement useful string functions, effectively obtaining a small string-specialized [[https://en.wikipedia.org/wiki/Domain-specific_language|DSL]]. | ||
Line 14: | Line 16: | ||
val res56: String = oders | val res56: String = oders | ||
</code> | </code> | ||
+ | |||
+ | <note> | ||
+ | Reminder from the DSL (Domain-Specific Language) Course: | ||
+ | |||
+ | <code scala> | ||
+ | extension (str: String) { | ||
+ | def >>(rhs: Int): String = ??? | ||
+ | } | ||
+ | </code> | ||
+ | </note> | ||
+ | |||
**6.1.2** Implement the ''<nowiki><<<</nowiki>'' and ''<nowiki>>>></nowiki>'' operators for a ''String'' and an ''Int'', that **rotate** the string in a direction by the given amount: | **6.1.2** Implement the ''<nowiki><<<</nowiki>'' and ''<nowiki>>>></nowiki>'' operators for a ''String'' and an ''Int'', that **rotate** the string in a direction by the given amount: |