bike icon indicating copy to clipboard operation
bike copied to clipboard

Doc request: instancing, drill down to use a method, and working with a float or string array?

Open PersonalityEngineer opened this issue 3 years ago • 4 comments

I was hoping someone could give me a simple example to get me started?

I'd like to access an object by instancing it and activate some of its methods, e.g:

SomeObject ob = new SomeObject(); ob.SubObject.SubObject2.Method("data");

I've been able to instantiate via (defvar x (new ...)) and even (invoke ob Method arg), but I can't seem to drill down through other objects.

Also an example on using a .Net float or string array (unboxing?) would be fantastic.

Any help would be greatly appreciated, thanks so much for writing this library! I've been hoping to access some .Net-only libraries for a very long time.

PersonalityEngineer avatar Jul 09 '22 12:07 PersonalityEngineer

I think you can do it with a combination of bike:property and bike:invoke.

Here's what I did:

using System;

namespace MyNamespace
{
	public class InnerClass
	{
		public string GetInfo()
		{
			return "This is from the inner class!";
		}
	}

	public class OuterClass
	{
		public OuterClass ()
		{
			this.Inner = new InnerClass();
		}

		public InnerClass Inner {get; set;}

		public string GetInfo()
		{
			return "This is from the outer class!";
		}

		public int GetNumber()
		{
			return 1234;
		}
	}
}

Then in Lisp, I did this:

;; in SLIME
BIKE-EXAMPLES> (let ((the-outer-ojbect (new 'OuterClass)))
      (invoke (property the-outer-ojbect 'Inner) 'GetInfo))
=> "This is from the inner class!"

johnhilts avatar Jul 13 '22 01:07 johnhilts

@johnhilts Yep, that's the way to do it.

@PersonalityEngineer Also, consider using included custom syntax.

(named-readtables:in-readtable bike:bike-syntax)

;; change %SubObject to $SubObject in case if that is a field not a property
(let ((ob (new 'SomeObject)))
  [[[ob %SubObject] %SubObject2] Method "data"])

@PersonalityEngineer

Also an example on using a .Net float or string array (unboxing?) would be fantastic.

bike provides a generic way to access any array with dnaref and (setf dnaref) functions, e.g.

(let ((v (new '(array :float) 123)))
  (setf (dnaref v 0) 123.0s0)
  (dnaref v 0))

However, bike also allows to use pinned objects, so that you can access their memory directly (for ex. using cffi)

(let ((vector (new '(array :int) 10)))
    (with-fixed (ptr vector)
      (dotimes (i 10)
        (setf (cffi:mem-aref ptr :int i) (1+ i))))

Lovesan avatar Jul 20 '22 14:07 Lovesan

Didn't know about dnaref - very handy :)

(dnaref (invoke (invoke 'Enumerable 'Range 1 10) 'ToArray) 1)

=> 2

johnhilts avatar Jul 20 '22 20:07 johnhilts

Finally, here are the docs: https://github.com/Lovesan/bike/blob/master/doc/README.md

Lovesan avatar Mar 27 '24 23:03 Lovesan