Forums

All about types

General discussion about Cobra. Releases and general news will also be posted here.
Feel free to ask questions or just say "Hello".

Re: All about types

Postby Charles » Fri Oct 02, 2009 7:37 pm

Re: @hopscc most recent post on initializers:

-- If there is advice in the Java community that there always be a parameterless initializer, it's bad advice because it's an instant failure for classes that define immutable objects. It's also a failure if you want to code your class such that it starts with the data it needs rather than having to constantly check in each method "did property A get set yet?".

-- The problems with the single default initializer that C# and Java insert are that (a) it's an instant failure if the base class has no parameterless initializer and (b) there's no sensible reason I know of why only that initializer would be covered and not the others. Let's say you have:
class Foo
cue init
...
cue init(i as int)
...
cue init(i as int, j as int)
...

class Bar inherits Foo
pass

I can see the purpose for Bar having covers for all 3 inits. I could almost see the purpose for it having none, but that's very inconvenient. But what is the case for having the 1st, but not the 2nd or 3rd?

-- The reasons I made the call to .init or base.init explicit:

(a) The automatic insertion by compilers seems to encourage developers to just skip the issue entirely, often resulting in multiple initializers calling base, when it's more often the case that one should call the base and the others should call that. Not to mention thinking about which base initializer you want to call. It's not like it's always "base.init" as opposed to a "base.init(foo)".

(b) It's consistent with methods which don't secretly call "base".

(c) Languages that also make this explicit, such as Smalltalk and Objective-C, do well with that with no resulting problems.
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: All about types

Postby todd.a » Sat Oct 03, 2009 4:18 pm

"Coding cowboy" I like it.

My bottom line is that it's not "consistent". There is "String" then there is "int". By now I think it's established that's the way you want it. I was just raising the discussion.
todd.a
Site Admin
 
Posts: 81
Location: Chicago, IL

Re: All about types

Postby hopscc » Sun Oct 04, 2009 4:48 am

re initializers
Nah The advice is necessary because of compiler insertion of a default ctor (parameterless initializer that does nothing)
(On rereading what I wrote probably should have emphasised that that is to provide a (explicit) no-arg ctor rather than allowing a default ctor )
For an immutable object you'd be defining a ctor anyway so its not directly applicable
but its not an 'instant failure' on immutable class objects cos a no-arg ctor can be made to define a
null/zeroed immutable object or a default one ( like the origin point for an immutable point class) and its
existence continues to allow stable subclassing ( with a default (compiler inserted) constructor on the subclass)
worst case without it is is a compiler error for a missing no-arg ctor like youd called a non existent baseclass ctor anyway

for the 'coding an initialiser with initial data' - same thing - allows null/empty object or a convenience default - theres still the basic requirement that a visible object is always in a stable state

Its the same 'instant failure' if the initializer is explicit and calls a baseclass initializer with a signature that doesnt exist - so?

Anyway I've already said I dont disagree(strongly) with the idea of covering all the baseclass initializers though if having coverage of a single no-arg initializer may be confusing to some I'm not sure that covering all baseclass initializers will be less confusing.

re: explicit .init or base.init
I dont have any data on the reality of 'skip the issue' assertion in a) beyond that the ref .init vs ref.base.init and post adjust is probably more a style issue than an instruction.
I'd imagine that the path of least resistance to making the baseclass call explicit would be that the default coding response would be to just boilerplate a call to the base class shadow and then maybe
adjust to what may be a 'better' call

re (c) the resulting problem is not exposed in those languages - the additional needless boilerplate

If we take the cobra source as representative ( debateable but) simple grepping (./Source) gives
about 278 classes ( '^class')
324 lines of cue init ('cue init') => say 46 classes with multiple initializers (~ 17%)
26 lines of no-arg ctor ( 'cue init$') (9.35%) => 90% have arged ctor
53 calls to no-arg base.init ('base.init$') (19 %)

The last line is the overhead.
I'd argue that calls to baseclass arged ctors would need to be done explicitly anyway given that they're not no-arged (:-) and most of the app classes have some sort of arged ctors so forcing all the baseclass calls to be explicit forces a pretty high overhead for the simplest/default case....

