Changes to Enum naming?

128 views
Skip to first unread message

Keith Stanger

unread,
Nov 23, 2015, 4:02:10 PM11/23/15
to j2objc-...@googlegroups.com
Hello users, I'm looking for your opinion on this matter.

J2ObjC generates two types for your Java enums. One is a Objective-C class type that emulates the Java type and is used by any translated code. The other is a C enum (using NS_ENUM macro). For a java enum named "Foo", the generated Objective-C class is named "FooEnum" and the generated C enum is named "Foo".

I'm proposing to reverse this naming. I'd like to make the generated Objective-C type consistent with the translated names of regular Java classes. I'm also assuming that the Objective-C type is more frequently used than the C enum, so the more verbose "Enum" suffix should go on the less frequently used type.

Aside from having to update your code, would such a change be harmful in any way, or reduce the readability of your code? Are there suggestions on how to name the C enum so that it doesn't collide with the Objective-C type?

I'm trying to make the generated code more intuitive to work with. In this case enums are the only kind of Java type that deviate from the regular naming scheme. (classes, interfaces and annotations all follow the same naming pattern)

er...@pltech.co.nz

unread,
Nov 23, 2015, 4:38:23 PM11/23/15
to j2objc-discuss
Sounds like a good change - I only use the Objective-C type, and having the C enum there was initially rather confusing

Daniel Dickison

unread,
Nov 24, 2015, 5:14:07 PM11/24/15
to j2objc-discuss
I also think this would be a good change, especially because this will result in the actual enum type being named with an "Enum" suffix.

Bruno

unread,
Nov 25, 2015, 9:30:23 PM11/25/15
to j2objc-discuss
The change makes sense and I don't have any code that needs editing for this.

I'm seeing some issues though trying to use the enums within Swift. When I use the C enums, they run successfully but don't print correctly. For the Objective-C enums, with Swift it fails during linking.

Java Source:

public class MyClass {
    public enum Day {
        SUNDAY, MONDAY, TUESDAY, WEDNESDAY,
        THURSDAY, FRIDAY, SATURDAY
    }
}


C Enum prints enum class name, not the value

let mon: MyClass_Day = MyClass_Day.MONDAY
let tue: MyClass_Day = MyClass_Day.TUESDAY
print("Enums: \(mon), \(tue)")

It now prints the class name and not the enum value or string:
"Enums: MyClass_Day - MyClass_Day"
They are two distinct values and comparison shows them as being
different


Objective-C Enum compiles in Swift but fails to link

let mon: MyClass_DayEnum = MyClass_DayEnum_get_MONDAY()

This autocompletes in Swift and compiles... but fails at link time:
Undefined symbols for architecture arm64:
  "_MyClass_DayEnum_get_MONDAY", referenced from:
  ...
ld: symbol(s) not found for architecture arm64




--
You received this message because you are subscribed to the Google Groups "j2objc-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to j2objc-discus...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

er...@pltech.co.nz

unread,
Nov 25, 2015, 10:35:40 PM11/25/15
to j2objc-discuss
What I'm doing is instead of using:

let mon: MyClass_DayEnum = MyClass_DayEnum_get_MONDAY()
I use:
let mon: MyClass_DayEnum = MyClass_DayEnum.MONDAY()
to avoid that error

Keith Stanger

unread,
Nov 26, 2015, 9:17:24 AM11/26/15
to j2objc-discuss
This makes sense because "MyClass_DayEnum_get_MONDAY()" would be an inline function. Using the class method as Eric suggests would be the supported way to access the value in Swift.

I'm not sure what the issue is with printing the C enum. Do you experience different behavior with other NS_ENUM declarations? There's nothing special about the C enums we generate.

Bruno

unread,
Nov 26, 2015, 11:59:17 AM11/26/15
to j2objc-discuss
Keith - why do we have the C style enum at all? What's the purpose?

Eric - thanks for the suggestion, when I try that, the compile fails reporting:

Type 'MyClass_DayEnum' has no member 'MONDAY'

I'm using J2ObjC 0.9.8.2.1, anything else to try?


Here's the output in the Objective-C header file:

#define MyClass_DayEnum_MONDAY MyClass_DayEnum_values_[MyClass_Day_MONDAY]
J2OBJC_ENUM_CONSTANT_GETTER(MyClass_DayEnum, MONDAY)

Keith Stanger

unread,
Nov 26, 2015, 12:10:04 PM11/26/15
to j2objc-discuss
Try adding --static-accessor-methods or --swift_friendly.

The C enums are provided as a convenience for native Objective-C code in case you don't need the heavy Java-style enum. Of course if you're interacting with a translated API you'll need the heavier type. So far the main use-case is as case values in a switch statement. This is actually how we translate a Java switch on an enum:

switch([javaEnum ordinal]) {
  case MyClass_Day_MONDAY: ...
  case MyClass_Day_TUESDAY: ...

Bruno

unread,
Nov 26, 2015, 12:20:55 PM11/26/15
to j2objc-...@googlegroups.com
Thanks Keith - it was fixed with --static-accessor-methods. Also, the Objective-C enum prints correctly in that case.

When I used --swift_friendly flag, that wasn't recognized. That isn't found in the GitHub repo, so I presume that this will be added in the future?

Keith Stanger

unread,
Nov 26, 2015, 12:27:53 PM11/26/15
to j2objc-...@googlegroups.com

Bruno

unread,
Nov 28, 2015, 9:52:23 PM11/28/15
to j2objc-...@googlegroups.com
Thanks Keith. The code looks correct but I suspect "--swift-friendly" hasn't been pushed to a public release yet. No great problem, just please check this when the next version is pushed.

If the flag is supplied to j2objc 0.9.8.2.1, then it's identified as "invalid flag":

$ /Users/brunobowden/dev/j2objc-versions/j2objc-0.9.8.2.1/j2objc --swift-friendly
j2objc: invalid flag: --swift-friendly
Usage: j2objc <options> <source files>
use --help for a list of possible options

Whereas, only supplying "--static-accessor-methods" flag, the flag is accepted and it instead fails with the message "no source files" - this is expected behaviour of course.

$ /Users/brunobowden/dev/j2objc-versions/j2objc-0.9.8.2.1/j2objc --static-accessor-methods
j2objc: no source files
Usage: j2objc <options> <source files>
use --help for a list of possible options

Finally, just confirm that we've got the right version:

$ /Users/brunobowden/dev/j2objc-versions/j2objc-0.9.8.2.1/j2objc -version
j2objc 0.9.8.2.1

Keith Stanger

unread,
Nov 30, 2015, 8:50:35 AM11/30/15
to j2objc-...@googlegroups.com
--swift-friendly is a new flag and currently only available at HEAD. It enables --static-accessor-methods and --nullability.  --nullability is another new flag at HEAD that converts Java's @Nullable and @Nonnull to Objective-C's _Nullable and _Nonnull.
Reply all
Reply to author
Forward
0 new messages