views:

733

answers:

17

I've been a Ruby developer for several years, and a Perl developer before that, with some PHP somewhere in there. I want to expand my knowledge, and think the best way to do that will be by learning something very different from what I know. I know OO, and I know functional programming pretty well. What I don't know is a language that uses static typing effectively. (Actually, ok, I kind of know Java, but not enough. You can still recommend that.)

What language is going to best show me the features of static typing coming from a dynamic background?

+1  A: 

C#. But that's the best language for anything, as I'm sure you're all aware.

Alex Fort
For real, C# is very powerful but not that difficult to start with.
Ed Schwehm
best language for anything?...yeah right...
mmattax
+3  A: 

Well, there are clearly lots of options out there. If you're just learning for the sake of learning, without any specific application in mind, I would recommend C. A low level language like that will broaden your understanding of many aspects of computing, not just features specific to statically typed languages.

Kamil Kisiel
+5  A: 

F# is a fantastic static-typed functional language with phenomenal type inference eliminating a lot of the code bloat often associated with statically-typed languages. It also has access to the .net libraries which are extensive and very well designed, and being multi-paradigm its flexible enough to allow you to use imperative programming when required. It really is one of the best languages out there at the moment! :-)

kronoz
F# is Microsoft's partial port of OCaml on the .NET runtime and libraries. They had to reintroduce stupidities such as null to reuse existing libraries... If you still opt for F#, my advice is to learn OCaml, then go for F# to leverage its huge standard library.
bltxd
+3  A: 

Everyone should learn C. Then there's interesting functional languages such as OCaml with type inference in addition to static typing. Some say it can give you a nice boost in productivity. I'm currently getting into Haxe which is statically typed but compiles into Javascript and PHP among others, so you can mix and match.

jjrv
+2  A: 

Learn C. It might seem painful at first but it will teach you about static typing and memory management, both of which are good things to have a handle on.

Plus there's so much code out there written in C; it's a shame to not understand the language even if you don't end up using it that much.

Steve Losh
A: 

C.

You've had all the languages that made all the hard things easy, now you need to learn the language which makes easy things hard.

When you've mastered C and the joys of manually pointing memory, step up to C++ ( considering your background, you probably don't have windows, so C# is just "what?" for you )

You could learn assembly, but ... yeah, you'll be back on dynamic languages just loving how they just work. :)

Kent Fredric
+9  A: 

I would suggest Haskell. Because of type classes as well as some of the type system's more exotic features, the code you will write has the most dynamic feel of modern, statically-typed functional languages.

Like F#, Haskell has type inference, but it has an even more powerful type system.

Denis Bueno
C++ also has a "more powerful" type system but that doesn't equate to "better". Look at the 600 line Haskell equivalent of `a from OCaml, for example.
Jon Harrop
I, too, would suggest Haskell. But OCaml/F# are good candidates as well.
SealedSun
A: 

My recommendation would be Java or C#, but both have some downsides coming from a dynamically typed language.

Java is 100% statically typed, but miss neat stuff like function pointers. C# is mostly statically typed, but newer versions (3 or 3.5, can't remember) allows you to use the 'var' keyword to allow for dynamic typing.

I still think Java is solid, but whether you chose C# or Java is a matter of opinion (and what platforms you want to distribute on due to the lag in versions with Mono and other CLR implementations on non-Windows systems).

Christian P.
The var keyword doesn't constitute dynamic typing in C#, it simply uses type inference to reduce keystrokes.
kronoz
Well to be pedantic it's also used for anonymous types (which you can't declare using other type declaration), but that's still statically-typed :-)
kronoz
+1  A: 

How crazy are you feeling? :-)

Java and C# are both popular and widely used. Both have recently added some interesting type system features: Java generics, and local type inference in C#. C++ templates are also interesting, in a perverse sort of way. You'll get a nice, gentle introduction using any of these languages.

If you're looking to really stretch your brain, look at Haskell or ML. These languages have more advanced type systems (type inference, Haskell's typeclasses, ML's parameterized modules).

emk
+1  A: 

Java is a useful language, but I'm not sure whether you will learn much from it if you are used to OO programming in dynamic languages.

Perhaps you should try a language with strong type-inference such as Haskell?

See What To Know Before Debating Type Systems for a good summary about the more interesting things that static typing can do for you in some of the stronger static typing languages.

djpowell
+11  A: 

I'd say OCaml, since that's what whet my teeth for static typing. I tried it out after programming in Ruby for a while, and though there was a bit of a brain break with the fact that its not C-like at all, after a little while, I got it.

There's a bunch of languages out there that put C and Java's static typing to shame.
OCaml's just one of them, but I enjoyed that it was object-oriented (if you wanted it to be), and highly functional (I heart anonymous functions). I also fell for algebraic types and type inference (static typing doesn't have to mean explicit typing).

Plus, since OCaml can be interpreted or compiled, it's a nice transition from a interpreted language like ruby.

rampion
Agreed. OCaml's structural typing (objects and polymorphic variants) really close the remaining gap with dynamic typing. You just don't get that in other languages (including Haskell).
Jon Harrop
+3  A: 

Coming from a dynamically typed background, I would recommend a language with Henley/Milner type inference. That would make it much easier to transition to all of the "pain" of type annotations. So in other words, either Haskell or one of the MLs:

  • SML (very clean language, but not so practical)
  • OCaml
  • F#

Of the four choices given, I think that SML is the easiest, but F# is probably the most applicable in modern environments.

Daniel Spiewak
+4  A: 

Scala's a functional/oo bybrid with static typing. It has proper mixins and it's generics system is pretty smooth. And if you feel the need to do duck typing, there are structural types.

sblundy
+1  A: 

It depends on what you want to do with what you learn. If you want something marketable, learn either Java or C#. Both have solid communities and a lot of opportunities for employment right now.

If you want to expand your understanding, then learn C. The languages you already know are very good at hiding what's going on "under the hood," but eventually you're going to want to learn what's going on at the stack/register level, and C is as close to Assembly as you can get. Learn how to de-reference a pointer, how to open a file stream, how to handle strings on a character-by-character basis, and how to manage memory without the convenience of automatic garbage-collection. You won't necessarily get a lot done in C, but you will gain a huge appreciation of just how much hand-holding other languages do for you.

And then, if you can stand it, learn Assembly. Become the machine. :)

rtperson
+2  A: 

boo - because it is inspired by a dynamically typed language (python) and a little bit by Ruby, uses type inference to give the partial-illusion of being dynamic, and really has little language overhead.

torial
+1  A: 

I would suggest you try out modula-2. It has none of the drawbacks and pointer pitfalls that C has and can handle from the lowest level to massive projects with its excellent modularity. Writing in a non-preemptive multitasking environment? I supports threads internally.

+2  A: 

Both Ocaml and Haskell have already been mentioned, but I'll just pitch in with an additional aesthetic judgement:

-Ocaml for practical work

-Haskell for beauty

Don Wakefield