An OS file path or URL are basically strings. They impose some validity constrains and change some of behaviour (like conncatenation or comparison) but in fact are only strings.
I was always bothered by the way languages treat such special strings. You can not in a simple way express them as strings. You have to pull out a whole library to deal with them:
File.open(File.join('..', 'spec_helper'))
What I would like to see instead is something more simple, easier to remember and more object oriented. I wonder if we can put in a language a way to simply and quickly morph literals from basic type to more specific type. Something among the lines of:
(#path{.}+'..'+'spec_helper').open
There should be a way of saying: hey, I want this particular string literal to be treated as instance of a special type. Behind the scenes platform may use a call to Path.new but I'm not too interested in that. I want a way of expressing my intent of work with paths rather than strings.
Implementation of this pattern is quite simple. On parser side we need to declare a special way of marking literals as 'morphable' and providing target type. When parser encounters such literal it emits a call to BasicType.new_with_target(target) instead of simple BasicType.new. In addition BasicType must provide BasicType.register_target(target, TargetType) so library creators can register own morph targets.
This allows to easily incorporate regular expressions (#r{} or #regex{}), URLs (#u{} or #{url}). Sigils may be used to designate literal type. TargetType of course has full control over the valid format accepted within the literal.
0 comments:
Post a Comment