Author Topic: ya gotta love C#6: String Interpolation  (Read 3239 times)

0 Members and 1 Guest are viewing this topic.

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2139
  • class keyThumper<T>:ILazy<T>
ya gotta love C#6: String Interpolation
« on: January 04, 2016, 04:40:51 AM »
Really pretty
(though it is in the eye of the beholder I s'pose )

Code - C#: [Select]
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace TextRotationTester {
  5.     class Program {
  6.         static void Main(string[] args) {
  7.             var degreeList = new List<double> {0, 45, 89, 91, 180, 269, 271, 360, 405,-40};
  8.             foreach (var degree in degreeList) {
  9.                 var degradians = degree * Math.PI / 180.0;
  10.                 Console.WriteLine($"{degree,6 :F2}" +
  11.                                   $" = {degradians,9 :F6} :cos()" +
  12.                                   $" = {Math.Cos(degradians),9 :F6}");
  13.             }
  14.             Console.ReadLine();
  15.         }
  16.     }
  17. }
  18.  
« Last Edit: January 04, 2016, 04:44:56 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.

Jeff H

  • Needs a day job
  • Posts: 6150
Re: ya gotta love C#6: String Interpolation
« Reply #1 on: January 04, 2016, 11:09:02 AM »
I like it a lot!
Kinda angularish. 

JohnK

  • Administrator
  • Seagull
  • Posts: 10639
Re: ya gotta love C#6: String Interpolation
« Reply #2 on: January 12, 2016, 09:03:39 AM »
That example is very obfuscated. I guess, what I mean to say is that there is obviously so many things going on in the background it can be very confusing for people who haven't touched a low level language and almost hard to keep track of for those that have. I guess the first part that sticks out to me is the casting in the array construction -i.e. casting INT to DOUBLE (I know, allowing the implicit cast does make for easier typing and I've done it myself many times, I'm sure, as well). And the second is the use of double in the first place (Yes, I know double is the "default" but I've just been burned with floating point math several times and I just get a little jumpy whenever I see it and when I have to use it I hate it). And lastly, I don't like that C# allows you to not explicitly signify that a double is a double (double doubleValue = 0.2; is the same as double doubleValue = 0.2d;); I think forcing this explicit notation should be the default not the other way around. -i.e.
double thisIsNotReallyADoubleVar = 50 / 100;

Yes, the example makes for "pretty code" but I guess I just look at it with nervous eyes.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2139
  • class keyThumper<T>:ILazy<T>
Re: ya gotta love C#6: String Interpolation
« Reply #3 on: January 12, 2016, 06:36:06 PM »
Hi John,

I don't see it as obfuscated,

Quote
I don't like that C# allows you to not explicitly signify that a double is a double (double doubleValue = 0.2; is the same as double doubleValue = 0.2d;)

Sorry, I don't get your point. The value does not affect run-time. All the hard work is done by the compiler and the result is the same, but your suggestion required more from the programmer.


//-----
Quote
I think forcing this explicit notation should be the default not the other way around. -i.e.
double thisIsNotReallyADoubleVar = 50 / 100;

Actually, the result is 0.0 not 0.5, but it IS a double.

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.

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2139
  • class keyThumper<T>:ILazy<T>
Re: ya gotta love C#6: String Interpolation
« Reply #4 on: January 12, 2016, 06:42:00 PM »

Hi John,

The String Interpolation changes in C#6 deal with the functionality of the string formatting and concatenation.
If you compare this code with the code that would be required pre c#6 the differences are significant.

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.

JohnK

  • Administrator
  • Seagull
  • Posts: 10639
Re: ya gotta love C#6: String Interpolation
« Reply #5 on: January 12, 2016, 06:50:58 PM »
> it is a double.
That's good, at least, but it still doesn't have the correct answer.

The short answer is, I'm all for syntactic sugar but I would also like a good balance for maintenance as well. I think the explicit signification the programmer would be reminded that, "this number is really a double/decimal". ...but I get your point. Not much motivation for doing it my way.

Well, that is also good. Strings are a pain in the butt.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2139
  • class keyThumper<T>:ILazy<T>
Re: ya gotta love C#6: String Interpolation
« Reply #6 on: January 12, 2016, 07:03:49 PM »
> it is a double.
That's good, at least, but it still doesn't have the correct answer.



Yes it is correct. The compiler has done exactly what we told it to do.

On the right: divide 50 by 100. Both numbers are integers so the result is an integer rounded down.
on the left. coerce the integer result to a double to suit the declared storage type.

To get the "true" result, we would have to do something like  :
Code - C#: [Select]
  1.         double thisIsReallyADoubleVar = 50 / (double)100;
  2.         Console.WriteLine(thisIsReallyADoubleVar);
  3.  
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.

JohnK

  • Administrator
  • Seagull
  • Posts: 10639
Re: ya gotta love C#6: String Interpolation
« Reply #7 on: January 12, 2016, 07:09:06 PM »
You have to use an explicit cast?! That's just goofy. When I get access to a compiler tomorrow I'm going to play around with this. ...obviously 50d /100d works though, right?

Sorry, typing this on my phone.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2139
  • class keyThumper<T>:ILazy<T>
Re: ya gotta love C#6: String Interpolation
« Reply #8 on: January 12, 2016, 07:20:16 PM »
...obviously 50d /100d works though, right?


Yep, as does 50 / 100d   :)
and  50 / 100.0
and  50.0 / 100

//-----

https://msdn.microsoft.com/en-us/library/3b1ff23f.aspx

Quote
The division operator (/) divides its first operand by its second operand. All numeric types have predefined division operators.
Quote
When you divide two integers, the result is always an integer. For example, the result of 7 / 3 is 2. To determine the remainder of 7 / 3, use the remainder operator (%). To obtain a quotient as a rational number or fraction, give the dividend or divisor type float or type double. You can assign the type implicitly if you express the dividend or divisor as a decimal by putting a digit to the right side of the decimal point, ....
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.

JohnK

  • Administrator
  • Seagull
  • Posts: 10639
Re: ya gotta love C#6: String Interpolation
« Reply #9 on: January 12, 2016, 07:29:45 PM »
They say float and double; what about decimal?

Code - C#: [Select]
  1. decimal decimalVariable = 50 / 100;

Sorry, last question. :)
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2139
  • class keyThumper<T>:ILazy<T>
Re: ya gotta love C#6: String Interpolation
« Reply #10 on: January 12, 2016, 07:43:43 PM »

Conversions from Single or Double to Decimal throw an OverflowException if the result of the conversion is not representable as a Decimal.



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.

JohnK

  • Administrator
  • Seagull
  • Posts: 10639
Re: ya gotta love C#6: String Interpolation
« Reply #11 on: January 12, 2016, 07:46:33 PM »
See, I'm a hypocrite! I forgot the 'm'!

Thanks!
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2139
  • class keyThumper<T>:ILazy<T>
Re: ya gotta love C#6: String Interpolation
« Reply #12 on: January 12, 2016, 07:46:55 PM »
and ...
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.

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2139
  • class keyThumper<T>:ILazy<T>
Re: ya gotta love C#6: String Interpolation
« Reply #13 on: January 12, 2016, 07:49:14 PM »
I forgot the 'm'!

Thanks!

Pleased to help John
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.

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2139
  • class keyThumper<T>:ILazy<T>
Re: ya gotta love C#6: String Interpolation
« Reply #14 on: January 12, 2016, 07:51:59 PM »

and for anyone following later ..
I should have changed the variable names to be more relevant to the test being done.
... sorry about that if it is confusing.
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.