www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Error: undefined identifier 'selector'

reply Mike McKee <volomike gmail.com> writes:
When I run this piece of code:

// FROM: https://dlang.org/spec/objc_interface.html
module main;

extern (Objective-C)
interface Class
{
   NSString alloc()  selector("alloc");
}

extern (Objective-C)
interface NSString
{
   NSString initWithUTF8String(in char* str) 
 selector("initWithUTF8String:");
   void release()  selector("release");
}

extern (C) void NSLog(NSString, ...);
extern (C) Class objc_lookUpClass(in char* name);

void main()
{
   auto cls = objc_lookUpClass("NSString");
   auto str = cls.alloc().initWithUTF8String("Hello World!");
   NSLog(str);
   str.release();
}

I get this error:

$ dmd -L-framework -LFoundation test.d
test.d(7): Error: undefined identifier 'selector'
test.d(13): Error: undefined identifier 'selector'
test.d(14): Error: undefined identifier 'selector'

It just doesn't like that  selector statement. What's the catch?
Dec 14 2015
parent reply tcak <1ltkrs+3wyh1ow7kzn1k sharklasers.com> writes:
On Monday, 14 December 2015 at 20:46:41 UTC, Mike McKee wrote:
 When I run this piece of code:

 // FROM: https://dlang.org/spec/objc_interface.html
 module main;

 [...]
UDA s cannot be used for functions/methods AFAIK.
Dec 14 2015
parent reply Meta <jared771 gmail.com> writes:
On Monday, 14 December 2015 at 23:34:28 UTC, tcak wrote:
 On Monday, 14 December 2015 at 20:46:41 UTC, Mike McKee wrote:
 When I run this piece of code:

 // FROM: https://dlang.org/spec/objc_interface.html
 module main;

 [...]
UDA s cannot be used for functions/methods AFAIK.
It doesn't give an example in the documentation, but UDAs certainly can be used for functions and methods. http://dpaste.dzfl.pl/14722d9a289b In fact, `selector` is a compiler-recognized UDA specifically used for Objective-C compatibility. As for OP's issue, from the page he linked: The attribute [selector] is only defined when the version identifier D_ObjectiveC is enabled. Also, just in case: It [D's Objective-C support] is only available on OS X, compiling for 64bit.
Dec 14 2015
parent Mike McKee <volomike gmail.com> writes:
I found the fix:

$ sudo brew update
$ sudo brew uninstall --force dmd
$ sudo su
$ cd /Library
$ rm -rfd D
$ exit
$ sudo brew install dmd

This not only takes one from an older dmd to a more current 
version (in my case, from 2.068 to 2.069), but it also fixes a 
bug where /Library/D doesn't get updated. (Besides, the El 
Capitan version of 2.069 now doesn't use /Library/D.)

When I did that, I found I was able to compile like so:

volomike:cpptod4 mike$ dmd -m64 -L-framework -LFoundation test.d
volomike:cpptod4 mike$ ls
test	test.d	test.o
volomike:cpptod4 mike$ ./test
2015-12-15 03:07:52.669 test[7308:116958] Hello World!
volomike:cpptod4 mike$

So, it not only used an Objective C NSString object, but it fed 
it to NSLog and I got console output in NSLog format.
Dec 15 2015