- Code: Select all
string[] array;
array = new string[] {"A", "B", "C"};
Forums
Omitted array coding
11 posts
• Page 1 of 2 • 1, 2
Omitted array coding
Is there such a form of the C# in Cobra?
- arisawa
- Posts: 51
Re: Omitted array coding
strarray = @['A', 'B', 'C']
# so the general form is:
@[ ... ]
In other words, like a list, but starting with an @. This is a syntax I copied from Powershell.
An array-based syntax became important because there are several methods in the .NET standard library that take an array of some type as opposed to a List<of>. .NET < 2.0 did not have generics.
Using .toArray is okay, but the @[...] is more direct and efficient.
- Charles
- Posts: 2515
- Location: Los Angeles, CA
Re: Omitted array coding
Would it be possible to extend String to overload:
String().split()
String.join()
so they can use List<> instead of array?
In case that you think it is a good idea and makes sense, could you give me some tips so I can integrate it in Cobra.Lang?
String().split()
String.join()
so they can use List<> instead of array?
In case that you think it is a good idea and makes sense, could you give me some tips so I can integrate it in Cobra.Lang?
- agustech
- Posts: 37
Re: Omitted array coding
Yes, I think this is a good idea. Unfortunately, Cobra does not yet handle the combination of extensions and member overloads. For example, the following code fails to compile:
However, it does get all the way to code generation so maybe this is a reasonably small bug. I have augmented ticket:1 with some notes.
You can tell extensions were high on my mind since I entered them as the first Trac ticket.
Regarding augmenting Cobra.Lang, the files are now found in a subdir of Source called Cobra.Lang so that's:
<workspace>\Source\Cobra.Lang\*.cobra
You'll notice one of the files is called "Extensions.cobra". Also, if you were to add a file, you would add its filename to the "files.text" file in that same directory.
Then to rebuild the Cobra.Lang.dll in the Source directory, you say "cobra -bsl" or "cobra -bsl -v:2" if you want something more interesting to look at it.
Or you can say "install-from-workspace" if you're ready for that.
Finally, when creating library code, strongly consider using interfaces like IList<of> for argument types so that the method can be reused with more types of objects.
extend String
def split(chars as IList<of char>) as String[]
return String[](5) # TODO
class X
def main is shared
s = 'a,b:c:d,e,f'
trace s.split([c',', c':'])
However, it does get all the way to code generation so maybe this is a reasonably small bug. I have augmented ticket:1 with some notes.
You can tell extensions were high on my mind since I entered them as the first Trac ticket.
Regarding augmenting Cobra.Lang, the files are now found in a subdir of Source called Cobra.Lang so that's:
<workspace>\Source\Cobra.Lang\*.cobra
You'll notice one of the files is called "Extensions.cobra". Also, if you were to add a file, you would add its filename to the "files.text" file in that same directory.
Then to rebuild the Cobra.Lang.dll in the Source directory, you say "cobra -bsl" or "cobra -bsl -v:2" if you want something more interesting to look at it.
Or you can say "install-from-workspace" if you're ready for that.
Finally, when creating library code, strongly consider using interfaces like IList<of> for argument types so that the method can be reused with more types of objects.
- Charles
- Posts: 2515
- Location: Los Angeles, CA
Re: Omitted array coding
Chuck wrote:strarray = @['A', 'B', 'C']
# so the general form is:
@[ ... ]
In other words, like a list, but starting with an @. This is a syntax I copied from Powershell.
An array-based syntax became important because there are several methods in the .NET standard library that take an array of some type as opposed to a List<of>. .NET < 2.0 did not have generics.
Using .toArray is okay, but the @[...] is more direct and efficient.
I forgot to mention that you can allocate a blank array like so:
nums = number[](100)
# the very general format is
<var-name> = <type>(<args>)
# and in the case of arrays, the type is an array type (suffixed by "[]")
# and the arg is the length of the array,
# so the array allocation form is:
<var-name> = <type>[](<length>)
# another example
n = 1024
ch = char[](n)
for i in n, ch[i] = c'z'
- Charles
- Posts: 2515
- Location: Los Angeles, CA
Re: Omitted array coding
agustech wrote:Would it be possible to extend String to overload:
String().split()
String.join()
so they can use List<> instead of array?
In case that you think it is a good idea and makes sense, could you give me some tips so I can integrate it in Cobra.Lang?
I fixed the problem that was preventing this. I also added the extensions for split() to Cobra.Lang\Extensions.cobra. You can pass in any IList<of char>. The return value is a List<of String>.
Try it out.
- Charles
- Posts: 2515
- Location: Los Angeles, CA
Re: Omitted array coding
It works, but ...
I have been trying to implementing some other methods and I have found the following issues (sorry if they are stupid ones):
1. This gives errors on the "Clean up workspace" phase but it works ok when I try to use it (I guess that there are better/more efficient ways to do it, it is just a test).
2. I can not use list[:-1] inside Extensions.cobra while it works perfectly on my programs. I guess it is because that idiom is defined somewhere later.
3. overloaded shared methods compile correctly but they can not be used on programs, i.e. this compiles inside Extensions.cobra:
but gives ' "String" is type' error when trying to use it:
am I doing something wrong?
I have been trying to implementing some other methods and I have found the following issues (sorry if they are stupid ones):
1. This gives errors on the "Clean up workspace" phase but it works ok when I try to use it (I guess that there are better/more efficient ways to do it, it is just a test).
- Code: Select all
def split(ch as char) as List<of String>
test
s = 'a,b,c,d,e,f'
assert s.split(c',') == ['a', 'b', 'c', 'd', 'e', 'f']
body
return List<of String>(.split(@[ch]))
2. I can not use list[:-1] inside Extensions.cobra while it works perfectly on my programs. I guess it is because that idiom is defined somewhere later.
3. overloaded shared methods compile correctly but they can not be used on programs, i.e. this compiles inside Extensions.cobra:
- Code: Select all
def join(j as String, li as IList<of String>) as String is shared
test
#li = ['a','b','c','d','e']
#assert String.join(',',li) == "a,b,c,d,e" # error, can not test it while adding it?
pass
body
str = ""
if li.count == 0, return str
#for s in li[:-1] #fails to compile if in library, if standalone program it works ok
for n in li.count
if n > (li.count - 2), break
str += li[n] + j
str += li[li.count - 1]
return str
but gives ' "String" is type' error when trying to use it:
- Code: Select all
assert String.join(',',['a','b','c','d','e']) == 'a,b,c,d,e' # error compiling
am I doing something wrong?
- agustech
- Posts: 37
Re: Omitted array coding
1. I fixed the problem with the "Clean up workspace" phase of the installer. It was forgetting to use the "-ert:yes" flag that you see in comp.bat (or comp on *nix).
2. The problem with list[:-1] is almost certainly a bug, but I'm unable to reproduce it. I would love to get the exact code for this.
3. Interesting. C# has only instance-oriented extension methods and I followed their lead doing the same thing with Cobra. I never even tried a shared one. But I can't think of any reason not to support shared. Can anyone?
2. The problem with list[:-1] is almost certainly a bug, but I'm unable to reproduce it. I would love to get the exact code for this.
3. Interesting. C# has only instance-oriented extension methods and I followed their lead doing the same thing with Cobra. I never even tried a shared one. But I can't think of any reason not to support shared. Can anyone?
- Charles
- Posts: 2515
- Location: Los Angeles, CA
11 posts
• Page 1 of 2 • 1, 2
Who is online
Users browsing this forum: No registered users and 40 guests