Writing .NET in dynamic language?
I'm confused by the possibility of writing .NET in dynamic languages, such as (Iron)Ruby. Particularly, I've seen code in IronRuby that used generics (...foo[String]
), but I'm not aware of this feature in Ruby as it seems nonsense to me in dynamic languages.
So, when I write .NET app in IronRuby, how is it with type safety and compilation? I thought that it is just as dynamic as Ruby everywhere e开发者_开发百科lse. I thought that if the Ruby syntax is OK all the type checking would be done at the runtime.
Also, as far as I know, .NET itself is type-oriented - there are classes that heavily utilize the mentioned generics. How is this handled?
And what about delegates? In dynamic languages I can have almost function-spaghetti and sometimes, its just fine (like hacking UI in javascript).
Or do I have to care even about generic delegates?
Bear in mind that even languages like C# and VB .NET are now (optionally) dynamic e.g instead of
string strongTypedReference = "Hello";
Since .NET 4 you can now also do:
dynamic dynamicReference = "Hello";
Therefore:
strongTypedReference.Foo() // gives a compile-time error as expected.
dynamicReference.Foo() // compiles, but will give a run-time error.
dynamicReference.ToString() // compiles and binds to the correct call at run-time.
So yes, with dynamic, the compiler does not check resolution of method calls, property accessors etc. at compile time but at runtime instead. This applies to all languages that offer dynamic typing in .NET e.g. IronRuby, IronPython, C#, VB etc.
精彩评论