1

Possible Duplicate:
Is there something like Matlab's eval statement in Objective-C 2.0?

Is there an eval function (as in lisp, javascript, python, ruby...) in Objective-C?

What I mean by eval is a function that can take in arbitrary Objective-C code (possibly having class definitions, side effects, instrospection, etc) as a string (not a block or an NSInvocation or IMP or something like that), and evaluate each expression, respecting the current runtime environment state, side effects, class definitions, etc.

If not, is it possible to implement within the confines of the existing runtime?

Community
  • 1
  • 1
vlazzle
  • 811
  • 9
  • 14
  • http://stackoverflow.com/questions/1116885/is-there-something-like-matlabs-eval-statement-in-objective-c-2-0, http://stackoverflow.com/questions/2550897/eval-for-objective-c-code, http://www.google.com/search?q=objective-c+eval – rid Apr 14 '12 at 23:00

3 Answers3

4

Neither the language nor Apple's frameworks directly support such a thing. However, one of the goals of LLVM is to be an embeddable compiler suite. I'm pretty sure it can generate executable code right into memory. The hard part would be providing that code with access to the pre-existing environment of the calling code. For example, compiling code which references a local variable or something like that.

(Mind you, this approach is forbidden for the iOS App Store, but it could maybe be workable on Mac OS X.)

Ken Thomases
  • 88,520
  • 7
  • 116
  • 154
1

Absolutely not. Objective-C is a fully compiled language. Only interpreted languages can do that sort of thing.

Philip Sheard
  • 5,789
  • 5
  • 27
  • 42
  • 13
    As an aside, compiled/interpreted is a property of the implementation, not tied to the language. Of course, if there's effectively only one implementation, that doesn't buy you anything. –  Apr 14 '12 at 23:07
  • 2
    Breathtakingly pedantic there, delnan. Definitely deserves a +1. – Philip Sheard Apr 14 '12 at 23:19
  • 1
    Proper usage of `pedantic`, @PhilipSheard. +1 – craig Dec 10 '14 at 12:15
0

No. Code eval is the feature for dynamic language. Although objective-C has dynamic feature , and even Cocoa runtime , it is still considered as a static language (generally).

SwiftMango
  • 15,092
  • 13
  • 71
  • 136