Quantcast
Channel: わんくま同盟
Viewing all articles
Browse latest Browse all 994

Visual Basic 2015の新機能 その1

$
0
0

これは、Visual Basic Advent Calendar 2014の12/8日の記事。

【VB2015(VB14.0)の新機能】参照:VBの新しいステートメントUpcoming Features in Visual Basic
1.Select Caseステートメントの拡張
2.読み取り専用の自動実装プロパティー
3.複数行にまたがった文字リテラル
4.yyyy-MM-dd型の日付リテラル
5.バイナリーリテラル
6.数値区切りリテラル
7.複数行にまたがるステートメントに対するコメント
8.バグ修正と小さな変更
9.C#との相互運用でOverridesキーワードがある場合のOverloadsキーワードの省略を可能に


1.Select Caseステートメントの拡張について説明します。
この機能を確認しようとVisual Studio 2015 Preview版をインストールしましたが、結果的にはまだ未実装でした。

Roslynのコードリポジトリ上も、masterブランチにはまだとりこまれていない機能なのか、pattern-matchingっていうブランチが
見られます。この機能が最終的に実装されるか分かりません。
こういう機能を追加したいと思っているのは確かなので、とりあえず説明していきます。

以前からVBやC#に関数型言語の特徴までを取り入れようとするのが見受けれられましたが、今回も同様です。
F#などの関数型言語には大体パターンマッチがあります。構文的にはSelect Case文に近いです。
数値や文字列だけであれば、今でもSelect Case文で問題ありません。しかし、型によるパターンマッチは今は出来ません。
下記はF#による型パターンマッチです。図形の形と長さを出力しています。
shapeの型で分岐し、更にwhenにてw=hが同じ値なら正方形、違うなら四角形としています。

type Shape =
    | Rectangle of width:int * height:int
    | Circle of radius:int

let shape = Rectangle(10,10)
match shape with
| Rectangle(w,h) when w=h -> printfn "Square %d" w
| Rectangle(w,h) -> printfn "Rectangle %d, %d" w h
| Circle(r) -> printfn "Circle %d" r

これと同じことをSelect Caseステートメントでも出来るよう拡張するのが、今回の目的です。
PublicMustInheritClass ShapeEndClassPublicClass RectangleInherits ShapePublicProperty Width AsIntegerPublicProperty Height AsIntegerEndClassPublicClass CircleInherits ShapePublicProperty Radius AsIntegerEndClassSub Main()Dim shape As Shape = New Rectangle With {.Width = 10, .Height = 10}SelectCase shapeCase r As Rectangle When r.Width = r.Height
            Console.WriteLine("Square of {0}", r.Width)Case r As Rectangle
            Console.WriteLine("Rectangle of {0},{1}", r.Width, r.Height)Case c As Circle
            Console.WriteLine("Circle of {0}", c.Radius)EndSelectEnd Sub
他にも下記のような使い方もできます。
PrivateSub Button_Click(sender AsObject, e As EventArgs) Handles OKButton.Click, CancelButton.ClickSelectCase senderCaseIs OKButton
            DialogResult = DialogResult.OK
            Close()CaseIs CancelButton
            DialogResult = DialogResult.Cancel
            Close()CaseIsNothingThrowNew NullReferenceException("sender")CaseElse
            Debug.Fail("Unknown sender.")EndSelectEnd Sub

以上です、便利そうなので実装されるといいのですが…。


Viewing all articles
Browse latest Browse all 994

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>