• Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.

Poll: String API Preference

Which interface do you prefer?


  • Total voters
    15
  • Poll closed .
Status
Not open for further replies.
Out of curiosity, which do you prefer more?

Option A:

JASS:
call String["Hello"].find("ell")
call String["Hello"].toUpperCase()
set someStr = String[someStringVariable].trim()

(just examples)

Option B:
JASS:
call String.find("Hello", "ell")
call String.toUpperCase("Hello")
set someStr = String.trim(someStringVariable)

I'm just curious. I can't really tell myself. As for efficiency, the first option is 1 extra function call/set, but it is sort of negligible if you prefer the API that way. It is kinda annoying to type the brackets, but it seems to make sense. The second option is the traditional JASS method, where you just make the "source" string the first parameter.

Thanks. :grin:

FAQ: (Frequently Anticipated Questions)

  • What about Wurst stdlib?

    It is already awesome enough as it is. I'm just curious about it from a vJASS standpoint.
 
Level 29
Joined
Jul 29, 2007
Messages
5,174
What exactly is option A supposed to be? is String["Hello"] some voodoo object initialization in vJass? because if so, it's the most ugly and ambiguous object initialization I ever saw.

I assume with less ambiguous code it would be something along the lines of
JASS:
local String s = String("Hello")
call s.find("ell")
// or
String("Hello").find("ell")
// but this is ugly and bad

In this case, I believe most languages actually give access to both - methods of a global object (static functions) and methods for each instance (instance methods)...which are really the same thing, except for the this argument being explicit or implicit.
 
What exactly is option A supposed to be? is String["Hello"] some voodoo object initialization in vJass? because if so, it's the most ugly and ambiguous object initialization I ever saw.

vJASS allows you to override the operator [] to return an instance from some given "key". For example, let's say you have a struct that is associated with a unit:
JASS:
struct A
    unit u

    static method create takes unit input returns thistype
        local thistype this = thistype.allocate()
        set this.u = input
        return this
    endmethod
endstruct
And you want to get the instance from the unit. You can store the instance in a table under the unit's ID. Then you can write a method like this:
JASS:
static method getInstance takes unit u returns thistype
     return SomeTable[GetHandleId(u)]
endmethod
As such, if you want to quickly do an action for a unit's instance, you would end up with something like:
JASS:
call A.getInstance(u).doSomeAction()
Which is fine, but long. You can always set A.getInstance(u) to a variable, but it is still a little ugly. Alternatively, you can implement an operator to return it, and then you'd end up with:
JASS:
call A[u].doSomeAction()
Which is considerably cleaner.

It isn't technically initialization though. It is just retrieval. The way it is set up in object A might as well be an initialization though due to the implementation.

GhostWolf said:
I assume with less ambiguous code it would be something along the lines of
JASS:
local String s = String("Hello")
call s.find("ell")
// or
String("Hello").find("ell")
// but this is ugly and bad

In this case, I believe most interpreted languages actually give access to both - methods of a global object (static functions) and methods for each instance (instance methods)...which are really the same thing, except for the this argument being explicit or implicit.

Yes, sort of. Although, actually having them as objects would take a little extra work and it would serve as different functionality. Right now, consider the two options to be equivalent.

And yeah, that's true. I suppose I could always offer both options. Thanks. :)

@gorillabull: Thanks. :)
 
Level 14
Joined
Jun 27, 2008
Messages
1,325
A is syntax sugar abuse.
#

Id prefer a mix of both, trash the syntax abuse but keep the possibility to "chain" functions, because
stringfun("Hello").toUpper().substring(2, 3).replace("AT", "@")
looks cooler than
replace(substring(toUpper("hello"), 2, 3), "AT", "@")
(Functions are arbitrary examples, no clue what ur implementing)

.. or just use wurst and do
Wurst:
"Hello".toUpper().substring(2, 3).replace("AT", "@")
 
B bro.

A is syntax sugar abuse.

I see what you mean. But syntactic sugar inherently states "abuse me".

#

Id prefer a mix of both, trash the syntax abuse but keep the possibility to "chain" functions, because
stringfun("Hello").toUpper().substring(2, 3).replace("AT", "@")
looks cooler than
replace(substring(toUpper("hello"), 2, 3), "AT", "@")
(Functions are arbitrary examples, no clue what ur implementing)

.. or just use wurst and do
Wurst:
"Hello".toUpper().substring(2, 3).replace("AT", "@")

That'd be nifty if it were possible. I could make it as an object, and then add .str method or something. e.g.:
JASS:
local String obj = String.create("Hello")
local string s = obj.toUpper().substring(2, 3).replace("AT", "@").str

It could be interesting. It would be annoying to add .str to the end of all your actions, but it is reasonable.
 
Level 6
Joined
Jul 30, 2013
Messages
282
JASS:
a) love the wurst way
b) String(str).method().method().toString()
c) some funky thing that looks like b) but only stores currently manipulated string (efficient but cant store reference to wrapper or blows up in your face)
d) A  It's better than String.fn(str, arg, arg2)  but barely.
 
Sorry to post but.

Shall we put Regex support? It would be awesome.

Seems a little overkill to add Regex, especially since only programmers are familiar with it. It would have the same issue as all those data structure snippets.

I suppose it could be useful for really intense chat parsing, but that is about it. :p
 
but why would we possibly need intense chat parsing?

idk. If you want to have advanced reading or filtering or something strange like that. Regex just makes pattern matching and all that shiz a lot easier. But it is rather annoying to implement in Wc3, especially since strings all end up cached in memory.
 
Level 6
Joined
Jul 30, 2013
Messages
282
I vote for

JASS:
local String s = String.create("My String")
call s.substr(0, 5).append("some string").toLower().toUpper()
call BJDebugMsg(s.str())
call s.display() // shorter

i kind of like that.. except i feel that you shouldn't edit the original string..
(or you should like explicitly as for it if you want it)
 
idk. If you want to have advanced reading or filtering or something strange like that. Regex just makes pattern matching and all that shiz a lot easier. But it is rather annoying to implement in Wc3, especially since strings all end up cached in memory.

Mag requested me this months ago.

We could just add String.Match or String.Gsub functionalities to JASS(yes, Im talking about Lua)
 
Level 6
Joined
Jul 30, 2013
Messages
282
i would go for
.matches("regex here") : boolean
.replace("regex", "with plain string") : string // and replaceAll or replace N to go with it possibly..
.indexOf("regex") : int , -1 if not found (might have extra args for start index, or some bool flag for regex/plainstr)
 
Status
Not open for further replies.
Top