Lorenz Cuno Klopfenstein

Posts tagged "Cobra"

While I was implementing the data import/export functionalities of MotoX Stopwatch, having decided to use the XmlSerializer to load and save human readable files, I found out that the serializer doesn't work for TimeSpan values for some reason!  :S

It appears to be a known problem and several other people have already posted their workarounds. However, here's my solution using Cobra: mark the TimeSpan properties (which cannot be serialized normally) with the XmlIgnore attriute. This will force the serializer to skip them. Then declare new string properties, which you will never use in your code, that simply wrap the original TimeSpan value.

use System.Xml
use System.Xml.Serialization

class StopWatchInfo
	has Serializable

	# ... normal properties ...
	
	# The next property will be ignored by the serializer
	var time as TimeSpan
		is public
		has XmlIgnore
		
	# This property wraps the one above for the serializer
	pro timeWrap as String
		get
			return .time.toString
		set
			ret = TimeSpan()
			if TimeSpan.tryParse(value, out ret)
				.time = ret
			else
				.time = TimeSpan.zero

Remember that the serializer uses the property names of the class, therefore "timeWrap" will appear in the output file instead of "time". To avoid this, you'll want to rename the wrapping property adding an XmlAttribute to it.

Posted on Tuesday, November 11, 2008
377 Views
2 comments posted

Stopwatch - from Geekpedia.com I just published my new small project "MotoX Stopwatch" on the website: it's basically a simple stopwatch that allows you to keep track of times and laps for multiple people.

It's also my first program written with the Cobra language and released under the "Do What The Fuck You Want" public license (I always wanted to use it  :D)!

Posted on Monday, November 10, 2008
Tagged as
252 Views
1 comments posted

As I started working on a little pet project for my friend NiKo, before clicking on the Visual C# icon (as I usually do) I actually started thinking about using languages other than C#. As blasphemous as it might sound, I really liked the idea of getting something to work using a "new" language, even if I had to study it and glance over the documentation the whole time (besides, you should learn a new language every year).

Jaipur picture by Carolina Mendonça on Flickr.com But still, I absolutely wanted the program to run on .NET because I'm really used to the base class library and to Windows Forms in particular: therefore, IronPython, IronRuby and similar languages came to my mind. Brooding about that on the train back to Foligno, I was almost sold on using Python, since I have been reading a lot about it and never had the chance to use it. But then, I discovered another "reptile" programming language for .NET: the Cobra language.  :)

Cobra has a similar syntax to Python but seems to better adapt to .NET (properties and such) and drops all the - in my opinion - extremely ugly conventions of Python (like the double underscores, all the self declarations in member functions, and so on).

Starting using Cobra

All you need to start programming with Cobra is in the small package you find on the website: the Cobra compiler essentially converts all cobra code to C# files (referencing its runtime Cobra.Lang.dll if needed) and then compiles to an executable using the standard C# compiler.

I also didn't use any IDE for my work with Cobra: just Notepad++ (Python syntax highlighting works quite well) and a hand made batch file to compile all files. This rudimental programming style was especially painful in the beginning: I noticed I can't even remember the namespaces of all classes I use...  :S But anyway, at the end of the day you gain some speed and I also noticed that I seemed to be more focused without IDE (might as well be a completely wrong impression since I was still struggling with the language).

Writing a Windows Forms project

The project I mentioned is mainly focused on the GUI and working with Windows Forms on Python, without a visual designer can be pretty boring and painful. Anyway, it works and it actually forces you to realize how hard it is to design a decent looking GUI directly in code.

Anyway, I was almost stuck with two big problems. I will explain them here, hoping that someone will find the solutions I found useful someday...  :)

Nested class references

The Cobra compiler (version 0.8) has some problems in referencing nested classes sometimes. I had the exact problem as also seen on the Cobra forums, where the compiler would spit out this

error: Cannot locate nested CLR type "System.Windows.Forms.Layout.LayoutUtils+MeasureTextCache" (simple name is "MeasureTextCache"). Compilation failed - 1 error, 0 warnings

as soon as I referenced the "Label" class in my code (and working without labels can be quite difficult  :D).

There is no working solution, but only an ugly workaround hack: first of all, download the Cobra source from the repository:

svn co http://cobra-language.com/svn/cobra/trunk/ cobra-workspace

You'll discover that the Cobra compiler itself is written in Cobra. Open up the file /Source/TypeProxies.cobra and go to line 182 or so. You'll find this:

if potential is nil # comes up on MS Windows .NET 2.0 for multiple types when using System.Windows.Forms # error: Cannot locate nested CLR type "System.Windows.Forms.UnsafeNativeMethods+IOleControl" (simple name is "IOleControl"). if 'NativeMethods' in clrType.toString return _hack(clrType) .throwError('Cannot locate nested CLR type "[clrType]" (simple name is "[clrType.name]").') else if potential inherits IType return potential else .throwError('Located CLR type spec "[clrType]" but got a [potential.englishName] instead of a type.')

As you can see, the bug is known but not fixed. Replace the code with something like this:

if potential is nil # comes up on MS Windows .NET 2.0 for multiple types when using System.Windows.Forms # error: Cannot locate nested CLR type "System.Windows.Forms.UnsafeNativeMethods+IOleControl" (simple name is "IOleControl"). if 'NativeMethods' in clrType.toString return _hack(clrType) #.throwError('Cannot locate nested CLR type "[clrType]" (simple name is "[clrType.name]").') return _hack(clrType) else if potential inherits IType return potential else .throwError('Located CLR type spec "[clrType]" but got a [potential.englishName] instead of a type.')

I know this is ugly and could probably bite you somewhere in other code, but it worked for me.  :D Recompile cobra (launching comp.bat) and replace the generated cobra.exe with your current compiler from the 0.8 version.

OpenFileDialog blocking

Another mysterious problem was caused by a simple call to OpenFileDialog.ShowDialog() which was blocking the whole application thread without showing any dialog. I investigated for hours to find the reason for this very weird behavioud and finally found out it was all my fault for forgetting something very basic...  :|

Therefore: always ensure that your Windows Forms application has the STAThread attribute set! It's easy to forget it because Visual Studio always generates those bits of code for you...

use System.Windows.Forms class MyApplication def main is shared has STAThread Application.enableVisualStyles Application.setCompatibleTextRenderingDefault(false) Application.run(MainForm())
Posted on Tuesday, November 04, 2008
380 Views
0 comments posted
Back to Klopfenstein.net
Clemens Klopfenstein Serena Kiefer Lukas Tiberio Klopfenstein Lorenz Cuno Klopfenstein
English German