lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]




On 17/08/15 03:57 PM, Mason Bogue wrote:
The proposal is this:

str1 / str2 -- equivalent to str1:match(str2)

This is a simple quality-of-life change that encourages the use of
pattern-matching over functions like string.find, sub, etc. The
resulting code is slower, but potentially easier to write, since
memorizing the string library is less important. Compare:

trailing_whitespace = #(s / ".*(%s*)$")

to

start, end = s:find("%s*$")
trailing_whitespace = end - start

Of course, some people will prefer the latter code, due to the
disastrous effects of "line noise". But in many cases people are able
to write perfectly readable code while using patterns, which are
otherwise an underappreciated feature of Lua. The confusion with
integer division seems like an unlikely problem since patterns used in
this way are "almost always" (a dangerous assertion) string literals.
It does however make sense to me that only *one* operator be used to
match strings in this way; having operators for find, gmatch, etc
would quickly become confusing.

The other architectural issue is that now the string matching
functionality is always loaded into a Lua state, instead of some cases
where calling `string = require "string"` may have been necessary. I
don't know if this is considered good or bad.

Printing a list of character classes whenever a string contains an
invalid class, or as part of `lua --help` or `man lua` might also help
people "learn as they go". I know I always forget half of them :p (Pop
quiz: what does %S match?)

start, end = s / "()%s*()$"
trailing_whitespace = end - start

--
Disclaimer: these emails are public and can be accessed from <TODO: get a non-DHCP IP and put it here>. If you do not agree with this, DO NOT REPLY.