Sunday, May 3, 2009

Some people think that dynamic typing is bad! This is because most of them get confused with Static Typing & Strict Typing. They think dynamic typing means 'No Type'.In Python, for example, we have strict typing, but most of times, the type is guessed by the compiler itself.

>>> a=5

>>> type(a)

int

>>> b=5.43

>>> type(b)

float

>>> c='test'

>>> type(c)

str

The best part of the fact is that you can have a complex type (e.g. Dictionary) which has different key types (despite C# or so).

>>> myDic={'test':45 , 56.7:'sd'}

>>> type(myDic.keys()[0])

str

>>>type(myDic.keys()[1])

float

>>> type(myDic['test'])

int

>>> type(myDic[56.7])

str

3 comments:

  1. Maybe I have learned from not proper sources but I am thinking these phrases are unrelated completely.

    We have 2 orthogonal ranges for describing types:
    One dimension is Statically Typed - Dynamically typed and the other one is Strongly Typed - Weakly (loosely) Typed for programming languages.
    Since I can not draw here I write down 4 quarters of my diagram clockwise:

    Strongly/Statically Typed: Java, C#
    Strongly/Dynamically Typed: Ruby, Python
    Weakly/Dynamically Typed: JavaScript, PHP
    Weakly/Statically Typed: C

    The concept of being strictly typed expression in a programming language is mostly in the area of statically typed ones. In a language like Java you need to Strictly declare the type of every bit of your code. In a language like Scala (or Haskell) the compiler "Infer"s types from your expressions "at compile time".

    It is just recently that using strict type annotations got a place in dynamically typed languages like what ActionScript 3.0 allows to be written and PowerShell has it too. But in fact it is of no use! Because it does not helps out the compiler/interpreter much. It is just a shorthand for writing down less lines of dynamically type checking.

    ReplyDelete
  2. Yes u r exactly right! Python is Strongly (I said strictly, by mistake) typed, but compiler dynamically guesses the data type.
    VB Compiler does not try to guess types. It just 'Infer's types as you said. You can not have a Array or Dicitonary of various types in VB.
    From the other hand, you can not compare VB syntax with Python. Python readability is much better.
    About you previous comment, a functional (Dynamic) language does not have to be fully OOP, since it is in fact a 'Hybrid' one. And, obeying OOP so strict is not necessarily a preference for a language.
    Nowadays, dynamic languages are so growing mostly since they make development really 'Agile'. They accelerate development noticeably, without noticeable loss of speed most of the time. There are tons of articles from famous pro programmers against OOP. They can prove obeying OOP does not necessarily make debugging easier, and being hybrid does not make it harder. I am a Python developer for 2 years, I have absolutely NO PROBLEM with debugging Python as a dynamic hybrid language. Do you know why u don't think so? Since u have never really developed a serious project with one of them. When developing in a dynamic language, you must change your concept and point of view. Think of when u first faced OOP. you thought that is boring and unusable! but after months of development, you make fun of older methods! it is just because you have changed your view to programming concepts.

    ReplyDelete
  3. I consider saying VB has 'type inference' as an insult to Haskell, Scala, OCaml and F#! ;)

    And about real project; you are right. I have forced to use PHP for one and now I can kill everyone who suggest me to use PHP! Maybe this bad phenomenon made up from that experiences (And nothing on PHP please!).

    ReplyDelete