.NET Return Conversion to TypedValue
I was thinking about how to return values to LISP from .NET and played around with some code and came up with this.
It can be made more complete, but it is a place to start.
Code:
Public Function ObjectToTypedValue(ByRef objItem As Object)
Dim intDatatype As Integer = Nothing
If objItem Is Nothing Then
intDatatype = 5000
objItem = -1
Else
Select Case objItem.GetType
Case GetType(Double)
intDatatype = 5001
Case GetType(Point2d)
intDatatype = 5002
Case GetType(Int16)
intDatatype = 5003
Case GetType(String)
intDatatype = 5005
Case GetType(ObjectId)
intDatatype = 5006
Case GetType(Point3d)
intDatatype = 5009
Case GetType(Int32)
intDatatype = 5010
Case GetType(Boolean)
If objItem = False Then
intDatatype = 5019
objItem = -1
Else
intDatatype = 5021
objItem = -1
End If
End Select
End If
If Not intDatatype = Nothing Then
Return New TypedValue(intDatatype, objItem)
End If
Return Nothing
End Function
Thoughts?
Re: .NET Return Conversion to TypedValue
Quote:
Originally Posted by
peter
Thoughts?
This old post should help to simplify:
Quote:
Originally Posted by
BlackBox
From post #2 of this thread:
Re: Return value of a lisp callable function
Quote:
Originally Posted by Tony Tanzillo
A method marked with the LispFunction attribute should return a ResultBuffer only if you are returning a list.
If you are returning an atom, you just return that value directly.
For example, this method will return the symbol T:
Code:
[LispFunction("foo")]
public static object Foo( ResultBuffer args )
{
return true;
}
and this will return the integer 99:
Code:
[LispFunction("bar")]
public static object Bar( ResultBuffer args )
{
return 99;
}
and this will return the string "Hello":
Code:
[LispFunction("baz")]
public static object Baz( ResultBuffer args )
{
return "Hello";
}
IOW, the result of your lisp-callable function does not have to be a ResultBuffer, it can be that, or any value you can put into a TypedValue's Value field as well. The runtime looks at the result and converts it to the appropriate LISP result. In the case of System.Boolean, that would be the symbol T for True, or NIL for False.
You cannot return COM objects to VisualLisp using LispFunction, but you write a COM-callable wrapper that can return COM objects or any other ActiveX compatible type, which can be called from LISP using VLAX-* functions.
Re: .NET Return Conversion to TypedValue
The function still does what it is listed to do convert objects to typed values, but as you pointed out it is built in for objects to be converted to typed values when returning to lisp function. The post I made on the multiple file select dialog does return a result buffer for simplicity. It could return an atom with a minor change of code.
This function above can be used to build result buffers that can be returned as lists.
Re: .NET Return Conversion to TypedValue
Just as an aside, the GetType function isn't very optimal. You might want to rather try using GetTypeHandle instead as the compiler then circumvents getting the entire Type class through reflection. See this: http://blogs.msdn.com/b/vancem/archi...01/779503.aspx
Another alternative way would be to make a parameter overloaded set of functions. That way the compiler would choose the best suiting one for you, no need for if's and switch's. Not to mention, the functions would be very trivial (usually just the return line). Therefore chances are that the compiler might even inline them, negating the need for passing by reference and not even adding a function stack. Though in DotNet 4.5 you can now force agressive inlining: http://stackoverflow.com/questions/4...functions-in-c
Re: .NET Return Conversion to TypedValue
As an example, here's what I mean (just written it within the last 30 min, since it's mostly copy-paste with minor changes):
(Edited: See revised code in later post)
Note I've also added enumerables (i.e. arrays, linked lists, collections, etc.) and also tupples->dotted pairs, not to mention dictionaries to association lists (dotted pairs if value is an atom).
Edit: Also note I use the built-in constants from LispDataType instead of some "magic-number". The compiler should optimize the lookup and convert to int out of the actual code because they're constants, not variables. This makes the code more readable while not loosing efficiency.
Re: .NET Return Conversion to TypedValue
The same code in VB.Net:
Code:
Public NotInheritable
(Edited: See revised code in later post)
Edit: Just noticed something really strange ... copy-pasting from SharpDevelop adds the colour highlights for VB.Net code but not for C# :shock:
Re: .NET Return Conversion to TypedValue
Excellent...
I like the overload method, and it was along the same lines I was envisioning when I coded the function at the top.
(to include lists, dotted pairs, collections... I would like to see xml too...
I would hope that as this forum grows, and the simple initial functions are matured (with exception handling etc...) a standard LISP.Interop assembly could be put together that would expose all of these functions to anyone who wants to download them.
p=
Re: .NET Return Conversion to TypedValue
What is the trick you are using to add the color to the text in the vb.net example?
Re: .NET Return Conversion to TypedValue
Quote:
Originally Posted by
peter
What is the trick you are using to add the color to the text in the vb.net example?
That's just it ... no trick ... I was just as surprised when I saw it:
Quote:
Originally Posted by
irneb
Edit: Just noticed something really strange ... copy-pasting from SharpDevelop adds the colour highlights for VB.Net code but not for C# :shock:
I've tested it again, it doesn't want to work now! Not even the same VB code. This really is weird!
Re: .NET Return Conversion to TypedValue
Sorry, this is probably totally off topic.
Just found out it's something to do with this forum. There's definitely something strange going on, I remember that I was pretty fast when pasting from #Dev into the reply-box - the buttons hadn't even loaded yet. Perhaps there's some strange JS code which converts any RTF/HTML text into un-formatted text? I've tried the same from both FF and Chrome, and in both it works fine in other forums, e.g. using this to test: http://www-archive.mozilla.org/editor/midasdemo/
Seems it's due to the WYSIWYG editor turning itself off all the time: I'll have to remember to click the top-left button in the Advanced Page every time in order to have formatting inside the editor.