I'd imagine (untested assertion) that on a less complex app than the compiler therd be a simpler class/ctor chain of construction and the proportion of ( what should be implicit) refs to base
no-arg ctors would be higher..

Hmmm
Also it seems to me that if its reasonable to shadow all the baseclass initializers as defaults ( cobra virtualized) in cobra subclasses it would also be reasonable to by default implicitly call the sig matching the baseclass initializer if a initializer is explicitly declared
Code: Select all
class Foo
    cue init
        pass
    cue init(a as int)
        pass
    cue init(a as int, b as bool)
       pass

class Bar inherits Foo
    cue init
        pass  # implicit base.init
   cue init(a as int)
        pass # implicit base.init(a)

   cue init( a as int, b as bool)
       pass # implicit base.init(a,b)
   cue init(a as int, b as int)
        pass     # gen warning no explicit call to .init   base.init as no matching base.init signature
   cue init(b as bool, a as int)
         .init(a,b)    # explicit always OK

hopscc
 
Posts: 632
Location: New Plymouth, Taranaki, New Zealand

Re: All about types

Postby todd.a » Mon Oct 05, 2009 10:27 pm

This thread reminds me of the Google Chrome thread where people were asking/begging for them to add SOCKS5 proxy support like in Firefox and Opera and the powers gave everyone the "F.U. we won't add it" :D. Do most people need it? No. Would it hurt to add it? Nope.
todd.a
Site Admin
 
Posts: 81
Location: Chicago, IL

Re: All about types

Postby hopscc » Tue Oct 06, 2009 8:09 pm

Missed this question earlier fm webnov8.
These warnings are very annoying on larger code bases and I don't think there is anyway to turn them off (if there is please tell).


Currently you can suppress warnings on a per-line basis by putting the following (trailing comment) on the line you want warnings suppressed on.
Code: Select all
 # .no-warnings.

additional comments can trail the .no-warnings. directive
e.g.
Code: Select all
var count as Int32   #  .no-warnings.
var count2 as Int32  #  .no no-warnings.  - wont recognise directive so this line will still gen warnings 

doing this will suppress any and all warnings that may come from this line (so its not specific to a particular warning)
See CobraTokenizer.cobra about line 863.

It would probably be very tedious to use this for the purposes of your particular desires ( but it is possible :) )

re your other comment, neither of us seem to be having much success in convincing the other of 'the way'. no matter...

Also to backup a bit, Funnily enough I found the other day that I would actually like/prefer a cobra (builtin) type 'byte' ( alias/synonym for 'uint8')
In programs doing byte-bashing on files, lines sprinkled with 'uint8' dcls just looks wrong - I put it down to too much eye poisoning from c/Java code ....
hopscc
 
Posts: 632
Location: New Plymouth, Taranaki, New Zealand

Re: All about types

Postby todd.a » Wed Oct 07, 2009 10:50 am

I actually don't believe in "the way" because that "way" is eventually a matter of opinion. Just trying to get some consistency in the fundamentals--types.

I'll take a patch for
#define Int int
:D
todd.a
Site Admin
 
Posts: 81
Location: Chicago, IL

Re: All about types

Postby hopscc » Wed Oct 07, 2009 6:05 pm

webnov8
Heres a freebie for you :)

If you're on a *nix box you can get the same effect by hacking up a script ( or pipeline) that
runs your source containing desired macros and use of redefinitions through either cpp or m4 to a temp file and then run cobra on the temp file
( would be slightly cleaner if cobra.exe could read its input from stdin but not really necessary)

something like (untested)
Code: Select all
m4 <switches> cobraMacroDefs myfile.cobra.m4 > myfile.tmpXXX.cobra
cobra <switches> myfile.tmpXXX.cobra
rm myfile.tmpXXX.cobra
 
hopscc
 
Posts: 632
Location: New Plymouth, Taranaki, New Zealand

Re: All about types

Postby todd.a » Wed Oct 07, 2009 8:21 pm

hopscc, you're getting dir-tay now aren't ya :D
I think it's rubbing off on me. Now Chuck will really think I went into cowboy mode.
todd.a
Site Admin
 
Posts: 81
Location: Chicago, IL

Previous

Return to Discussion

Who is online

Users browsing this forum: No registered users and 47 guests