2013年4月10日 星期三

Static Typing, Dynamic Typing, Strong Typing, Weak Typing

Static Typing

如果一個程式語言中的每個變數型別在編譯時期就完全決定好,我們稱這個程式語言屬於static type。這類的程式語言在使用變數時通常都需要先宣告變數,編譯器會在編譯時期去做type checking。比如說Java, C, C++, C#。以下是一段C的範例:

int num, sum; // explicit declaration
num = 5; // now use the variables
sum = 10;
sum = sum + num;

變數使用前需要宣告是static type的特徵之一

Dynamic Typing

Dynamic type和static type相反,一個屬於dynamic type的程式語言不需要宣告變數,因此編譯器無法對型別做檢查在編譯時找出型別上的錯誤。在執行期間存取變數時,會透過適當的機制來判斷此變數可能的型別。Dynamic type不需要事先宣告變數就能直接使用,比如說Python, PHP, Ruby, Perl。以下是一段Python的範例程式:

sum = 1   // directly using the variable
sum += 2

上面的範例我們直接使用變數sum而沒有事先宣告,這是dynamic type的其中一個特徵。 簡單來說,Static typing會在編譯時去做type checking。而dynamic type會在run-time時做check。

Strong Typing

Strong typing很常會和static typing以及dynamic typing搞混。Strong typing是指變數會被綁定到某個型別,除非經過明顯的型別轉換,不然型別不會任意改變。比如說Python就是strong typing,以下是個範例:

>>> x = 3
>>> y = '4'
>>> print(x+y)
Traceback (most recent call last):
  File "", line 1, in 
TypeError: unsupported operand type(s) for +: 'int' and 'str'

Python不允許int和string相加,除非我們主動去做型別轉換,不然x和y變數不會經過隱藏的型別轉換加在一起。這就是Strong typing的主要特徵。Python是dynamic typing同時也是strong typing

Weak Typing

Weak typing是指型別的概念比較弱,變數的型別可能隨時會經過隱性的轉換。比如說JavaScript,以下是JavaScript的範例:

var x = 3    
var y = '4'
alert(x + y) //Produces "34"

不同於Python,JavaScript允許這種語法並且會自動幫你做型別轉換。這就是weak typing的特徵之一。

Strong Typing v.s. Weak Typing

其實這兩者並沒有一個約定成俗的定義,很多程式語言都同時有兩種的特性。只能說偏向某一邊比較多。比如說C,大部分人認為C偏向Weak Typing,因為C有pointer可以做任意轉型,同時也有void *這種變數可存取任意型別。

2013年4月9日 星期二

Type-safe v.s. Type-Unsafe

某一些程式語言比較自由,允許我們去做一些危險的動作。比如說C和C++的Pointer就允許我們直接對記憶體做操作。這類的功能讓程式語言變得很強大,但是一不小心有可能會讓程式整個當掉。這種允許我們任意存取記憶體位置的程式語言就不能算是Type-safe。比如說以下的C程式讓我們access超過int boundary的位置:
int i = 10;
char *s = (char*)i;
print(*(s+10));
這種問題不會發生在C#身上,因為C#是Type-safe的程式語言。以下範例在C和C++合法,但是在C#會丟出錯誤訊息:
double d = 10.59; 
int i = 5; 
i = d; //this causes an Error
在C#中不能隨便把某個型別的資料當作是另外一種型別,如果編譯器認為這樣轉型是不合法的,在編譯的時候就會丟出錯誤。

