Skip to content

Instantly share code, notes, and snippets.

@nasser
Created April 10, 2020 21:14
Show Gist options
  • Save nasser/2d0031a47ac4dabc0a66960ec08d312c to your computer and use it in GitHub Desktop.
Save nasser/2d0031a47ac4dabc0a66960ec08d312c to your computer and use it in GitHub Desktop.
Clojure proxy
using clojure.lang;
using Magic;
using System;
using System.Reflection;
[assembly: AssemblyVersion("0.0.0.0")]
public class proxy22351 : Random, Counted, IDeref, IProxy {
private readonly long y;
// closures and base type constructor arguments
// seed here is x in the clojure code, num is y
public proxy22351(int seed, long num) : base(seed) {
this.y = num;
}
// (proxy-super NextDouble) statically calls the base class method
// good type information when invoking count method on the proxy type
public object deref() {
return base.NextDouble() * (double)((Counted)this).count();
}
// method binding with proxy type works
// more good type information with closures and proxy-super
public int count() {
Console.WriteLine(this);
return (int)(this.y * (long)base.Next());
}
// update-proxy api not supported and will throw exceptions if used
public void __initClojureFnMappings(IPersistentMap persistentMap) {
throw new NotSupportedException();
}
public void __updateClojureFnMappings(IPersistentMap persistentMap) {
throw new NotSupportedException();
}
public IPersistentMap __getClojureFnMappings() {
throw new NotSupportedException();
}
// mutual recursion of methods possible
public override int Next() {
return (int)this.NextDouble();
}
public override double NextDouble() {
return (double)this.Next();
}
}
public class user$fn$22353 : AFunction, Function<object, int>, Function<object, object>
{
public override bool HasArity(int num) {
return num == 1;
}
// closures and base class constructor arguments passed in as
// constructur arguments to proxy type. good type information
// when interoping with proxy outside its method bodies
public override object invoke(int x) {
long num = 70L;
proxy22351 proxy = new proxy22351(x, num);
Console.WriteLine(proxy);
Console.WriteLine(proxy.NextDouble());
return null;
}
public override object invoke(object x) {
return this.invoke(RT.uncheckedIntCast(x));
}
}
(fn [^int x]
(let [y 70
p (proxy [Random clojure.lang.Counted clojure.lang.IDeref] [x]
(deref [] (* (proxy-super NextDouble) (.count this)))
(count []
(Console/WriteLine this)
(* y (proxy-super Next)))
(Next [] (.NextDouble this))
(NextDouble [] (.Next this)))]
(Console/WriteLine p)
(Console/WriteLine (.NextDouble p))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment