The Crystal programming language | http://crystal-lang.org | Fund Crystal's development: http://is.gd/X7PRtI | Docs: http://crystal-lang.org/docs/ | API: http://crystal-lang.org/api/
Foo.new 1
or Foo.new id: 1
alias DeviceName = String
alias DeviceFile = Int32
alias DeviceDir = Hash(DeviceName, DeviceFile | DeviceDir)
pp Hash(DeviceName, DeviceDir).new
pp Hash(DeviceName, DeviceFile).new
pp DeviceDir.new
#=> 21 | pp DeviceDir.new
#=> ^--
#=> Error: undefined method 'new' for DeviceDir.class
DeviceDir
is an alias of Hash(DeviceName, DeviceFile | DeviceDir)
, why can't I call .new
on it?
related: crystal-lang/crystal#5155
recursive aliases are usually frowned upon, so might want to go to plan b
Hi all, does anyone know of a good way to type an array of generic classes which is populated with an instance method using (forall)?
context here: https://play.crystal-lang.org/#/r/e7ts
Bonus: i was mucking around and did a bad here: https://play.crystal-lang.org/#/r/e7tt
Hi all, does anyone know of a good way to type an array of generic classes which is populated with an instance method using (forall)?
context here: https://play.crystal-lang.org/#/r/e7ts
Options
array can hold, but add
method isn't imposing any restriction, so it might break for cases where you call add
with type which isn't part of your expected types.
https://play.crystal-lang.org/#/r/e7ul
this snippet contains a macro check for add
method and ensure only allowed types are allowed at compile time. So if you try invoking add
method with Types which aren't part of Type
alias, it will fail to compile.
e.g. invoking add
with Int64
will raise compile time error.
Ah, thanks.
also in your class you have restricted the types which Options array can hold, but add method isn't imposing any restriction, so it might break for cases where you call add with type which isn't part of your expected types.
Good call, I'd actually be fine without such restriction; just an array of OptionBuilder(T). I'd like to use the type to cast string values to (T), but probably not the best idea: https://play.crystal-lang.org/#/r/e7uu
Compiler is right here, so you would have to perform checks before you can invoke methods on Union Type
looking for something in particular?
Was looking for something lightweight, extensible and up to date with latest Crystal.
I was looking into Admiral and Commander, but I think I'm going to poke around the docs for Athena console.
I want to attach a CLI to a task runner (base/abstract class), but also be able to compose (sub)commands in implementations of the task runner.
:
as a way to group related commands. E.g. admin:create-user
or admin:sync-data
, etc