東方算程譚

Oriental Code Talk ── επιστημηが与太をこく、弾幕とは無縁のシロモノ。

目次

Blog 利用状況

ニュース

著作とお薦めの品々は

著作とお薦めの品々は
東方熱帯林へ。

あわせて読みたい

わんくま

  1. 東京勉強会#2
    C++/CLI カクテル・レシピ
  2. 東京勉強会#3
    template vs. generics
  3. 大阪勉強会#6
    C++むかしばなし
  4. 東京勉強会#7
    C++むかしばなし
  5. 東京勉強会#8
    STL/CLRによるGeneric Programming
  6. TechEd 2007 @YOKOHAMA
    C++・C++/CLI・C# 適材適所
  7. 東京勉強会#14
    Making of BOF
  8. 東京勉強会#15
    状態遷移
  9. 名古屋勉強会#2
    WinUnit - お気楽お手軽UnitTest

CodeZine

  1. Cで実現する「ぷちオブジェクト指向」
  2. CUnitによるテスト駆動開発
  3. SQLiteで組み込みDB体験(2007年版)
  4. C++/CLIによるCライブラリの.NET化
  5. C# 1.1からC# 3.0まで~言語仕様の進化
  6. BoostでC++0xのライブラリ「TR1」を先取りしよう (1)
  7. BoostでC++0xのライブラリ「TR1」を先取りしよう (2)
  8. BoostでC++0xのライブラリ「TR1」を先取りしよう (3)
  9. BoostでC++0xのライブラリ「TR1」を先取りしよう (4)
  10. BoostでC++0xのライブラリ「TR1」を先取りしよう (5)
  11. C/C++に対応した、もうひとつのUnitTestFramework ─ WinUnit
  12. SQLiteで"おこづかいちょう"
  13. STL/CLRツアーガイド
  14. マージ・ソート : 巨大データのソート法
  15. ヒープソートのアルゴリズム
  16. C++0xの新機能「ラムダ式」を次期Visual Studioでいち早く試す
  17. .NETでマンデルブロ集合を描く
  18. .NETでマンデルブロ集合を描く(後日談)
  19. C++/CLI : とある文字列の相互変換(コンバージョン)
  20. インテルTBBによる選択ソートの高速化
  21. インテルTBB3.0 によるパイプライン処理
  22. Visual C++ 2010に追加されたSTLアルゴリズム
  23. Visual C++ 2010に追加されたSTLコンテナ「forward_list」
  24. shared_ptrによるObserverパターンの実装
  25. .NETでマンデルブロ集合を描く(番外編) ── OpenCLで超並列コンピューティング
  26. StateパターンでCSVを読む
  27. 状態遷移表からStateパターンを自動生成する
  28. 「ソートも、サーチも、あるんだよ」~標準C++ライブラリにみるアルゴリズムの面白さ
  29. インテルTBBの同期メカニズム
  30. なぜsetを使っちゃいけないの?
  31. WPFアプリケーションで腕試し ~C++でもWPFアプリを
  32. C++11 : スレッド・ライブラリひとめぐり
  33. Google製のC++ Unit Test Framework「Google Test」を使ってみる
  34. メールでデータベースを更新するココロミ
  35. Visitorパターンで遊んでみたよ
  36. Collection 2題:「WPFにバインドできる辞書」と「重複を許す検索set」
  37. Visual C++ 2012:stateless-lambdaとSQLiteのぷち拡張
  38. 「Visual C++ Compiler November 2012 CTP」で追加された6つの新機能

@IT

  1. Vista時代のVisual C++の流儀(前編)Vista到来。既存C/C++資産の.NET化を始めよう!
  2. Vista時代のVisual C++の流儀(中編)MFCから.NETへの実践的移行計画
  3. Vista時代のVisual C++の流儀(後編) STL/CLRによるDocument/Viewアーキテクチャ
  4. C++開発者のための単体テスト入門 第1回 C++開発者の皆さん。テスト、ちゃんとしていますか?
  5. C++開発者のための単体テスト入門 第2回 C++アプリケーションの効率的なテスト手法(CppUnit編)
  6. C++開発者のための単体テスト入門 第3回 C++アプリケーションの効率的なテスト手法(NUnit編)

AWARDS


Microsoft MVP
for Visual Developer - Visual C++


Wankuma MVP
for いぢわる C++


Nyantora MVP
for こくまろ中国茶

Xbox

Links

記事カテゴリ

書庫

日記カテゴリ

あいまいさ回避...しろよ!

VBのスコープわがんね でちょっと思ったんだけどぉ

  public static void Main() {
    int[] data = new int[] { 1, 2, 3, 4, 5};
    foreach ( int item in data ) { // ココと
      Console.WriteLine(item);
    }
    int item; // ココでカブる
  }

C#だと後発の int item でエラーとなります。

 ローカルの変数 'item'をこのスコープで宣言することはできません。
 これは、'子'スコープで別の意味を持つ 'item' の意味が変更されるのを
 避けるためです。

