“оценка” в Scala

Также имеется атрибут ThreadStaticAttribute, который делает статическое поле уникальным для каждого потока, поэтому вы можете иметь строго типизированное локальное хранилище для потоков.

Даже если методы расширения не такие уж секретные (LINQ основан на них), может быть не так очевидно, насколько они полезны и более читабельны для вспомогательных методов:

//for adding multiple elements to a collection that doesn't have AddRange
//e.g., collection.Add(item1, item2, itemN);
static void Add<T>(this ICollection<T> coll, params T[] items)
 { foreach (var item in items) coll.Add(item);
 }

//like string.Format() but with custom string representation of arguments
//e.g., "{0} {1} {2}".Format<Custom>(c=>c.Name,"string",new object(),new Custom())
//      result: "string {System.Object} Custom1Name"
static string Format<T>(this string format, Func<T,object> select, params object[] args)
 { for(int i=0; i < args.Length; ++i)
    { var x = args[i] as T;
      if (x != null) args[i] = select(x);
    }
   return string.Format(format, args);
 }
56
задан Thilo 26 July 2009 в 03:23
поделиться

2 ответа

Scala is not a scripting language. It may look somewhat like a scripting language, and people may advocate it for that purpose, but it doesn't really fit well within the JSR 223 scripting framework (which is oriented toward dynamically typed languages). To answer your original question, Scala does not have an eval function just like Java does not have an eval. Such a function wouldn't really make sense for either of these languages given their intrinsically static nature.

My advice: rethink your code so that you don't need eval (you rarely do, even in languages which have it, like Ruby). Alternatively, maybe you don't want to be using Scala at all for this part of your application. If you really need eval, try using JRuby. JRuby, Scala and Java mesh very nicely together. It's quite easy to have part of your system in Java, part in Scala and another part (the bit which requires eval) in Ruby.

50
ответ дан 26 November 2019 в 17:05
поделиться

Вы всегда можете использовать scalac для компиляции класса scala и затем динамической загрузки этого класса. Но я думаю, это не то, что вам нужно.

2
ответ дан 26 November 2019 в 17:05
поделиться
Другие вопросы по тегам:

Похожие вопросы: