dotnet / dotnet/vblang

[Proposal] Default value expression in VB

Open
#272 13 comments 0 reactions 0 assignees View on GitHub
Discussion LDM Reviewed: No plans
Dominant language
No language data
Stars
328
Forks
71
PR merge metrics
No merged PRs in 30d

Description

There is a [default value expression](https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/statements-expressions-operators/default-value-expressions) feature in ``C#`` language to produce default value for corresponding type. VB language is also have a ``Default`` keyword but it is working for the indexer property.

For the optional parameter in a function that its type is non-primitive, we can not declare it as a constant for set a default value. We usualy set its default value to ``Nothing`` and then using an ``If()`` expression or ``If ... Then`` statement to set the default value. But this have some drawbacks,

As @AnthonyDGreen comment that:

> It breaks the flow of typing/thinking. It can also create a situation where a lot of ceremony starts accruing on the left like multiple CType or Await keywords very far from what they relate to.

Using the ``If()`` expression actually can caused such problem: break our thinking while we programming. The ``Fluent design`` **language feature like ``Extension`` method can reduce or eliminate the backtracking.** There are proposals that in a common theme about the Fluent design in VB language:

+ [Proposal: Let's fix casting, unboxing and converting in VB with "As Type"](https://github.com/dotnet/vblang/issues/59)
+ [Reduce the need for backtracking while typing](https://github.com/dotnet/vblang/issues/154)
+ [Method cascading (like Dart) -- enables Fluent interfaces without any extra effort](https://github.com/dotnet/vblang/issues/245)

Proposal in this issue using default value expression for solving:

+ Optional parameter default value for non-primitive type that can not declare as constant
+ Fluent design language feature for Improvement of programming efficiency using VisualBasic

One of the VisualBasic language feature that people can distinguish VisualBasic with other programming language is that VB language is English words based (C family language like ``C#`` is symbols based). As one of the philosophy of VB language design is to design a programming more close to natural language for those beginers in programming. So proposal this new VB default value expression in syntax like: ``Or ... As Default If ...``.

The default value expression is a very common scenario, example as this code example list showns:

+ [``With App.GetVariable(NameOf(R_HOME)) Or R_HOME``](https://github.com/xieguigang/GCModeller/blame/75f7f39bea806c9cc925e1e179073aa5610de082/src/R.Bioconductor/RDotNET.Extensions.VisualBasic/Server/RInit.vb#L64)
+ [``Return Me.SaveTo(path Or _filePath)``](https://github.com/xieguigang/sciBASIC/blame/63df376d0ed8024dce93fbee41d93673a87d0c08/mime/application%25vnd.openxmlformats-officedocument.spreadsheetml.sheet/Excel/Model/File.vb#L176)
+ [``Dim out$ = (args <= "/out") Or ([in].TrimSuffix & "_" & name & ".vb").AsDefault``](https://github.com/xieguigang/sciBASIC/blame/63df376d0ed8024dce93fbee41d93673a87d0c08/vs_solutions/dev/vbproj/Program.vb#L118)
+ [``With dev Or Console.Out.AsDefault``](https://github.com/xieguigang/sciBASIC/blame/63df376d0ed8024dce93fbee41d93673a87d0c08/Microsoft.VisualBasic.Core/ApplicationServices/Terminal/PrintAsTable.vb#L197)
+ [``csv = Xlsx.Open(.ByRef).GetTable(sheetName:=args("/sheet") Or "Sheet1")``](https://github.com/xieguigang/sciBASIC/blame/63df376d0ed8024dce93fbee41d93673a87d0c08/mime/application%25vnd.openxmlformats-officedocument.spreadsheetml.sheet/Excel.CLI/CLI.vb#L188)
+ [``Dim mzGroup = data.GroupBy(Function(d) d.mz, equals:=AddressOf (tolerance Or ppm50).Assert)``](https://github.com/xieguigang/MassSpectrum-toolkits/blame/9384c85698bc8e2984b2c76b3e8d4af5c361defd/Assembly/assembly/MarkupData/MS1.vb#L23)
+ [``Dim css$ = (schema Or Schema.VisualStudioDefault).CSSStyle``](https://github.com/xieguigang/xDoc/blob/master/vbcode-highlight/HTMLRender.vb#L206)
+ [``Call fs.WriteAllText(path, text Or EmptyString, append, encoding Or UTF8)``](https://github.com/xieguigang/sciBASIC/blame/a53d92e973d44f83ca5b34c73b802da8c14c0d33/Microsoft.VisualBasic.Core/Extensions/Doc/Text.vb#L316)

And more example is not listed.

## Proposal ``Or ... As Default``

### Syntax

```vbnet
' Left: ValueExpression can be variable or function calls expression
' Right: The default value that will be used if the left is assert failure
' Optional If assert: assert left is equals to C# ``default(type)`` or user custom assert expression
ValueExpression Or default_value [As Default] [If assert = True]
```

### Improvement
+ Closer to the English grammar
+ Reduce the need for backtracking while typing
+ Elegant than ``If`` expression

### Drawbacks
People may be confused this feature with the ``Or`` logical operator, but this can be eliminated by using ``()`` bracket pair.

#### Default assert

The ``If`` expression can be ommited if using ``default(type)`` mechanism or using user custom expression.

#### Using ``default(type)`` assert

The If expression assert can be ommited in this ``Or ... As Default`` expression, If the assert is a ``C#`` ``default(type)`` assertion:

```vbnet
' Will changes
With If(dev Is Nothing, Console.Out, dev)
' to more natural language style
With dev Or Console.Out As Default ' [If dev Is Nothing] assert can be ommit

' Will changes
Dim css$ = If(schema Is Nothing, Schema.VisualStudioDefault, schema).CSSStyle
' to more natural language style
Dim css$ = (schema Or Schema.VisualStudioDefault).CSSStyle ' [If schema Is Nothing]

' Will changes
Dim name$ = args("/sheet")
csv = Xlsx.Open(.ByRef).GetTable(sheetName:=If(name = "", "Sheet1", name))
' to more natural language style
csv = Xlsx.Open(.ByRef).GetTable(sheetName:=args("/sheet") Or "Sheet1") ' As Default [If String.IsNullOrEmpty(anonymous_variable)]
```

#### User custom ``If assert`` for the default value

[Fibonacci numbers example](https://en.wikipedia.org/wiki/Functional_programming):

```vbnet
' Will change this long lambda
Dim fib As Func(Of Integer, Integer) = Function(x) If(x <= 1, x, fib(x - 1) + fib(x - 2))
' to more natural language style for describ an algorithm

' We can apply this default value feature to make this math lambda
' more closer to natural language in VisualBasic
'
' using user custom assert: If x <= 1
Dim fib(~x) = (fib(x - 1) + fib(x - 2)) Or x As Default If x <= 1
```

Factorial example:

```vbnet
' Will change this long lambda
Dim fac As Func(Of Integer, Integer) = Function(x) If(x <= 0, 1, fib(x - 1) * x)
' to more natural language style for describ an algorithm

' We can apply this default value feature to make this math lambda
' more closer to natural language in VisualBasic
'
' using user custom assert: If x <= 0
Dim fac(~x) = x * fac(x - 1) Or 1 As Default If x <= 0
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.