Page 1 of 1

C# to Cobra (was Dictionary Initializers)

PostPosted: Sat Apr 06, 2013 12:06 pm
by torial
I'm not sure if this is a PEBCAK issue or compiler issue, so thought I'd post here.

I've got the following code:
Code: Select all
d = {{ "&": "&amp;" }, { "<": "&lt;" }, { ">": "&gt;" }, { '"': "&quot;" }, { "'": "&#x27;" }, { "`": "&#x60;" }}
_oHTML = Dictionary<of String,String>(d,StringComparer.ordinal)


Which is attempting to make use of the constructor identified here: http://msdn.microsoft.com/en-us/library ... 00%29.aspx (which is a valid constructor all the way back to .Net 2.0 so I don't think I have a framework target issue). When I attempt to compile, I get:

SafeString.cobra(0,0): Error: The best overloaded method match for "System.Collections.Generic.Dictionary<string,string>.Dictionary(int, System.Collections.Generic.IEqualityComparer<string>)" has some invalid arguments (SnakeTracks)


Am I calling the constructor incorrectly? A similar call in C# does not cause this issue:
Code: Select all
            var x = new Dictionary<string, string>(HTML, StringComparer.Ordinal);

Re: Dictionary Initializers

PostPosted: Sat Apr 06, 2013 4:26 pm
by torial
PEBCAK - I was accidentally doing an initialization against a Set instead of a Dictionary... :oops:

This came about because I was porting some C# code to Cobra and was being lazy and thinking the initializers were more similar than was warranted.

Re: C# to Cobra (was Dictionary Initializers)

PostPosted: Sat Apr 06, 2013 6:07 pm
by torial
How do you do mixed scope properties in Cobra? For example I have this C# code, how would it be rendered in Cobra?

Code: Select all
       
public string LiteralText
        {
            get;
            private set;
        }

Re: C# to Cobra (was Dictionary Initializers)

PostPosted: Sat Apr 06, 2013 7:20 pm
by torial
I have another issue I'm trying to deal with. I have an interface that defines a property to a delegate. My class that attempts to implement this is fine, until I try to call this delegate. Because it is declared as a property, I get an error "Unexpected call".

a very dumbed down version:
class SafeHandler
sig EscapeExpressionXXX(expression as String?) as String

interface ITextExpression
pro escapeExpression as SafeEscapeHandler.EscapeExpressionXXX?

class FormatExpression
implements ITextExpression

pro escapeExpression from var as SafeEscapeHandler.EscapeExpressionXXX?

def eval(o as String) as String
return .escapeExpression(o) #<-- This is where I get the unexpected Call.

Re: C# to Cobra (was Dictionary Initializers)

PostPosted: Sat Apr 06, 2013 10:14 pm
by Charles
We don't have mixed visibility properties right now.

For your delegate problem, try:
e = .escapeExpression
e(0)

Re: C# to Cobra (was Dictionary Initializers)

PostPosted: Mon Apr 08, 2013 12:23 pm
by torial
Thanks - that seems to have worked for the delegate properties.

I'm getting a new error for a shared class:
SnakeTracksFormatter.cobra(0,0): Error: "SnakeTracksFormatter._ih_invariantGuard": cannot declare instance members in a static class (SnakeTracks)


I've confirmed that all methods have "is shared". I can't help but think it is from some shared variable declaration. I've included the list, in case you can see anything I'm doing wrong with it:

var __helpers = Dictionary<of String, Helper>(StringComparer.ordinal) 
is shared
var __blockHelpers = Dictionary<of String, BlockHelper>(StringComparer.ordinal)
is shared
var __partials = Dictionary<of String, BlockHelperContext>(StringComparer.ordinal)
is shared

var __templateCache = Dictionary<of String, BlockHelperContext>(StringComparer.ordinal)
is shared

enum ParamState
is private
Whitespace
InObjectName
InSingleQuoteBlock
InDoubleQuoteBlock

const formatExpressionBegin = "{{"
const formatExpressionEnd = "}}"
var expressionLength = .formatExpressionBegin.length
is shared

Re: C# to Cobra (was Dictionary Initializers)

PostPosted: Mon Apr 08, 2013 7:21 pm
by nerdzero
There must be more to it. That code in a stand alone file plus some empty classes for the various Helper types compiles fine.

I would try compiling the real code again but keeping the intermediate C# files and seeing if anything sticks out as wrong (assuming it gets that far in the compilation process).

Re: C# to Cobra (was Dictionary Initializers)

PostPosted: Mon Apr 08, 2013 7:28 pm
by torial
My temporary workaround is to just remove the "is shared" from the class definition -- I'm able to get it to compile that way. I'm about 80% done w/ my port from C# (then I have to test it!), so I probably won't revisit this until I've got the port knocked out.

Re: C# to Cobra (was Dictionary Initializers)

PostPosted: Mon Apr 08, 2013 9:08 pm
by nerdzero
Aha, I don't think "is shared" is officially supported at the class level but you can kind of do it with this syntax:

class PrettyMuchShared

cue init is private
# prevent instances from being created
base.init

shared # everything indented past here "is shared"
var foo = 1

def bar as String
return "something"

...