guicho271828 / trivia

Pattern Matcher Compatible with Optima
Other
334 stars 22 forks source link

Use of otherwise pattern in CCL results in spurious unused lexical variable warning #51

Closed dfmorrison closed 8 years ago

dfmorrison commented 8 years ago
CL-USER> (values (machine-type) (machine-version) (software-type) (software-version)
                 (lisp-implementation-type) (lisp-implementation-version))
"x86_64"
"Intel(R) Core(TM) i7 CPU       M 620  @ 2.67GHz"
"Linux"
"4.4.0-31-generic"
"Clozure Common Lisp"
"Version 1.11-r16635  (LinuxX8664)"
CL-USER> (asdf:component-version (asdf:find-system :trivia nil))
"0.1"
CL-USER> (defun f (x)
           (trivia:match x
             ((list 1 z) z)
             (otherwise :nope)))
;Compiler warnings :
;   In F: Unused lexical variable OTHERWISE
F
CL-USER> ; demonstrate that it works right, it's just a spurious warning
; No value
CL-USER> (f '(1 2))
2
CL-USER> (f '(1 2 3))
:NOPE
CL-USER> ; this is Ubuntu 16.04
; No value
CL-USER> 
guicho271828 commented 8 years ago

ok, "otherwise" is a feature in optima, which is not implemented in trivia. The compatibility between optima and trivia is important, but I am reluctant to add the feature to trivia.

Would you still really want it? if there is an urgent need I am ok with adding it (but probably without the error checking on the non-last clauses). Thanks for the report anyways.

dfmorrison commented 8 years ago

Well, Trivia does advertise itself as follows:

Trivia is a pattern matching compiler that is compatible with Optima. It shares the same testing code with Optima and acts as a drop-in replacement.

A "drop-in replacement" says, at least to me, that if I have a bunch of code written against Optima that works, when I swap out Optima for Trivia it will still work the same. If that is not your goal, then I think you should probably change your blurb to something like "Trivia is similar to Optima, but in some cases has different semantics" [or, perhaps, you'd prefer to say "better semantics" :-)], and, ideally, document those differences.

As to the value of otherwise, having a default clause at the end is frequently useful, and a lone underscore character is rather inscrutable there, at least to my eyes. From Optima's documentation I've always assumed it is strictly analogous to underscore, though I've never tried it elsewhere. And I do agree with you that it would be better as just something magic for use in the last clause, a la case (though if that breaks compatibility, then probably not if you really want to be a drop-in replacement). As to complexity of implementation, I (as an end user) obviously think tidiness and usefulness of Trivia as a language trumps ease and tidiness of implementation, but your perspective may well be different.

Thanks!

guicho271828 commented 8 years ago

ok, i should be strict and honest to what i advertize; I will add the feature. there are some differences between trivia and optima, still. its a kind of "do your Am486 has the same bug as i486 has?" kind of thing, though.

guicho271828 commented 8 years ago

I newly added test cases for OTHERWISE. It explicitly allows the use of OTHERWISE in clauses other than the last one, which means it is completely analogous to _.

fare commented 8 years ago

One solution would be to have different packages: one that is fully compatible, the other that is simpler and purer.

guicho271828 commented 8 years ago

Note that in this topic I did not intend to defend about not implementing OTHERWISE. I just tried to claim it is of less priority and is not necessary (untested, undefined, unreliable feature may not be implemented --- when it worked in optima previously, it just happened to work by luck, since there is no mean to test it.) I finally defined the behavior and implemented it. This is now documented in https://github.com/guicho271828/trivia/wiki/Known-Differences .

@fare , the idea sounds good. Isn't it about assoc pattern issue though?