Under Review
over 2 years ago

RFC: Convert String to Symbol: String.toSymbol()

So, I would like the following and the use case is Rez.Strings and method resolution.

I went into some kind of spam trap at the forums, so I made a snippet instead: https://gitlab.com/-/snippets/2400797

The rules are simple, any weird chars are discarded or normalized, so e-acute becomes e, chars which are not able to symbolized are discarded and that's it. Eg:

  • "foo bar" becomes :foo_bar
  • "foo bar baz." becomes :foo_bar_baz
  • " foo" becomes :foo 
  • "_foo " becomes :_foo
  • "_foo _foo" becomes :_foo__foo"
  • "foo" becomes :foo
  • "résumé" becomes :resume
  • "@you" becomes :you
  • "+you" becomes :you
  • "" becomes throw new exception because really

It kinda makes sense to do this, because symbols can be Stringified. 

var x = 0;

the symbol :x can now be stringified to and becomes "x", as discussed here: https://forums.garmin.com/developer/connect-iq/f/discussion/301701/struggling-a-bit-with-views-and-how-stuff-gets-on-the-screen/1460994#1460994


Original OP:

The use case is Rez.Strings, but it translates to other bits and pieces as well.

var resource = Ui.loadResource(getAttribute(Rez.Strings, :foo));
 

With Rez.Layouts you can sorta do this, because the layout is a function, so you can do this:

 

var layout = :MyLayout;
var method = new Lang.Method(Rez.Layouts, layout);
setLayout(method.invoke(dc));

Now you can for example ask Rez.Strings:

if (Rez.Strings has :foo) {
    Ui.loadResource(getAttribute(Rez.Strings, :foo))
}

// This ofc translates to every module or class

Parents Comment Children
No Data