Author Topic: TryParse out statement.  (Read 3120 times)

0 Members and 1 Guest are viewing this topic.

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2125
  • class keyThumper<T>:ILazy<T>
TryParse out statement.
« on: January 03, 2019, 02:42:28 AM »
Was playing with TryParse this evening and my IDE told me that the out variable could be declared in-line and didn't require a 'standard' declaration.

"That's cheeky" was my first thought.
"That could lead to confusion" was my second.

At what stage should the 'nanny' mentality take over ? ? ?

Code - C#: [Select]
  1.     class Program
  2.     {
  3.         static void Main(string[] args)
  4.         {
  5.             Console.WriteLine("Input first Integer : ");
  6.             string input = Console.ReadLine();
  7.  
  8.             //int convertedInputToNumber; // not needed when declared in-line in tryParse
  9.             if (!int.TryParse(input, out int convertedInputToNumber)) {
  10.                 Console.WriteLine("Oooops");
  11.             }
  12.             else {
  13.                 Console.WriteLine($"Converted '{input}' to {convertedInputToNumber}.");
  14.                 Console.WriteLine(42 + convertedInputToNumber);
  15.             }
  16.         }
  17.     }
  18.  
  19.  

I'll get out the popcorn.

Regards,
Called Kerry in my other life
Retired; but they dragged me back in !

I live at UTC + 13.00

---
some people complain about loading the dishwasher.
Sometimes the question is more important than the answer.

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: TryParse out statement.
« Reply #1 on: January 03, 2019, 03:18:10 AM »
It came with C# 7.
From my part, i like it.
Speaking English as a French Frog

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2125
  • class keyThumper<T>:ILazy<T>
Re: TryParse out statement.
« Reply #2 on: January 03, 2019, 04:32:28 AM »
I also like it Gilles.
« Last Edit: January 03, 2019, 04:49:09 AM by kdub »
Called Kerry in my other life
Retired; but they dragged me back in !

I live at UTC + 13.00

---
some people complain about loading the dishwasher.
Sometimes the question is more important than the answer.

MickD

  • King Gator
  • Posts: 3619
  • (x-in)->[process]->(y-out) ... simples!
Re: TryParse out statement.
« Reply #3 on: January 03, 2019, 04:53:08 AM »
From Gile's link:
Quote
When using the out variable declaration, the declared variable "leaks" into the outer scope of the if statement. This allows you to use the variable afterwards

Personally, and as you say Kerry, I think this could lead to confusion unless you keep/kept up to speed on every new language feature.
It looks ok when used directly within the if construct like that but if it leaks much further... :?
More explicit and clear code only helps when trying to find and fix a bug and I don't see a real need for such a feature. I'd rather see more things like good type inference, bundled types and the like if you want to save key strokes :)

Things like:
Code - C#: [Select]
  1. public int (
  2.     x, y = 0, 0;
  3.     width, height = 600, 400;
  4. )
  5.  
"Short cuts make long delays,' argued Pippin.”
J.R.R. Tolkien

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2125
  • class keyThumper<T>:ILazy<T>
Re: TryParse out statement.
« Reply #4 on: January 03, 2019, 06:23:43 AM »

Hi Mick,

It would take me a bit to get used to that sort if declaration/initialisation.
I can envision the lengths some could take it too. :)  ... besides, how many of us have the mental exactitude to coordinate much more than quadruple element sets (without the temptation to re-check them each time we saw them).
 ... though it does look sort of familiar.

Re the inline TryParse variable declaration ;
I'll check tomorrow, but I'd be surprised if the wandering went too much out of it's scope block ... we'll see.

Called Kerry in my other life
Retired; but they dragged me back in !

I live at UTC + 13.00

---
some people complain about loading the dishwasher.
Sometimes the question is more important than the answer.

MickD

  • King Gator
  • Posts: 3619
  • (x-in)->[process]->(y-out) ... simples!
Re: TryParse out statement.
« Reply #5 on: January 03, 2019, 03:28:58 PM »
It's more about less noise than the initialisation/declaration sets, typing keywords like using/public/private/type etc on every line can be tedious and could be made redundant.
You don't have to use it everywhere, just where it makes sense like listing class variables etc.

I probably should of used curly braces instead of brackets too :)

Code - C#: [Select]
  1. // types infered
  2. public static const {
  3.     PI                   = 3.1459;
  4.     MAX_POWER  = 100;
  5.     MIN_WIDTH   = 450;
  6.     TITLE              = "My App"
  7. }
  8.  

cheers.
"Short cuts make long delays,' argued Pippin.”
J.R.R. Tolkien

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2125
  • class keyThumper<T>:ILazy<T>
Re: TryParse out statement.
« Reply #6 on: January 03, 2019, 03:57:22 PM »
Yes, that makes sence now.

We'll call it Type Bundling

A nice bit of "low calorie syntactic sugar"

Would probably require some sort of attributing so the compiler could understand the statement ...
Probably a bit late to get it into C#8 though :(

Code - C#: [Select]
  1. [TypeBundle
  2.    public static const {
  3.        PI         = 3.1459;
  4.        MAX_POWER  = 100;
  5.        MIN_WIDTH  = 450;
  6.        TITLE      = "My App"
  7.    }
  8. ]
  9.  

That's probably worth a submission Mick !

Each variable would require an initialization value so the compiler could determine it's type.
« Last Edit: January 03, 2019, 04:05:47 PM by kdub »
Called Kerry in my other life
Retired; but they dragged me back in !

I live at UTC + 13.00

---
some people complain about loading the dishwasher.
Sometimes the question is more important than the answer.

MickD

  • King Gator
  • Posts: 3619
  • (x-in)->[process]->(y-out) ... simples!
Re: TryParse out statement.
« Reply #7 on: January 03, 2019, 05:07:36 PM »
The compiler should be able to work it out from the grammer.

Yes, initialising is key, this is how F#, Go and other lang's with type inference do it.

Here's a sample of Go code, it adds the type after the variable name as in the size.Event type below:
Code: [Select]
import (
"fmt"
"image"
"image/color"
"log"
"time"

"golang.org/x/mobile/event/lifecycle"

"golang.org/x/mobile/event/mouse"

"golang.org/x/mobile/event/key"
"golang.org/x/mobile/event/size"

"golang.org/x/exp/shiny/driver"
"golang.org/x/exp/shiny/screen"
)

var (
// set up some global helper var's
winWidth, winHeight = 800, 650

// We can get info from the event.Size() function along with other
// helpful functions and data.
sizeEvent size.Event
)

Note that by convention in Go, these variables (or functions) are only global in the scope of the module (this file), variables and functions starting with capital letters are public to everywhere.
'By Convention' sounds a bit strict but the convention is sound and all code looks the same as most editors use the gofmt and go import tools on save which automatically formats your code and brings in or deletes unused imports, very handy.
VS is getting better at this though and format on save would be nice....maybe it can be setup with an editor event??


EDIT (John): Getting forum errors from GiShi so I disabled the GisHi "go" code tag.
« Last Edit: January 04, 2019, 02:49:27 PM by John Kaul (Se7en) »
"Short cuts make long delays,' argued Pippin.”
J.R.R. Tolkien

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2125
  • class keyThumper<T>:ILazy<T>
Re: TryParse out statement.
« Reply #8 on: January 03, 2019, 09:34:07 PM »


>>> VS is getting better at this though and format on save would be nice....maybe it can be setup with an editor event??

There are a couple of add-ons that do clean-up on save .. CodeMaid is one.
Called Kerry in my other life
Retired; but they dragged me back in !

I live at UTC + 13.00

---
some people complain about loading the dishwasher.
Sometimes the question is more important than the answer.

Grrr1337

  • Swamp Rat
  • Posts: 812
Re: TryParse out statement.
« Reply #9 on: January 04, 2019, 07:00:33 PM »
Mentioning 'clear code', oh I wish there was with statement in C#..

So blocks like:
Code - C#: [Select]
  1. MyForm.Text = "This is My Form";
  2. MyForm.ShowIcon = false;
  3. MyForm.MinimumSize = new Size(600, 300);
  4. MyForm.StartPosition = FormStartPosition.CenterScreen;

became something like:
Code - C#: [Select]
  1. With MyForm
  2. {
  3.   .Text = "This is My Form";
  4.   .ShowIcon = false;
  5.   .MinimumSize = new Size(600, 300);
  6.   .StartPosition = FormStartPosition.CenterScreen;
  7. }
(apply ''((a b c)(a b c))
  '(
    (( f L ) (apply 'strcat (f L)))
    (( L ) (if L (cons (chr (car L)) (f (cdr L)))))
    (72 101 108 108 111 32 87 111 114 108 100)
  )
)
vevo.bg