I'm not a big fan of the "this" keyword.   I agree if going to use it, 
ensure to use it everywhere.  Consistency is good.  I like simplicity, and 
dislike redundancy.

So, about "this", alternatively,  don't use it anywhere, but maybe you 
could throw an error if a function argument name is also member variable 
name.  Not sure what static analysis is available - given that its a 
scripting language.  There's not really a compilation step is there?

OR, perhaps another option, just thinking outside the box, if a function 
argument does actually happen to match a member variable name - then 
automatically force it to actually set that variable.  When (if) this 
becomes the expected behavior, I think it would enable simplifying some 
things a lot.

Explaining with an example.  Often, when I'm coding and the arg name is the 
same as a member variable name,  then I usually find myself setting that 
early-ish in the code.  (just using a pseudo code to explain, obviously not 
real code;)

class Blahh
this.toX: TYPE_A
this.toX: TYPE_B
fn SetXandY(toX: TPYE_A, toY: TYPE_B )
  this.toX = toX
  this.toY = toY
enfunc
endclass

So, I find that pattern happens a lot.  And it gets real tedious.    I 
think this could be simplified to the following;

class Blahh
toX: TYPE_A
toX: TYPE_B
fn  SetXandY(toX, toY)
enfunc
endclass

ie. This would set the member variables, toX and toY automatically.

Now, we might want to do some sanity check on the argument before 
clobbering whatever was in there previously.  The developer could very 
easily use another temporary name, for example prefixed with p_ as they 
wish, and check it is in range for example, before setting it.  Like so;

class Blahh
toX: TYPE_A
toX: TYPE_B
fn  SetXandY(p_toX, toY)
if p_toX in range
    toX = p_toX
else
  ignore, or throw, or set some default, whatever for toX 
endif
enfunc
endclass

In this last example, any matching names could be automatically set, so 
here, the toY would be automatically set.

This is inspired by your idea from the constructor idea you had here;
def new(this.lnum, this.col)
enddef

I like that one :)

Cheers,

Chris Plewright





On Monday, 19 December 2022 at 10:00:22 UTC+11 Maxim Kim wrote:

>
>
> понедельник, 19 декабря 2022 г. в 00:33:24 UTC+11, Bram Moolenaar: 
>
>>
>>
>>
>>
>> For object members most languages use the "this." prefix. But not 
>> everywhere, which makes it inconsistent. A good example is a 
>> constructor where object members that are also an argument need to be 
>> prefixed with "this." to avoid a name collision, while other object 
>> members are used without "this.". I find that very confusing. Example: 
>>
>> SomeObject(String name) 
>> { 
>> this.name = name; 
>> gender = Gender.unknown; 
>> } 
>>
>>  
> I would go with this.name and this.gender while accessing the variables.
>  
>
>>
>> One thing I'm not yet sure about is the declaration. Currently it works 
>> like this: 
>>
>> this.name: string 
>> this.gender: Gender 
>>
>> and 
>
> var name: string
> var gender: Gender
>
> to declare them. 
>
>

-- 
-- 
You received this message from the "vim_dev" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

--- 
You received this message because you are subscribed to the Google Groups 
"vim_dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to vim_dev+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/vim_dev/0f9af573-98b6-47b5-aba3-eb36f8c19d1cn%40googlegroups.com.

Raspunde prin e-mail lui