New new

History Key

  • New content
  • Removed content

Recent Versions

Choose two versions to compare, or click the link to view it.

  1. 17. about 1 year by stuart.halloway
  2. 16. over 2 years by richhickey
  3. 15. over 2 years by richhickey
  4. 14. almost 3 years by richhickey
  5. 13. almost 3 years by richhickey
  6. 12. almost 3 years by richhickey
  7. 11. almost 3 years by richhickey
  8. 10. almost 3 years by richhickey
  9. 9. almost 3 years by richhickey
  10. 8. almost 3 years by richhickey
  11. 7. almost 3 years by richhickey
  12. 6. almost 3 years by richhickey
  13. 5. almost 3 years by richhickey
  14. 4. almost 3 years by richhickey
  15. 3. almost 3 years by richhickey
  16. 2. almost 3 years by richhickey
  17. 1. almost 3 years by richhickey
 

Read this first!

Clojure issue tracking now lives at http://dev.clojure.org/jira, and the wiki is at http://dev.clojure.org. These Assembla pages are kept online for historical interest only.

Note - the work below has been subsumed by reify et al

Docs

(new classname ...) does what it always did

(new [a vector] ...) generates a new instance of an anonymous class, implementing any supplied super class/interfaces

 (new [super? interfaces*] this-name?
  {:volatile [closed-over-locals+] :other-flags tbd}?
  (method [args*] body)* )

Usage:

 

  • If a non-interface superclass is supplied it must be first and have a no-arg constructor
  • this-name and flag map are optional
  • return type is type hint on method name
  • arg types are type hint on arg names
  • leave out all hints: will try to match on same name/arity method in superclasses
  • if overloaded with same arity in superclass you must specify full hints to disambiguate
    • missing hint implies Object
  • you can add new (e.g. helper) methods not in any super, must have different name (can't add overloads to supers). No way to call those from outside the methods other than via reflection
    • i.e. they are really for helpers
      • i.e. I may make them private
  • Can declare any closed-over local to be volatile. Will be mutable with set! in the body of methods only. If closed over captures current value, not mutable cell. 
    • Don't use this unless you know what you are doing, no concurrency support, no sympathy etc Tongue out
  • enclosing fn acts as factory, no ctors
  • use captured locals in lieu of fields
    • can be primitive
  • methods can take and return true primitives
  • recur works to method heads

Can be extremely lightweight:

(str (new [] (toString [] "hello")))
"hello"

 


Ideas/scratchpad

  • A new code generator
  • replaces fn, proxy
    • method body bytecode generating capabilities of fn
      • without being locked into generating only AFn/RestFn
    • dynamic class generating capabilities of proxy
      • without the indirection overhead
      • also missing the dynamic proxy-modifying capabilities
        • are they used?
      • should be able to overcome the lack of access to super/protected
  • looks like proxy
    • plus addition for naming instance for self-reference
    • (new [bases] [super-ctor-args]
        :as this
        :volatile [some captured names] 
        :other-flags tbd
        (method1 [] ...)
        (method2 [] ...))
    • or obj
    • (obj this [bases] [super-ctor-args]
        :volatile [some captured names] 
        :other-flags tbd
        (method1 [] ...)
        (method2 [] ...))
    • Can we distinguish from (new Classname ...)?
      • yes, always starts with vector, use :as to name this
    • Other names?
      • obj, instance
  • fn implemented in terms of new:
    • (fn foo [x y] ...) =>  (new [AFn] [] :as foo (invoke [x y] ...))
  • unlike proxy, must have same-arity overload resolution in declarations
  • like fn, can create closures
    • closed-over locals act as fields
    • expose 'fields'?
      • no way to access
    • as member fields, or as map keys?
      • latter could clash with map impls
      • implement struct on new
    • enclosing fn acts as factory, no ctors
    • no other members, define interfaces first
      • or could be private members for self-use only?
  • reflection is weak for compile-time type introspection
    • currently working ok for proxy/genclass
    • but class/jar file based solutions require compilation configuration of jars
    • needs to be extended in any case to enumerate protected super members for use in new
  • some logic of fn parsing moves to fn macro
    • arity processing
  • is :once logic still needed?
    • relationship to structs?
    • mutable fields?
      • means mutable locals
        • not necessarily, could be 'feature' of newnew
      • prevent closing-over?
        • no
        • or inherit isolated 'hole'? - no
        • just capture current value
      • but newnew is fundamentally closing over
        • would require something else for fields
        • only accessible from methods
        • so, close over, then use declaration in new to indicate volatile
          • creates local hole only, if nested closed over just captures current value
      • always volatile?
        • yes
    • Optional name for class?
      • won't name concrete class but an inserted abstract super
      • changing hierarchy will require restart
      • runtime accessibility of name still suspect
        • classloaders, load order
      • Why?
      • 'Type' slot a la meta?
        • better fit for dynamic use
        • can reuse 'type' tag across redefs (including hierarchy changes) in continuously running system
      • What is replacement for abstract bases?
        • Used often in clojure.lang
        • might be able to use prototype created with newnew
          • but would probably need constructors then
        • or macro-like code templates
        • Serialization issues
          • with no class names, no persistent serializability
          • so perhaps newnew + factory fn not suitable for Clojure's data structures?