[C#]Delegate

如果你有學過C,C#的delegate其實很類似C的function pointer。delegate讓我們可以把function透過參數的形式來傳遞,也就是說delegate可以讓我們把function當作first class opject來操作。下列是一個delegate的簡單範例:
using System;
delegate void StringProcessor(string input);
class Person
{
    string name;
    public Person(string name) { this.name = name; }
    public void Say(string message)
    {
        Console.WriteLine("{0} says: {1}", name, message);
    }
}

class SimpleDelegateUse
{
    static void Main()
    {
        Person jon = new Person("Jon");
        Person tom = new Person("Tom");
        StringProcessor jonsVoice, tomsVoice;
        jonsVoice = new StringProcessor(jon.Say);
        tomsVoice = new StringProcessor(tom.Say);
        jonsVoice("Hello, son.");
        tomsVoice.Invoke("Hello, Daddy!");
    }
}
此範例中我們建立了宣告了兩個delegate的instance,分別是jonsVoice以及tomsVoice。最後用兩種不同的方式呼叫function
使用delegate要有以下三個步驟:

1. 宣告delegate type
delegate void StringProcessor(string input);
這行就是我們宣告delegate type的地方,這個delegate的type是含有一個string參數且回傳值為void的function。此delegate type只能接受回傳值以及參數個數一模一樣的function。不過有一種特殊情況:
void Test(object x);
此function也可以被assigned給此delegate的instance,因為string是繼承自object

2. 建立delegate instance
StringProcessor jonsVoice, tomsVoice;
jonsVoice = new StringProcessor(jon.Say);
用new可以建立剛才宣告好得delegate instance,建立傳入的參數是之後要透過delegate呼叫的function

3. 呼叫delegate instance的function

編譯之後會把jonsVoice("Hello, son.")轉成jonsVoice.Invoke("Hello, son.")
最後又會透過delegate呼叫原本assigned的function jon.Say("Hello, son.")

delegate很常用在UI Button的click,我們會事先宣告EventHandler的delegate instance。當button被按下時,執行對應的處理function。有點類似call back function。

每個delegate instance內部都有一個invocation list用來紀錄function,也就是說一個delegate instance可以紀錄不只一個function。當有多個function時,delegate會一一呼叫invocation list中所儲存的function。我們可以透過+=以及-=運算子來新增移除function。以下是個簡單的範例:

void HowAreYou(string sender) {
  Console.WriteLine("How are you, " + sender + '?');
}
 
void HowAreYouToday(string sender) {
  Console.WriteLine("How are you today, " + sender + '?');
}
 
Notifier greetMe;
 
greetMe = new Notifier(HowAreYou);
greetMe += new Notifier(HowAreYouToday);
 
greetMe("Leonardo");                      // "How are you, Leonardo?"
                                          // "How are you today, Leonardo?"
 
greetMe -= new Notifier(HowAreYou);
 
greetMe("Pereira");                       // "How are you today, Pereira?"

2013年4月3日 星期三

[C#]快速讀取、寫入檔案的方法

C#提供一些快速的Method可以直接寫入或是讀取檔案,善用的話可以節省一些時間
以下三個static methods只需要一個步驟就可把資料從檔案讀到記憶體中,傳入參數都是讀取檔案的路徑
File.ReadAllText(path)                        // (回傳string)
File.ReadAllLines(path)                       // (回傳array of strings)
File.ReadAllBytes(path)                       // (回傳一個byte array)

下列四個static methods同樣只需要一個步驟就可把資料寫入到檔案中,傳入參數是路徑以及寫入的資料
File.WriteAllText(path, string)
File.WriteAllLines(path, array of strings)
File.WriteAllBytes(path, bytes)      
File.AppendAllText(path, string)              // (適合用在log file)
File.ReadAllLines和File.ReadLines看起來很像,但是ReadAllLines會一次把所有資料讀到記憶體中
ReadLines會回傳一個IEnumerable<string>,不會一次把所有資料讀到記憶體中

2013年4月2日 星期二

如何讓Visual Studio 2012中compile的程式可在Windows XP上執行

最近發現一個問題,Visual Studio 2012預設並不支援Windows XP。也就是說Compile出來的程式沒辦法在XP上執行。花了一點時間上網找解決方法,發現微軟在去年年底出的Visual Studio 2012 Update 1已經可以支援XP了。程式開發人員可以在Visual Studio 2012上撰寫程式並compile成XP可執行的執行檔。

安裝好更新檔之後還需要設定專案所使用的toolset為Visual Studio 2012 – Windows XP (v110_xp)

詳細步驟如下:

1. 在Solution Explorer中的Project上按右鍵並點選Properties

2. 在左手邊樹狀目錄中選取Configuration Properties, General,把Platform Toolset property設定成Visual Studio 2012 – Windows XP (v110_xp)

3. 重新編譯後會產生可在XP上執行的執行檔