VBよか親切な希ガス。

でね、こいつをエラーとするほどにあいまいさを回避したいのならば

class Foo {
  private int item;
  public void set(int item) {
    なぜにコレをエラーとしないのですか!
    スコープもろカブりやないですか!
  }
}

Public Class Foo
    Private item As Integer
    Public Sub SetItem(ByVal item As Integer)
        VBも同罪!
    End Sub
End Class

納得のいく説明を求ム。> .Netなひと

投稿日時 : 2008年8月12日 15:35

コメントを追加

# re: あいまいさ回避...しろよ! 2008/08/12 15:45 じゃんぬねっと

それはスコープ自体が違うからおk。

# re: あいまいさ回避...しろよ! 2008/08/12 15:59 επιστημη

いや、わかるよ。わかるんだけどぉ。
ひとつの土俵に見た目同じもんが現れるわけっしょ。
あいまいさ回避のどーのこーのと言うなら
こっちの方がよっぽどあいまいちゃうんかと。
# だからこそ this.item とか書いて区別しちょるわけで。

つまりこの、ここでの"あいまい"はコンパイラ目線での"あいまい"であって、
決してプログラマ様にとっての"あいまい"ではないのですな。

# re: あいまいさ回避...しろよ! 2008/08/12 16:03 みきぬ

> 納得のいく説明を求ム。> .Netなひと
Javaで書けたからなぁ、と責任転嫁してみるテスト。

# re: あいまいさ回避...しろよ! 2008/08/12 16:05 ネタ好き未記入

ズバリ、コンパイラの怠慢です。

# re: あいまいさ回避...しろよ! 2008/08/12 16:08 ネタ好き未記入

さらに、これも有名ですよね。

try {
int foo = GetFoo();
}
catch() {

}
Console.WriteLine( foo );

使えない面倒!
もうスコープ関係は変に親切にしないで、
コンパイラか人間かはっきりしてほしい。

# re: あいまいさ回避...しろよ! 2008/08/12 16:20 επιστημη

あー、これもモヤモヤするですね。

int foo;
try {
 foo = GetFoo();
} catch () {
...
}
Console.WriteLine(foo);

と書かざるを得んのだけど、
願わくは try の範囲とスコープとは
別物であってほしいわけね。

# re: あいまいさ回避...しろよ! 2008/08/12 16:24 Hirotow

C#とかIntelliSenceの利く言語だと、省略とかしないゆえにprivateなフィールドと引数で名前がかぶることはよくあるので、そういう仕様になってます。
普通にthis.some = someと書けばおkですし。
または引数のほうの頭に@つけるのもアリです(こちらのほうが楽)。
public void SetSome(int @some){
some = @some;
}
ですね。

>使えない面倒!
わざわざtry書く前にStreamとかWebRequestとか宣言しないといけない面倒さ。
using使えば多少ましにはなるけどさ。

# re: あいまいさ回避...しろよ! 2008/08/12 16:34 επιστημη

> かぶることはよくあるので

だからさー、それを認めるなら

foreach ( int item in ... ) { ... }
int item = ...;

これを認めん理由を失うんちゃうかと。
for とか foreach 内で使った変数なんざその場しのぎ
のad-hocなもんに決まってんだからこいつが
かぶることはよくありまくるんちゃうかと。

こんなものぁ所詮"決め事"でしかないけど、けど、ね。

# re: あいまいさ回避...しろよ! 2008/08/12 17:09 NyaRuRu

ちなみに以下のコードは OK です.念のため.

public static void Main()
{
  int[] data = new int[] { 1, 2, 3, 4, 5 };
  {
    foreach (int item in data)
    {
      Console.WriteLine(item);
    }
  }
  {
    int item; // かぶらない
  }
}

# re: あいまいさ回避...しろよ! 2008/08/12 18:41 出水

自動的に冒頭に移動させちゃうんですかね?
C++はどうだろうと思ったらちゃんとできるし…謎

# re: あいまいさ回避...しろよ! 2008/08/13 12:56 よねけん

> だからさー、それを認めるなら
>
> foreach ( int item in ... ) { ... }
> int item = ...;
>
> これを認めん理由を失うんちゃうかと。

程度の問題では?
上記のようなコーディングがどの程度必要になりますか?
インスタンス変数とローカル変数(メソッドなどの引数含む)とが同名になる/同名にしたいことは十分に想定されると思いますが、
上記の例のパターンの必要性はないと思いますし、
仮に実現できるような言語だと無用なバグを生むもとだろうと思います。

メリット/デメリットを天秤にかけずに
原理原則と一貫性だけを追い求めている学術的言語なら
使い勝手やバグの生みにくさなどを無視して
そういう言語仕様もありだと思います。

# re: あいまいさ回避...しろよ! 2008/08/13 13:25 επιστημη

> 上記のようなコーディングがどの程度必要になりますか?

どの程度必要になるか? と問われれば、

> インスタンス変数とローカル変数(メソッドなどの
> 引数含む)とが同名になる/同名にしたいこと

