visual basic14 の話

17
可可 Visual Basic14 のの

Upload: kazuki-kachi

Post on 07-Apr-2017

576 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Visual basic14 の話

  可知 一輝

Visual Basic14の話

Page 2: Visual basic14 の話

自己紹介• ひとりでやってます(会社員ではないという意味で)• 本名でやってます。• Facebook : Kazuki.Kachi• Twitter : @kazuki_kachi

Page 3: Visual basic14 の話

何故に今 VB なのか?• VS2015 と Windows10 が RTM になって早1ヶ月、ですが、 C#6.0なお話は VS2015 が CTP な頃からさんざん聞いて聞き飽きてるし、すでに書き飽きている物と想像しています。                      まあ、だったら VB かな?と(軽い気持ち)• VB が終わったとか言わせないぞ!とかではありません。• でも、そんなに悪い言語ではないと思うので忘れないで!• 今回は、「広く」「浅く」追加された機能を紹介する予定です。 ※深く知りたい場合は「 C# 」でググれば良いんじゃないかな…

Page 4: Visual basic14 の話

と思ったんですが、

ねこが居なくなって、やる気が…

昨日帰ってきました

Page 5: Visual basic14 の話

アジェンダ• 追加された機能• 実は元々有った機能• まとめ

ゴール• VB も(ちゃんと使えば)悪くないと思っていただければ良いかなと。

Page 6: Visual basic14 の話

.NET Compiler Platform(Roslyn)( 詳細は他の人に任せるとして ) 雑な解説• コンパイラを Managed 言語( C# は C# 、 VB は VB )で実装し直した。• IDE のコード分析にも同じものを使用する ( できる ) ようにした。• コード分析 API を公開した。 ( 誰でも ( 割と簡単に ) 使える )

結果…

Page 7: Visual basic14 の話

IDEの機能強化• リファクタリングが提供された。• ウォッチウインドウが Λ 式、 LINQ の結果表示に対応した。• 以下の機能が C# と同等になった• (IDE が提供する ) リファクタリングが強化された。• ソリューションエクスプローラに「参照」dllが表示されるようになった。 これは2013のどこかの Update からかも (2013Update5 では表示されている )• SharedProject に対応した。

Page 8: Visual basic14 の話

Feature Example C# VBAuto-property initializers public int X { get; set; } = x; Added ExistsRead-only auto-properties public int Y { get; } = y; Added AddedCtor assignment to getter-only autoprops Y = 15 Added AddedStatic imports using static System.Console; … Write(4); Added ExistsIndex initializer new JObject { ["x"] = 3 } Added No

Await in catch/finally try … catch { await … } finally { await … } Added No

Exception filters catch(E e) when (e.Count > 5) { … } Added ExistsPartial modules Partial Module M1 N/A AddedPartial interfaces Partial Interface I1 Exists AddedMultiline string literals "Hello<newline>World" Exists AddedYear-first date literals Dim d = #2014-04-03# N/A Added

Comments after implicit line continuation Dim addrs = From c in Customers ' comment N/A Added

TypeOf ... IsNot ... If TypeOf x IsNot Customer Then … N/A Added

Expression-bodied members public double Dist => Sqrt(X * X + Y * Y); Added No

Null-conditional operators customer?.Orders?[5] Added AddedString interpolation $"{p.Name} is {p.Age} years old." Added Addednameof operator string s = nameof(Console.Write); Added Added#pragma #Disable Warning BC40008 Added AddedSmart name resolution   N/A AddedRead-write props can implement read-only interface properties   Exists Added

#Region inside methods   Exists AddedOverloads inferred from Overrides   N/A AddedCObj in attributes   Exists AddedCRef and parameter name   Exists AddedExtension Add in collection initializers   Added ExistsImproved overload resolution   Added N/A

(地味な )機能が追加されました。

この中で、 VBに関係があるのは…

Page 9: Visual basic14 の話

新しくできるようになった事Feature ExampleRead-only auto-properties public int Y { get; } = y;Ctor assignment to getter-only autoprops Y = 15;

Null-conditional operators customer?.Orders?[5]String interpolation $"{p.Name} is {p.Age} years old."nameof operator string s = nameof(Console.Write);#pragma #pragma warning disable

Partial interfaces Partial Interface I1

Multiline string literals "Hello<newline>World"

Read-write props can implement read-only interface properties  #Region inside methods  CObj in attributes  

Partial modules Partial Module M1Year-first date literals Dim d = #2014-04-03#Comments after implicit line continuation

Dim addrs = From c in Customers ' comment

TypeOf ... IsNot ... If TypeOf x IsNot Customer Then …Smart name resolution  Overloads inferred from Overrides  

C#では以前からできた事

こんな感じ

Page 10: Visual basic14 の話

Read-only auto-propertiesCtor assignment to getter-only autoprops

Class Point ReadOnly Property X As Integer ReadOnly Property Y As Integer ReadOnly Property Name As String = NameOf(Point) Sub New(x As Integer, y As Integer) Me.X = x Me.Y = y End SubEnd Class

Page 11: Visual basic14 の話

Class Point Private _x As Integer ReadOnly Property X As Integer Get Return _x End Get End Property Private _name As Integer ReadOnly Property Name As String Get Return _x End Get End Property Sub New(x As Integer, y As Integer) _name = “Point” _x = x End SubEnd Class

コンパイル時には上記のように解釈されます。 セッションでは ReadOnlyな Privateフィールドに格納されると言ったな? あれは嘘だ! (と言うか、 C#は確かに readonlyなフィールドに格納されるんだけど… ) まさか (こんなことが )違うとは思わず、確認を怠っておりました。申し訳ない…

Page 12: Visual basic14 の話

Null-conditional operators

Private Sub IntroduceNullConditionalOperators( ps As IReadOnlyList(Of Point), Optional act As Action = Nothing) Dim p = ps?(0) ' これは Indexer Dim s = p?.ToString() WriteLine(If(s, "Null")) Dim x = p?.X WriteLine(If(x.HasValue, x.ToString(), "Null")) act?.Invoke()   'delegate の場合は Invoke() を使用するEnd Sub

Page 13: Visual basic14 の話

String interpolation

Private Sub IntroduceStringInterpolation() Dim formated = $“{1000:C}” ‘String.Format に展開される WriteLine(formated) ‘( 日本語環境での ) 実行結果は \1,000 With New Object() Dim format1 As IFormattable = $"{1000:C}“ Dim ci = CultureInfo.GetCultureInfo("en-us") WriteLine(format1.ToString(Nothing, ci)) ‘ 実行結果は$1,000.00

End WithEnd Sub

Page 14: Visual basic14 の話

nameof operator

Private Sub IntroduceNameOf(Optional arg As String = Nothing) If arg Is Nothing Then Throw New ArgumentNullException(NameOf(arg)) End If WriteLine($"{NameOf(arg)}:{arg}") WriteLine(" おまけ ") WriteLine($"{NameOf(DateTime.Now)}") WriteLine($"{NameOf(DateTime)}") WriteLine($"{NameOf(Date.Today)}") 'WriteLine($"{NameOf(Date)}") これは Build errorEnd Sub

Page 15: Visual basic14 の話

他の実例を挙げると、INotifyPropertyChangedの実装でしょうか

今までProperty FamilyName As String          (略) Set(value As String) If _familyName = value Then Return _familyName = value RaisePropertyChanged() RaisePropertyChanged("FullName") End SetEnd Property他にはExpression使ったり…

これからProperty FamilyName As String          (略) Set(value As String) If _familyName = value Then Return _familyName = value RaisePropertyChanged() RaisePropertyChanged(NameOf(FullName)) End SetEnd Property

こんなの書きまして、Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChangedSub RaisePropertyChanged(<CallerMemberName> Optional propertyName As String = Nothing) RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))End Sub

Page 16: Visual basic14 の話

Multiline string literals

Console.WriteLine("HelloWorld")

Year-first date literals

Dim today = #2015-08-29# ' 年は 1000 以上でないと Build errorDim todayOnOldVersion = #08-29-2015# ' 年は 100 以上でないと Build error

Page 17: Visual basic14 の話

以上です。