operator .? будет, но когда-нибудь в следующей версии

by Темных Сергей 09.07.2009 18:46:00

Перевел на английский, как смог, свою хочулку про operator.? и отправил Эрику Липперту (одна из шишек в разработке .net) (блог Эрика Липперта на русском, блог Эрика Липперта(оригинальный))

Ответ был следующий:

--

This is a pretty frequently requested feature. We like it a lot, and considered doing it for C# 4.0, but it did not make the cut. We will definitely consider doing it for a hypothetical future version of the language.

 --

 

-- Запрос --

What you think about "operator .?" ?

<object>.?<member>


"operator .?" is equivalent of construction
---
NullMemberType result;
if (<object> != null)
  result = <object>.<member>;
else
  result = null;
--
where MemberType is type of <member> result
where NullMemberType is
  MemberType for case MemberType is class;
  MemberType for case MemberType is Nullable<T>;
  Nullable<MemberType> for case MemberType is struct
  void for case MemberType is void
i.e. for MemberType is void - this operator is equivalent of construction
--
if (<object> != null)
  <object>.<member>();
--


this operator is very needed in script and business logic,
and this operator is realized Null Object Pattern (http://en.wikipedia.org/wiki/Null_Object_pattern)


example of using

now
--
int? age = null;
var root = Root;
if (root != null)
{
  var config = root.Config;
  if (config != null)
  {
    var user = config.User;
    if (user != null)
       age = user.Age;
  }
}
if (age != null && age.Value >= 21) { //bla-bla }
--

whit "operator .?"
--
if (Root.?Config.?User.?Age >= 21) {//bla-bla}
--


now this operator realized is very ugly as
if (Root._no(_ => _.Config)._no(_ => _.User)._nv(_ => _.Age) >= 21) {//bla-bla}




public static class NullableExtensions
{
  public static void _n<TItem>(this TItem item, Action<TItem> func)
  {
    if (item != null)
      func(item);
  }
  public static TResult _no<TItem, TResult>(this TItem item, Func<TItem, TResult> func) where TResult : class
  {
    if (item == null)
      return null;
    return func(item);
  }
  public static TResult? _nv<TItem, TResult>(this TItem item, Func<TItem, TResult?> func) where TResult : struct
  {
    if (item == null)
      return null;
    return func(item);
  }
  public static TResult? _nv<TItem, TResult>(this TItem item, Func<TItem, TResult> func) where TResult : struct
  {
    if (item == null)
      return null;
    return func(item);
  }
}



ps
what is better? "operator.?" or "operator?."

Оценок нет

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

Похожие записи

Powered by BlogEngine.NET 1.3.1.0
Theme by Mads Kristensen

Сергей Темных

Модулятор


Calendar

<<  Август 2017  >>
повтсрчепясуво
31123456
78910111213
14151617181920
21222324252627
28293031123
45678910

View posts in large calendar

Страницы

    Последние комментарии

    Категории

    None


    Disclaimer

    The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

    © Copyright 2017

    Sign in