Forums

C# to Cobra (was Dictionary Initializers)

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

C# to Cobra (was Dictionary Initializers)

Postby torial » Sat Apr 06, 2013 12:06 pm

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);
Last edited by torial on Sat Apr 06, 2013 6:06 pm, edited 1 time in total.
torial
 
Posts: 229
Location: IA

Re: Dictionary Initializers

Postby torial » Sat Apr 06, 2013 4:26 pm

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.
torial
 
Posts: 229
Location: IA

Re: C# to Cobra (was Dictionary Initializers)

Postby torial » Sat Apr 06, 2013 6:07 pm

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;
        }
torial
 
Posts: 229
Location: IA

Re: C# to Cobra (was Dictionary Initializers)

Postby torial » Sat Apr 06, 2013 7:20 pm

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.
torial
 
Posts: 229
Location: IA

Re: C# to Cobra (was Dictionary Initializers)

Postby Charles » Sat Apr 06, 2013 10:14 pm

We don't have mixed visibility properties right now.

For your delegate problem, try:
e = .escapeExpression
e(0)
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: C# to Cobra (was Dictionary Initializers)

Postby torial » Mon Apr 08, 2013 12:23 pm

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
torial
 
Posts: 229
Location: IA

Re: C# to Cobra (was Dictionary Initializers)

Postby nerdzero » Mon Apr 08, 2013 7:21 pm

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).
nerdzero
 
Posts: 286
Location: Chicago, IL

Re: C# to Cobra (was Dictionary Initializers)

Postby torial » Mon Apr 08, 2013 7:28 pm

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.
torial
 
Posts: 229
Location: IA

Re: C# to Cobra (was Dictionary Initializers)

Postby nerdzero » Mon Apr 08, 2013 9:08 pm

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"

...
nerdzero
 
Posts: 286
Location: Chicago, IL


Return to Discussion

Who is online

Users browsing this forum: No registered users and 16 guests

cron