Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it Possible to create an entire application using just dynamic parts of C#?

Tags:

c#

I am feeling rather impish about this, so please don't think I would seriously try this as I know from bitter experience the strengths of a static type checking system.

But, as for a concept, would it be possible to basically do C# in a completely dynamic way, throwing the static type checker to one side and saying "pah, you're so old fashioned!".

I have recently had great experience of IronPython and F#, but I feel truly at home in a C based language.

How far can one take C# on the road to dynamics? I recently wrote an XML to ExpandoObject parser and that was great, it felt like javascript prototyping.

How optimised is the dynamic stuff, is it as quick as some of the real Dynamic programming language environments out there such as NodeJs and CPython?

like image 309
WeNeedAnswers Avatar asked Jan 27 '12 14:01

WeNeedAnswers


2 Answers

would it be possible to basically do C# in a completely dynamic way?

Sure, why not? Just type everything as "dynamic".

I have recently had great experience of IronPython and F#

I note that F# is not a dynamically typed language; it is an implicitly typed statically typed language. Just because you don't see the type checking does not mean it isn't there.

How optimised is the dynamic stuff?

It's pretty optimized for speed but heavy on the memory use and code size. We build a cache on every call site. The second time you hit a call site it should be very fast indeed, but you pay a memory cost for that.

is it as quick as some of the real Dynamic programming language environments out there such as NodeJs and CPython?

Try both, measure them, and then you'll know.

like image 140
Eric Lippert Avatar answered Oct 13 '22 13:10

Eric Lippert


Pretty much. Of course, at any point where you interface with other code (including the FCL) that expects particular types, you'll have to cast to the type expected. But if you were really bullish about this you could create some methods like:

public static object IntegerAdd(object x, object y)
{
  return Convert.ToInt32(x) + Convert.ToInt32(y);
}

Now from the rest of your code the fact that there's some static typing going on need never upset you!

I have recently had great experience of IronPython and F#,

F# is a bit more like really heavy use of var and really heavy use of type inference, than it is like the use of dynamic and late-binding. It's not rare for some C# code (particularly with heavy use of linq) to do quite a lot without a single explicitly declared type, but to be statically-typed through and through, and that'd be a closer comparison.

How far can one take C# on the road to dynamics? I recently wrote an XML to ExpandoObject parser and that was great, it felt like javascript prototyping.

If you wrote it because you enjoyed experimenting with this approach, then fantastic.

If you wrote it because you needed something it gave, then also fantastic.

And if you wrote it because you needed something it gave, and you enjoyed experimenting with the approach, then bloody brilliant! What more can you ask for in a job?

If you're using it to shy away from C#'s strengths toward javascript's strengths, then I'd suggest caution. We're a long way now from static typing being "old fashioned". Both approaches have their strengths. I'm not saying that to be diplomatic, I really think they do. C# offering a mixture of both means it can benefit from some of the strengths of dynamic typing despite generally being a strongly typed language. It's an expressive and efficient combination, and there's no need to shoot it in one leg because you like the other leg better.

like image 33
Jon Hanna Avatar answered Oct 13 '22 13:10

Jon Hanna