Entries:
Comments:
Posts:

Loading User Information from Channel 9

Something went wrong getting user information from Channel 9

Latest Achievement:

Loading User Information from MSDN

Something went wrong getting user information from MSDN

Visual Studio Achievements

Latest Achievement:

Loading Visual Studio Achievements

Something went wrong getting the Visual Studio Achievements

Discussions

Bent Rasmussen exoteric stuck in a loop, for a while
  • Extensia

    More extensions, refined a bit

     

    public static T? Eval<T>(this object x) where T : struct { T? z = null; if (x != null) { var ct = TypeDescriptor.GetConverter(typeof(T)); if (ct.CanConvertFrom(x.GetType())) { try { z = (T)ct.ConvertFrom(x); } catch (NotSupportedException)
     { } } } return z; } public static string Value(this XElement node) { return node == null ? null : node.Value; } public static string Value(this XAttribute node) { return node == null ? null : node.Value; } public static string Value(this XElement node, XName
     name) { return node == null ? null : node.Element(name).Value(); } public static T? Eval<T>(this XElement node, XName name) where T : struct { return node.Value(name).Eval<T>(); } 

     

    Allows this form of expression

     

    return from d in result.Element("reply").Element("order-list").Descendants("order-detail") select new Order() { Confirmed = d.Eval<bool>("confirmed"), ExecutionDate = d.Eval<DateTime>("execution-date"), ExecutionTimeFrom = d.Eval<DateTime>("execution-time-from"),
     ExecutionTimeTo = d.Eval<DateTime>("execution-time-to"), //... }; 

  • Interpreting XML

    Well, the XML processor is the metaparser, but I have come to love using XLinq. It's the easiest way to deal with XML. In combination with the XPath extensions.

     

    Still, I'd be interested at looking at the XMPP schema and using a class generator tool to see the output. I don't know how these tools handle XML namespaces but I assume it's via CLR namespaces and attributes. An empty element (or the lack of it) would be the perfect match for a bool.

  • The Radio

    Jacques Loussier - Pastorale in C minor

     

    Antoni Tolmos - Dins Un Núvol

     

    Antoni Tolmos - Pensament

     

    & UACK

     

     

  • Extensia

    TommyCarlier said:
    Sven Groot said:
    *snip*

    I have a suggestion to improve your code: split EnumerateLines in a public method that validates the argument and a private method that implements the functionality. With your code, the entire method is lazily invoked and the argument validation code will not be called when EnumerateLines is invoked, but when the first MoveNext is called. When you split it, the arguments are validated immediately.

    public static class TextReaderExtensions
    {
      public static IEnumerable<string> EnumerateLines(this TextReader reader)
      {
        if( reader == null )
          throw new ArgumentNullException("reader");
    
        return EnumerateLinesCore(reader);
      }
    
      private static IEnumerable<string> EnumerateLinesCore(TextReader reader)
      {
        string line;
        while( (line = reader.ReadLine()) != null )
        {
          yield return line;
        }
      }
    }

    EDIT: struggling with the code snippet formatting.

    Exceptions with lazy evaluation really sucks, doesn't it. It seems to me that in the above case one could theoretically get into the situation where the compiler implementation shifted strategy and made the whole thing lazy. Then you have a dependency on a fixed compiler implementation which determines whether your code is correct or not. Not good at all.

  • Extensia

    TommyCarlier said:

    Here's a simple one I occasionally use (to avoid having to import System.Globalization each time I need CultureInfo.InvariantCulture):

    public static string FormatInvariant(this string format, params object[] args)
    {
      if (format == null) throw new ArgumentNullException("format");
      return string.Format(CultureInfo.InvariantCulture, format, args);
    }

    Used like this:

    s = "Date {0:d}, Time {0:t}".FormatInvariant(DateTime.Now);

    Sure you don't want it the other way around?

     

    myDateTime.FormatInvariant(myFormatString)

  • Extensia

    Charles said:

    The idea of a community driven source of useful extension methods would itself be useful. I'm assuming that's the point of this thread? How would this work, practically?

     

    C

    Actually the modest idea was to just collect it in here, but that's of course very disorganized.

     

    Your assumption is actually an interesting idea. Imagine ambient intellisense for extension methods out there - perhaps in a repository on codeplex.

     

    So you dot  (plus something) on a Control and find yourself with a list of free ambient extension methods. If you use the extension method, Visual Studio will transparently import the implementation code. The list will be prettified by WPF to allow documentation expansion as well as implementation expansion.

     

    Of course on something like String or Object, the number of community methods could become extreme. You'd probably also want a rating and commenting system for these.

     

    I hope BCL is gradually refactored to separate out methods as extension methods when they really don't need to be part of the implementation. This can also provide null-safety.

     

    And actually the BCL team can actually use ambient intellisense to be inspired here (1 cent for every use of this possible marketing term for me please heh)

     

    And of course ambient intellisense in Visual Studio will help prevent duplication of work because people will first try ambient intellisense and then implement a solution (and possibly directly inside Visual Studio submit to the community).

     

    One might also think about extension methods with dependencies on other extension methods. Often one good thing does not come alone and so you want a means to express that if you say A you also need to say B.

  • Extensia

    ido.ran said:

    Hi,

    I've been trying to use this technic but it's not all gold.

    The problem is that some actions compose of calls which some needs to be in a background thread yet some need to be on the UI thread. How do you think of solving this problem?

    Actually, still waiting for Reactive LINQ, then all this is probably obsolete.

  • Extensia

    ido.ran said:

    Hi,

    I've been trying to use this technic but it's not all gold.

    The problem is that some actions compose of calls which some needs to be in a background thread yet some need to be on the UI thread. How do you think of solving this problem?

    In actual fact it's a "hack". It does not support things like progress indication. This is why I want to look at possible monadic solutions for better composability. The technique shown is very coarse-grained, but you can still synchronize with the UI in the normal way if you need to, e.g.

     public static void Do(this Control control, Action action) { if (control.InvokeRequired) control.Invoke(action); else action(); } 

    But I'm working on composability atm.

  • Extensia

    How about a place to collect nice and useful extension methods (and possibly argue their utility etc.)

    I'm not going to show the definition(s) but here's how to use it

     eventsButton.Enabled = false; this.TryCatchFinally ( () => OrderActions.RequestEvents(session, max), xs => { DatabindEvents(xs); }, ex => MessageBox.Show(ex.Message, "Error"), () => eventsButton.Enabled = true ); 

    This approach is lacking in some departments but it looks safe and is exceptionally easy to use. It takes care of asynchronous computation and continuation on the UI thread, as well as handling exceptions on the UI thread as well.  Sure this can be monadified into further blissfullness.

     

  • A type-​inference typo

    Sven Groot said:
    exoteric said:
    *snip*

    I guess for the moment they just decided to only allow type inference if the type on the right-hand side of the expression is totally unambiguous, which just isn't the case for delegates.

    I get your sentiment, it's just an itch I'll have to live with.