Forums

Converting objects to strings

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

Re: Converting objects to strings

Postby hopscc » Fri Apr 18, 2008 12:47 am

yes.. should have remembered that - the only solution to that without some sort of dynamic extension is to make cobra provide its own augmented version of the system classes/intefaces and only allow/expose those in cobra ... yucky

Shouldnt the "To??String" check be first ( at least for non baseclass objects) otherwise a user class that also supports one of the I{LIst,Dict,ENumerable} interfaces
will never get its own to??String implementation checked for or called

I presume reflection lookup is a relatively expensive op in .NET.
Does C#/.Net provide a simple cheap way to tag a class? (in Java I'd use a tag interface, check for that and invoke if exists\otherwise continue on with the
cobra runtime handling (List and delegate invocation))

You're right about the "care for access to these details" - most user programs will use the system collections ( or their interfaces anyway) so end up passing
through the cobra runtime lookup/invocation for to{tech,Display}String which means access to that needs to be the cheapest/fastest path withous disallowing an override by user classes..

Other random synapse firings...
Are there still only the basic 7 baseclass types to check for ??
null, bool,string,IList,IDict,IEnumerable, Enum
fallback to ToString...

if that s all just make a precedence ordered list of type -> delegate and expose it for user augmentation if need be ...

also,
for system library baseclasses if ToTechString is basically just ToDisplayString prefixed by the type ( from getType) do you still need both??
do the implementation only for To DisplayString and ToTechString becomes composited from "[x.getType][x.toDisplayString]'..
User implemented types can still implement their own if desired.
hopscc
 
Posts: 632
Location: New Plymouth, Taranaki, New Zealand

Re: Converting objects to strings

Postby Charles » Fri Apr 18, 2008 12:58 am

You're right, the custom handling should be checked for first.

Reflection is expensive, but after the first lookup you can cache the result in a dictionary keyed by type. So shall we use interfaces like IToPrintString and IToTechString, or use reflection to look up the methods by names and then cache them by type for speed? The name approach might be interesting because then you could make your own ToFooStringBuilder and have it look for "toFooString". Foo could be "Display" for you GUI or something.

Regarding the "precedence ordered list of type --> delegate" that's what I was referring to before. But it's on hold for now.

The differences between toPrintString and toTechString are more involved than just the type prefix on collections. For example, toPrintString does not quote strings--unless they are inside a collection. This matches the str() and repr() functions in Python:
Code: Select all
>>> s = 'aoeu'
>>> print str(s)
aoeu
>>> print repr(s)
'aoeu'
>>> t = ['a', 'b']
>>> print str(t)
['a', 'b']
>>> print repr(t)
['a', 'b']


I hope to post some code later.
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: Converting objects to strings

Postby Charles » Fri Apr 18, 2008 1:41 am

Here's what I have so far as a standalone Cobra file. After the TODO list is largely finished, then I'll incorporate into Cobra itself.
Attachments
ToStringBuilder.cobra
(8.16 KiB) Downloaded 574 times
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: Converting objects to strings

Postby Charles » Thu Jul 03, 2008 10:04 am

I just checked in the enhancements described above. The major benefit is that when you print a collection such as [1, 2, 3] you will get:

[1, 2, 3]

instead of the ugly and uninformative .NET default:

System.Collections.Generic.List`1[System.Int32]

Ditto for when you do string substitution such as 'The numbers are [numbers]'. This is accomplished without a special "CobraList" class. Lists in Cobra are still the standard .NET generic lists that developers normally use.

Also, there are new properties, CobraCore.printStringMaker and CobraCore.techStringMaker. The first is used for print statements and string substitution. The second is used for assert failures and the trace statement. You can assign your own custom string makers to these in order to customize output. See the test case in the check-in for how to do so.
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: Converting objects to strings

Postby hopscc » Mon Jul 07, 2008 5:21 am

Cool - havent thrashed this properly yet but theres a coupla things

a) SInce that code went into the tree I've started getting intermittant failues of the 370-Threads test
r-testify contains
----------------------------------------------------------------------------------------------------
Compile C#:
Compiling to produce 370-Threads.exe
Deleting intermediate files.
Compilation succeeded
----------------------------------------------------------------------------------------------------
Run:
Output:
Unhandled Exception: Cobra.Lang.EnsureException:
sourceSite = C:\home\hops\src\cobra\wkspace\Source\CobraLang.cobra:1396 in Set<of>.add for object Set<of Object>[]
info = nil
this = Set<of Object>{,}
.contains(item) = false
item = 'a'

at Cobra.Lang.Set`1.Add(T item)
at Cobra.Lang.StringMaker.MakeString(Object x)
at ThreadExample.Run() in c:\home\hops\src\cobra\wkspace\HowTo\370-Threads.cobra:line 20
at ThreadExample.Main() in c:\home\hops\src\cobra\wkspace\HowTo\370-Threads.cobra:line 33

'a''b'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb...
Exit code = -532459699


It doesnt always fail but does 4 runs out of 5.... (Threads, loaded system - Race??)


b) The following code
Code: Select all
   def main is shared
      
      a as char[] = @[c'0',c'0',c'0']         #$sharp(r'new Char[3]')   # as char[3] # = @[c'0']
      a[0]=c'a'
      a[1]=c'b'
      a[2]=c'c'
   
      print '1', a
      
      a[0]=c'a'
      a[1]=c'a'
      a[2]=c'b'
   
      print '2', a
      
      a[0]=c'a'
      a[1]=c'a'
      a[2]=c'a'
      print '3', a
      
      # more than 3 items in a row stack ovfl ??
      #b = @[ c'a', c'a', c'a', c'a', c'a', c'a', c'a', c'a', c'a', c'a']
      #print '4',b
      
      c = @[ c'a', c'a', c'b', c'b', c'b']
      print '5',c
      # uncommenting this gives stack overflow
      #c = @[ c'a', c'a', c'b', c'b', c'a', c'a', c'b', c'b', c'a', c'a']
      #print '5',c



compile and go gives
1 [a, b, c]
2 [a, (Repeated 'char'), b]
3 [a, (Repeated 'char'), (Repeated (Repeated 'String'))]

Process is terminated due to StackOverflowException.


Havent done any investigation as to cause.
hopscc
 
Posts: 632
Location: New Plymouth, Taranaki, New Zealand

Re: Converting objects to strings

Postby Charles » Mon Jul 07, 2008 9:33 pm

Nice finds! I'll look into these this week.
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: Converting objects to strings

Postby Charles » Mon Jul 07, 2008 11:31 pm

I cannot reproduce the race condition problem you are having even after running 10 times. But I have no doubt that you're having it. So I made StringMaker.makeString synchronized. Try again and let me know.
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: Converting objects to strings

Postby Charles » Tue Jul 08, 2008 12:22 am

I fixed the bugs you reported. Let me know if you have any further problems.
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: Converting objects to strings

Postby hopscc » Tue Jul 08, 2008 3:36 am

I'll give a qualified OK to the race condition fix :)
patched source run 10 times sequentially and concurrently didnt fail at all
Un patched source failed on first run
assume fixed until proven otherwise
Ta.
hopscc
 
Posts: 632
Location: New Plymouth, Taranaki, New Zealand

Previous

Return to Discussion

Who is online

Users browsing this forum: No registered users and 24 guests