#295 Why should I have to specify types at all in fan?

eching Thu 17 Jul 2008

Hi,

I am pretty much a novice "programmer" so go easy on me ;-). I use ruby at work for scripting and have worked in java on a few small projects, but still wouldn't put myself anywhere near the expert category.

What I am wondering is why should I have to specify types at all in fan code (like with fields and closure args) if there is no user compilation (is there?) and there aren't any tools for development that do the type checking as I work (much like java in eclipse). I won't know about any type errors until I run the fan code and at that point I'm no better off than using ruby.

I know tools will help with that, but without those I have a hard time understanding why I have to specify the types. Is it performance?

Sorry for being dense, but I'd like to know.

Thanks!

jodastephen Thu 17 Jul 2008

Fan compiles to normal fully typed JVM bytecode, thus types are essential information. It also means performance is good, and all the benefits of HotSpot are obtained.

I expect that tooling will appear at some point for Fan, and all that type information will produce a very usable editor.

BTW, you can run fan code as scripts, where your points make slightly more sense.

eching Thu 17 Jul 2008

Ah, a pod! I see now I was running my little tests in "script" mode.

That clears a bunch of stuff up in my mind, thanks!

BTW, I am liking Fan.

tactics Thu 17 Jul 2008

This isn't really a Fan question. You should learn a little more about type systems in general. Most importantly, you should know the difference between static typing and dynamic typing.

Dynamic typing is where the type information is determined at runtime. Ruby, Python, Perl, PHP, Javascript, Erlang, and most "scripting languages" are dynamically typed. Dynamically typed languages are very flexible. Types can usually be added or altered at runtime. However, many errors which would normally have been caught at compile time are not detected until that code is executed.

Statically typing is where type information is determined at compile time. Fan, Java, C++, C#, Haskell, and Ocaml are all statically typed. These languages are less flexible than dynamic languages, but because the type information is all available at compile time, their bytecode can be optimized to be very, very fast. The extra typing information is also useful for documentation. If you have a method onClick(MouseEvent event), you know that that event is of type MouseEvent, and consequently, you can figure out its methods. With a dynamically typed language, this information must be supplied for you in the documentation (and often... it's not).

Fan is actually statically typed, but was made to feel like a dynamic language. It is automatically compiled for you before it is executed. The equivalent compiled code (the .exe, .obj, .py, .class files, etc...) are not ever created on your disk unless you compile the .fan files into a pod.

Most statically typed languages like Java and C++ require you put down a variables type when you declare it. However, there are techniques for figuring out variable types automatically at compile time without having to specify (or "annotate" as it's usually called) the type. This is called type inference. It was first popularized in ML, which is a statically typed language which doesn't require the program to ever specify a single type.

cbeust Fri 18 Jul 2008

Just to add to the good summary that tactics just posted: one of the main benefits of static typing is that it enables automatic refactoring.

Most automatic refactorings that you find in Eclipse/IDEA cannot be performed in dynamically typed languages like Ruby or Python because the IDE doesn't have enough information to figure out what codes needs to be changed and what code needs to be left alone.

-- Cedric

eching Fri 18 Jul 2008

Thanks for all the responses, good stuff.

Yeah the main problem was I wasn't aware of compiling pods (duh). Not really sure how I missed that (well I know how, I like to play first before reading)

I should research more before asking the obvious. I was looking in /bin for fanc =p

Login or Signup to reply.