crystal-lang


Crystal: abstract static method


Is it possible to define abstract static methods?
I'm trying:
abstract struct MyStruct
abstract def self.myfun # ERR
abstract def MyStruct::myfun # ERR
end
Abstract class methods don't appear to be a language feature:
abstract class Abstract
abstract self.doit
end
# => Syntax error in /home/bmiller/test.cr:23: unexpected token: self
However you could always delegate to an instance:
abstract class Parent
def self.instance
##instance ||= new
end
def self.doit
instance.doit
end
abstract def doit
end
class Child < Parent
def doit
"ok"
end
end
p Parent.doit # => can't instantiate abstract class Parent
p Child.doit # => "ok"
I faced the same problem and figured out a (in my opinion) nicer solution:
abstract class Something
module ClassMethods
abstract def some_abstract_class_method
end
extend ClassMethods
abstract def some_abstract_instance_method
end
The documentation mentions that module methods can be made abstract as well, so this builds on top of that.
Implementing this class without implementing the class method some_abstract_class_method will raise an error, as expected.

Related Links

What object receives #puts in my Crystal programs?
Avoid repeating variable name in assignation
Sharing variables between different macros
passing json to a superclass using .from_json
execution of command failed with code: 1: `cc -o "/root/.cache/crystal/var-app-staging-new-http.cr
How to use union in “if” statement [Crystal]
How to convert a hex string to bytes array in Crystal?
is INSTANCE = new in class is run after compilation automatically or the first time it is accessed?
Crystal lang: understanding method return type of Void
Crystal-lang httpget basic_auth
How to control using CPU cores [Crystal]
How can I read a slice with other size in crystal?
Unix-style password readline
Cannot find -lxml2 when trying to run crystal spec
How do I access an object's eigenclass in Crystal?
Why does my code never reaches the end when waiting for pipe?

Categories

HOME
embedded
delphi-7
c++11
c99
ecmascript-6
object
shiro
nuget
bookmarks
jtextpane
character
elasticsearch-5
fosrestbundle
commonjs
android-securityexception
mxgraph
angular-dart
symbol
libusb
alpacajs
symfony-2.8
jersey-1.0
xmonad
jmp
selinux
lilypond
microsoft-ocr
cgbitmapcontext
react-toolbox
visual-studio-extensions
monit
dapper-extensions
pic32
pylons
underscore.js
apktool
ceylon
paperjs
cppreference
sqsh
panoramas
event-flow
record
git-squash
biztalk-rule-engine
openwhisk
carriage-return
arduino-ide
latitude-longitude
sim-toolkit
xcode7.3
openalpr
z-notation
gdata
css-position
urlsession
xv6
preloader
dbnull
signal-strength
serde
dbcontext
manova
mongotemplate
java-bytecode-asm
boxing
cup
template-toolkit
pybrain
gameplay-kit
grails-plugin
microstation
qtplugin
drawstring
jquery-jscrollpane
tooleap
android-dateutils
gogs
appscale
jparsec
maybe
vigenere
lasso
skspritenode
avahi
mmwormhole
procedural-programming
rapidsvn
viewer
ghcjs
encryption-asymmetric
with-statement
template-deduction
kissfft
rcaller
vsx
decoupling
koala
rfc5322
inputmismatchexception
isqlquery
oocss
rolify
neos-server
workitem
video-codecs
onload-event
fb.ui
android-sdk-2.3
device-width
adobe-media-server
boost-signals
mbeans
massive
collectionviewsource
mtu
commerceserver2007
metaweblog
objective-j
binomial-heap
lxr
strtod
dsl-tools
sifr3
escrow

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App