Is it possible to use string literal types in AssemblyScript? - assemblyscript

Is it possible to create a string literal type in AssemblyScript that resembles the type keyword used in TypeScript?
TypeScript example:
export type MyType = 'foo' | 'bar';

Related

Convert Type Output to String

is it possible to collect the output of the type function and return it as a string? I can write out a list of conditions for each type of output this function can produce, but I'm hoping I can just convert the output directly into a string variable.
(type "Test")
STR ;<--- System value type
(cond ((= 'STR (type "Test")) "STR"))
"STR" ;<- String value type
vl-princ-to-string or vl-prin1-to-string will generate a string representation of any type of AutoLISP data (though, be aware that you have no control over the resulting precision of numerical data, as these functions operate independently of the LUPREC/AUPREC system variables and do not accept any precision arguments).
Since the type function returns a symbol, you can use the vl-symbol-name function to convert the symbol to a string, e.g.:
_$ (vl-symbol-name (type "test"))
"STR"
Note also that, contrary to your answer, the vl-* functions do not require the prior evaluation of (vl-load-com), this is only required for ActiveX functions (vla-*/vlax-*).
After some additional research, I found these two functions: vl-princ-to-string and vl-prin1-to-string.
(vl-princ-to-string (type "Test"))
"STR"
(vl-prin1-to-string (type "Test"))
"STR"

Enum Solidity Understanding

I am trying to understand solidity bit better, however the underscores drive me crazy.
Let’s say I declare an enum
enum wine {red, white}
And now I declare a variable of type enum and I assign a value
wine _wine = wine.red
Why do I need there an underscore in front of the second wine?
Happy for comments!
You cannot have a variable with the same name as an already existing datatype.
Example from many other typed languages:
// ok
string _string = "hello";
// error - cannot have a `string` type variable called `string`
string string = "hello";
In your case, wine specifies the datatype, and _wine is the variable name.

Not able to print the value of a key of a Map in Kotlin

I'm fairly new to Kotlin. I want to print the count of a character in a string. For this, I'm using Kotlin's groupingBy() function and applying eachCount() to it.
My code:
val str = "0100101101010"
val countMap : Map<Char, Int> = str.groupingBy { it }.eachCount()
println(countMap["1"])
But I'm getting this error in the console: Type inference failed. The value of the type parameter K should be mentioned in input types (argument types, receiver type or expected type). Try to specify it explicitly.
Can someone explain to me what I'm doing wrong here?
"1" is a string literal, but your map has characters as keys. You should use the character literal '1':
println(countMap['1'])
The reason for that confusing error message is because it is trying to call this get overload, which is generic. It tries to infer the generic type arguments, and fails.

Type alias for list of other types in Elm

If I try to define new type alias, for example, in a following manner:
type alias ListOfInts = [Int]
I get following error:
I was partway through parsing a type alias, but I got stuck here:
11| type alias ListOfInts = [Int]
^
I was expecting to see a type next. Try putting Int or String for now?
Is there a way to define type alias for Lists in Elm?
Unlike some other languages such as Haskell, Elm does not use square brackets as special notation for its list type. Consequently, as the compiler tells you, it can't make sense of the [ in a context where it expects to see a type.
The type is instead written List Int, as you can find for example in the Elm guide.
So just change your code to this:
type alias ListOfInts = List Int

type +'a t in Ocaml Map Library?

I'm working with Ocaml's built-in Map library for a problem set, and I'm having trouble accessing the datatype of a map itself. This is supposed to be the third implementation of a dictionary (the first two being a list and an unbalanced binary search tree), and part of the functor I have to implement is "type dict", which is the datatype of the actual dictionary. For list, type dict was (D.key * D.value) list; for the tree, type dict was Empty | Branch((D.key * D.value), dict, dict). The Ocaml documentation says:
type +'a t
The type of maps from type key to type 'a.
This seems like what I need, but I can't seem to use it correctly. M is my Map.Make module, by the way. I've tried
type dict = M.t
type dict = M.+D.value t
type dict = M.+
But I keep getting error messages. Can anyone help? Thanks so much!
+ is a variance annotation, it is not part of the name. The syntax of parametrized type is param type or (param, param, ...) type in OCaml : int list, (int, string) Hashbl.t. What you want here is D.value M.t.
You can find out yourself by asking the ocaml compiler: ocamlc -i file.ml
To create a map via Map.Make from the standard ocaml library, this file would look like this (for a map from int to 'a):
module Intmap = Map.Make (struct
type t = int
let compare = Pervasives.compare
end)
The ocaml compiler will give you something like this (when calling ocamlc -i mymap.ml):
module Intmap :
sig
type key = int
type +'a t
val empty : 'a t
...
end