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

Type checking in Crystal
Condition over object's type
Crystal C bindings: argument const unsigned char **
Crystal C bindings: return type char*
Make Class member attribute of type Array(T) accept 2D arrays of T
JSON.mapping if “root” attribute has inherit keys
Crystal C bindings: using fopen
Define Class object validations
Set attribute of type “Class” to define it as Int32, String, Float64
Set both default attribute and type of attribute
How can I test private methods?
Write binary data to stdout in Crystal
Unable to compile after upgrade from crystal 0.19.4 to 0.20
Application server for Crystal web app / Kemal
Crystal Fiber error - “Unhandled exception in spawn: SSL_shutdown: Operation now in progress (Errno)”
Convert hex string to int in Crystal

Categories

HOME
url-redirection
google-spreadsheet
angular-formly
youtube-data-api-v3
jwplayer
graphics
can
google-apps
3d-reconstruction
google-cloud-datalab
pkcs#11
openstreetmap
vimdiff
fhir
heap
ycsb
nancy
plink
linear-algebra
standards
fileserver
google-content-api
google-data-studio
simpleitk
backpack-for-laravel
php-mysqlidb
roundup
pylons
corpus
linked-data
sinch
browser-sync
recurring-events
kudu
cart
kodi
jmockit
laravel-eloquent
biztalk-rule-engine
activexobject
pop3
least-squares
news
kill
preloader
install.packages
tcc
signal-strength
rfc3339
dapper-simplecrud
datamaps
openblas
compiler-design
avx
template-toolkit
swingworker
juice-ui
pure-css
redistributable
reporting-services-2012
ziparchive
pacemaker
signalr.client
macaulay2
x-tag
wso2developerstudio
installshield-2010
dimension
ikiwiki
contentcontrol
mobiledoc-kit
asyncdisplaykit
gocql
toolkit
viola-jones
apache-commons-daemon
dnssec
cwrsync
git-ftp
phpldapadmin
delayed-execution
sip-server
tweenlite
raygun
mdichild
create.js
ignite-ui
radiant
maven-scm
synapse
scringo
diem
android-loadermanager
thttpd
window-decoration
jsr286
android-holo-everywhere
webdev.webserver
inline-formset
silent
pos-for-.net
web-application-project
alsb
int64
java1.4
dsoframer
image-capture
idatareader

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App