これと同じくらい必要になりません。
僕はむしろ同名にしたくありません。
混乱のタネと考えています。

メンバ名と引数名が同じでも構わない
のはCからの名残りじゃないかと思っています。

> 使い勝手やバグの生みにくさなどを無視して

引数名をちょっといじらなきゃいけないとして、
そんなに使い勝手が悪くなるかしら?
名前のマチガイによるバグはゼロになりますよ?

# だからそうしろ! って主張してるんじゃないです。

# re: あいまいさ回避...しろよ! 2008/08/13 17:10 みきぬ

> 僕はむしろ同名にしたくありません。
> 混乱のタネと考えています。
賛成~。


...でもバグはゼロにはならないと思う。
今でも _item と item を間違えちゃったりするしorz

# Spot on with this write-up, I absolutely believe that this website needs a lot more attention. I'll probably be back again to read more, thanks for the advice! 2021/08/29 2:04 Spot on with this write-up, I absolutely believe t

Spot on with this write-up, I absolutely believe that this website needs a lot
more attention. I'll probably be back again to read more, thanks for the advice!

# Outstanding quest there. What occurred after? Good luck! 2021/09/05 9:19 Outstanding quest there. What occurred after? Good

Outstanding quest there. What occurred after? Good luck!

# Outstanding quest there. What occurred after? Good luck! 2021/09/05 9:20 Outstanding quest there. What occurred after? Good

Outstanding quest there. What occurred after? Good luck!

# Outstanding quest there. What occurred after? Good luck! 2021/09/05 9:21 Outstanding quest there. What occurred after? Good

Outstanding quest there. What occurred after? Good luck!

# Outstanding quest there. What occurred after? Good luck! 2021/09/05 9:22 Outstanding quest there. What occurred after? Good

Outstanding quest there. What occurred after? Good luck!

# I was recommended this blog by my cousin. I am not sure whether this post is written by him as nobody else know such detailed about my trouble. You are amazing! Thanks! 2021/09/06 10:29 I was recommended this blog by my cousin. I am not

I was recommended this blog by my cousin. I am not sure whether this post is written by him as nobody
else know such detailed about my trouble. You are amazing!
Thanks!

# I was recommended this blog by my cousin. I am not sure whether this post is written by him as nobody else know such detailed about my trouble. You are amazing! Thanks! 2021/09/06 10:30 I was recommended this blog by my cousin. I am not

I was recommended this blog by my cousin. I am not sure whether this post is written by him as nobody
else know such detailed about my trouble. You are amazing!
Thanks!

# I was recommended this blog by my cousin. I am not sure whether this post is written by him as nobody else know such detailed about my trouble. You are amazing! Thanks! 2021/09/06 10:31 I was recommended this blog by my cousin. I am not

I was recommended this blog by my cousin. I am not sure whether this post is written by him as nobody
else know such detailed about my trouble. You are amazing!
Thanks!

# I was recommended this blog by my cousin. I am not sure whether this post is written by him as nobody else know such detailed about my trouble. You are amazing! Thanks! 2021/09/06 10:32 I was recommended this blog by my cousin. I am not

I was recommended this blog by my cousin. I am not sure whether this post is written by him as nobody
else know such detailed about my trouble. You are amazing!
Thanks!

# No matter if some one searches for his vital thing, thus he/she wants to be available that in detail, thus that thing is maintained over here. 2021/10/26 20:56 No matter if some one searches for his vital thing

No matter if some one searches for his vital thing, thus he/she wants to
be available that in detail, thus that thing is
maintained over here.

# No matter if some one searches for his vital thing, thus he/she wants to be available that in detail, thus that thing is maintained over here. 2021/10/26 20:57 No matter if some one searches for his vital thing

No matter if some one searches for his vital thing, thus he/she wants to
be available that in detail, thus that thing is
maintained over here.

# No matter if some one searches for his vital thing, thus he/she wants to be available that in detail, thus that thing is maintained over here. 2021/10/26 20:58 No matter if some one searches for his vital thing

No matter if some one searches for his vital thing, thus he/she wants to
be available that in detail, thus that thing is
maintained over here.

# No matter if some one searches for his vital thing, thus he/she wants to be available that in detail, thus that thing is maintained over here. 2021/10/26 20:59 No matter if some one searches for his vital thing

No matter if some one searches for his vital thing, thus he/she wants to
be available that in detail, thus that thing is
maintained over here.

# Hello, just wanted to say, I enjoyed this blog post. It was practical. Keep on posting! 2021/12/15 5:04 Hello, just wanted to say, I enjoyed this blog pos

Hello, just wanted to say, I enjoyed this blog post.
It was practical. Keep on posting!

# Hello, after reading this remarkable article i am also happy to share my knowledge here with colleagues. 2022/01/01 11:52 Hello, after reading this remarkable article i am

Hello, after reading this remarkable article i am also happy to share my
knowledge here with colleagues.

タイトル
名前
URL
コメント