TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Feature Macros, an Alternative to Feature Expressions in Clojure

80 pointsby woobyover 10 years ago

8 comments

sgroveover 10 years ago
I was just talking about the potential problems of FX last night - very interesting to see such an impressive and simple alternative approach. I'm not sure what the drawbacks are, but looking forward to seeing the discussion that grows from this.
评论 #8925193 未加载
brandonbloomover 10 years ago
Many of the most useful applications of feature expressions simply will not work as macros. You&#x27;d need to create one new feature-cond enabled macro for each other macro use case. Feature expressions, on the other hand, are a general solution that can be used at the call site without having to reinvent every macro that may ever need platform-conditional behavior, like your ns+ macro.<p>Consider the proposed +clj form in the body of an extend-protocol or a deftype:<p><pre><code> (defmacro +clj &quot;Form is evaluated only in the JVM.&quot; [form] (when (= :clj *host*) form)) (deftype Foo SomeCommonProtocol (aMethod [blah blah] ...) (+clj JvmOnlyInterface (someMethod [blah] ...) ) ) </code></pre> Of course, this <i>does not work</i>. The deftype macro gets the +clj form unmodified and must handle it on its own.<p>At best, you could have a syntax-quote style wrapper form:<p><pre><code> (feature-quote (deftype Foo SomeCommonProtocol (aMethod [blah blah] ...) (+clj JvmOnlyInterface (someMethod [blah] ...) ) )) </code></pre> However, this is essentially what Feature Expressions are anyway. Only broken, since macros don&#x27;t only operate on syntax&#x2F;edn, they can operate on any compile-phase value, like dates and times or the output of any tagged literal handler. Without being part of the phase before the macro expander, you can&#x27;t prevent platform specific types from reaching the macro body.<p>The reality is that Clojure, in the tradition of most lisps, has rigid read&#x2F;compile&#x2F;run phase separation where each phase has a varied evaluation strategy, but the phases run interleaved. Unlike say something like Mathematica, which does not run the reader interleaved with the evaluator - and the evaluator uses normal-order rather than applicative-order! Without such a uniform evaluation strategy (nevermind Mathematica&#x27;s evaluator&#x27;s other problems) it simply doesn&#x27;t make sense to fight so hard against beefing up the read phase.<p>Related: I&#x27;ve written before about how I don&#x27;t think that this impacts tooling as badly as you guys thing it does. I just don&#x27;t understand why you guys are so against feature expressions, nor do I think you&#x27;ve succeeded in proposing a desirable alternative.
评论 #8923835 未加载
评论 #8926408 未加载
smrtinsertover 10 years ago
I do like the idea of keeping them regular forms as it feels more future proof than cljx style expressions.
jballancover 10 years ago
It seems to me that Feature Macros will be trivially implementable as a library once Feature Expressions land:<p><pre><code> #+clj (def *host* :clj) #+cljs (def *host* :cljs)</code></pre>
评论 #8925338 未加载
ICWienerover 10 years ago
I am looking at one of the examples:<p><a href="https://github.com/feature-macros/clojurescript/blob/feature-macros/feature-macros-demo/src/demo/util.clj" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;feature-macros&#x2F;clojurescript&#x2F;blob&#x2F;feature...</a><p>But if I run Clojure on the JVM, the following fails:<p><pre><code> (def host nil) (defn foo [] (when (= :cljs host) (gstring&#x2F;format &quot;something&quot;))) </code></pre> ... the error being that there is no &quot;gstring&quot; namespace.<p>Since the proposed solution with macros would produce such code (I am right?), how does it solve the issue of namespaces that only exist in a specific platform?
评论 #8924054 未加载
mklappstuhlover 10 years ago
Does this also affect the #_ discard macro? And what about anonymous functions like `#(+ 5 %)`? Since those are reader macros I assume yes?
评论 #8923533 未加载
_halgariover 10 years ago
a clean and simple approach to the problem, +1
pandeiroover 10 years ago
tldr:<p><pre><code> git clone git:&#x2F;&#x2F;github.com&#x2F;feature-expressions&#x2F;clojurescript cd clojurescript&#x2F;feature-macros-demo make deps make demo</code></pre>