Dynamic typing in C# ?

Been searching for some time now about this …

Mono site says it’s not available : Redirecting…

Isn’t there some way to dynamize a Type in .NET 1.1 ?

Do we have to wait for 2.0 with type inference ?

I am pretty sure that the current version of Mono that Unity is running on (1.2.5) does not support the dynamic typing feature of c#. I however believe that getting used to strict type promotes cleaner and less buggy code. My day job is PHP which is a dynamically typed language and while dynamically typed languages does make life easier some times, it can create bugs at the same time so it is kinda of a double edged sword.

For sure it’s safer, but dynamic typing can avoid 7389727892 lines long classes … :stuck_out_tongue:

LMAO. I do agree it does have benefits but only if it is used only when it is truly useful. I would hate to see a ton of code like this:

var my_int = 9;

where the dynamic typing has no real purpose it this case.

haha, I feel you on this one xD

or like :

var PleaseAdressMe200MbOfRAM = 1;
var my_int = 9;

In Unity JavaScript and Boo this IS strongly typed. JS (and Boo, which it is based on) use type inference which guesses the type when a variable is defined.

Why adding redundant information, when the definition makes it clear what type it is used for?

Also, Boo in Unity does support dynamic typing using the duck type, offering most of C# advanced features while having a lightweight syntax.

Just sayin’ :slight_smile:

Didn’t try Boo, but as C# is natively strict, I believe it is faster :slight_smile:
(maybe I’m wrong, though)

And overall, C# classes feel more “pro” :roll:

Sorry, I did not use proper “lingo” when describing my example.

Yes I know that

var my_int = 9;

is a strict typed variable and that I could not go around and do

my_int = "a string";

as that would break the code. My point on that example is that since I know my_int is of type int, then why not just do

int my_int = 9;

to give a better example of why I don’t like type inferencing, lets look at the following code.

var var1;
var var2;
var var3;
var var4;
var var5;

If I look at this code I would need to actually look in the code to see what type these variables are. Now in most cases (at least for me), you are going to know what type a variable is, so for those cases, I just feel it is cleaner to tell anyone looking at your code (including yourself) what type they are when you declare the variable.

Also, it is slightly faster AFAIK to not use type inferencing.

That’s not type inferencing, that’s dynamic typing. None of those variables are typed. This is why type inference is nice:

var foo = Vector3(3.0, 4.0, 5.0);
var bar = Matrix4x4.identity;

as opposed to

Vector3 foo = new Vector3(3.0f, 4.0f, 5.0f);
Matrix4x4 bar = Matrix4x4.identity;

Since it’s obvious and already stated that it’s a Vector3 or a Matrix4x4 (and so on), there’s no need to tiresomely repeat those all the time. Note that C# has type inference because of these benefits, but Unity won’t be able to use it until its Mono implementation is eventually upgraded to match .net 3.0.

It’s not, since the CIL compiles to the same thing. Type inferencing is a compiler thing, nothing to do with run-time. Presumably it slows down compiling a little, but probably not enough to be noticeable.

–Eric

It is still not dynamic typing (at least what I concerned dynamic typing which is the ability to change the type of a variable from one to another) in C# because once I do

var1 = 1;
var2 = "string";
var3 = 1.0F;
var4 = Vector3(3.0, 4.0, 5.0);
var5 = Matrix4x4.identity;

they are now statically typed but I do understand that until the variable is initialized, it is not tied to any specific type.

I was unaware that type inferencing was done during compilation and not run time but I still thinking declaring the type when you declare the variable is cleaner if you know what it is beforehand. Everyone codes in there own way, I just find that being lazy in one area where it is not a big deal can sometimes lead to being lazy in another area where you should not be and with the code completion editors have today, there is really not that much extra typing.

Also

While that is true when you declare and initialize the variable at the same time but with class members/fields, the declaration and initialization is in separate areas.

No, they’re not.

I can do:

var test1;
test1 = 1;
Debug.Log(test1.GetType()); // System.Int32
test1 = 2.0;
Debug.Log(test1.GetType()); // System.Single
test1 = "3";
Debug.Log(test1.GetType()); // System.String

And that’s what I call dynamic typing.

In Boo it’s more explicit since there’s no var and so you can only define a variable by setting it or by defining its type. Dynamic variables are only created by defining them as duck.

In JavaScript, by using var and specyfing no type, you implicitly declare the variable as dynamic (duck). You cannot change that later on.

The code you provided does not compile with visual studio (assuming that is c# code).

I also just found out that when using var, you have to initialize the variable at the same time so my comment about have the declaration and initialization of variables declared with var in different places is also not true. I guess I should have read up on var a little bit more before making some of the comments I did (I don’t use them unless I have to).

No its UnityScript code as it is present in Unity 2.1, 2.5, 2.5.1

Ah, sorry, it seem like we were taking of different languages. The example is written in Unity’s JavaScript. To compile it, you have to do that in the Unity IDE.

Unity’s version of Mono doesn’t yet support C#'s var statement and dynamic typing and so we were talking about dynamic typing in Unity’s JavaScript.

In JavaScript, var is used differently from what it’s used for in C# 3.0. In C# it’s a pseudo-type to tell the compiler to figure out the type himself - i.e. some kind of explicit type inference. In JavaScript it’s simply used to define variables:

// This is a full variable declation
var myVar :myType = myValue;
// The shortest form is
var myVar;
// In which case myVar is dynamically typed (see example above)

Hey there, just to let you know I found some way to simulate dynamic typing, thanks to several sources in here and over the net.

using System.Reflection; // include at the top of the C# script



MethodInfo _method = typeof(YourClassName).GetMethod(_methodName); 
_method.Invoke(MyClassInstance, null);

Well, the YourClassName is not a string, but could be by using GetType(string _className) method (more info on MSDN).

But … I made some latency test comparing to native function calling (aka. MyFunction() ), and as expected, Invoke is slower.

Invoke : 0.03 ms
native : 0.01 ms to 0.009 ms

For a way to get C# 3.0 language features, including implicitly typed local variables, take a look at: http://answers.unity3d.com/questions/746/the-type-or-namespace-var-could-not-be-found-in-c

what is a var ?

a “variable type” declaration (not strong typed)

Thanx dreamora
I was being facetious
as i think dynamic typing is the devils work

Not devils work … but definitely the work of a very sarcastic beeing as you need to take a spiteful, malicious delight in the misfortune of others when they have to debug such stuff