foreach type unsafe

G

Guest

/* foreach does implicit type conversion on elements of a params argument or
Generic.List.
* This is not good.
* Examples of evil follow...
*/

using System;

// I love it when C# is strict with me...
using System.Collections.Generic;
// ...prettier than C++ templates, not slutty like ArrayList.

namespace DoubleUtilities
{
public static class MaxDouble
{
public static double Max(params double[] list)
{
if (list.Length == 0)
return 0.0D;
double m = list[0];
for (int i = 1; i < list.Length; i++)
{
// Taste the whip of love not given lightly...
//int d = list; //Type error reported (double to int
conversion).
double d = list; // The correct type declaration.

if (d > m)
m = d;
}
return m;
}

public static double MadMax(params double[] list)
{
if (list.Length == 0)
return 0.0D;
double m = list[0];
// Her FOG, her amphetamine, and her pearls...
// And she BREAKS just like a LITTLE GIRL
foreach (int d in list) //Type error unreported (double to int
conversion).
if (d > m)
m = d;
return m;
}


//__________________________________________________________________________
public static double Max(List<double> list)
{
if (list.Count == 0)
return 0.0D;
double m = list[0];
for (int i = 1; i < list.Count; i++)
{
// Taste the whip of love not given lightly...
//int d = list; //Type error reported (double to int
conversion).
double d = list; // The correct type declaration.

if (d > m)
m = d;
}
return m;
}

public static double MadMax(List<double> list)
{
if (list.Count == 0)
return 0.0D;
double m = list[0];

// Her FOG, her amphetamine, and her pearls...
// And she BREAKS just like a LITTLE GIRL
foreach (int d in list) //Type error unreported (double to int
conversion).
if (d > m)
m = d;
return m;
}
//___________________________________________________________________
public static void TestCases()
{
double expected_max = 8.345;
double[] array = { -3.6, 1.1, -2.2, 3.4, 6.6, expected_max, 7.2 };
TestMaxes(array, expected_max);
}

private static void TestMaxes( double[] parameters, double
expected_max )
{
/* Output
parameters={-3.6, 1.1, -2.2, 3.4, 6.6, 8.345, 7.2}
Max( parms double[] list): max=8.345, is_ok=True
Max( List<double> list): max=8.345, is_ok=True
MadMax( parms double[] list): max=8, is_ok=False
MadMax( List<double> list): max=8, is_ok=False
*/
string delimiter = String.Empty;
Console.Write("parameters={");
for (int i = 0; i < parameters.Length; i++)
{
Console.Write("{0}{1}",delimiter, parameters.ToString());
delimiter = ", ";
}
Console.WriteLine("}");

double max = Max(parameters);
bool is_ok = Math.Abs(max - expected_max) < 1E-12D; //paranoid
double comparison
Console.WriteLine("Max( parms double[] list): max={0},
is_ok={1}", max, is_ok);

List<double> list = new List<double>(parameters);
max = Max(list);
is_ok = Math.Abs(max - expected_max) < 1E-12D;
Console.WriteLine("Max( List<double> list): max={0}, is_ok={1}",
max, is_ok);

max = MadMax(parameters);
is_ok = Math.Abs(max - expected_max) < 1E-12D;
Console.WriteLine("MadMax( parms double[] list): max={0},
is_ok={1}", max, is_ok);

max = MadMax(list);
is_ok = Math.Abs(max - expected_max) < 1E-12D;
Console.WriteLine("MadMax( List<double> list): max={0},
is_ok={1}", max, is_ok);
}
}
}
 
M

Michael C

"(e-mail address removed)"
/* foreach does implicit type conversion on elements of a params argument
or
Generic.List.
* This is not good.
* Examples of evil follow...
*/

That's just the way it is, not much you can do about it except not use for
each.

Michael
 
G

Guest

Michael C said:
That's just the way it is, not much you can do about it except not use for
each.

That's a pity: foreach has a nice uncluttered syntax.
I guess it is that way for historical reasons - I'd imagine it was those old
untyped collection classes that influenced the semantics of foreach.

You don't suppose that discussions in this group about holes in the type
system might influence the compiler writers to fix those holes, or at least
issue a warning?
Or is that too Pollyanna-ish of me?
 
D

Daniel O'Connell [C# MVP]

Paul Connolly said:
That's a pity: foreach has a nice uncluttered syntax.
I guess it is that way for historical reasons - I'd imagine it was those
old
untyped collection classes that influenced the semantics of foreach.

You don't suppose that discussions in this group about holes in the type
system might influence the compiler writers to fix those holes, or at
least
issue a warning?
Or is that too Pollyanna-ish of me?

It is unlikely, I don't know that anyone even bothers monitoring this group.
But I do agree a warning should be there in this case.

Have you looked to see if there is a suggestion at the product feedback
center yet?
http://lab.msdn.microsoft.com/productfeedback/default.aspx
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top